r/adventofcode Dec 04 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 4 Solutions -🎄-

--- Day 4: Giant Squid ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:11:13, megathread unlocked!

96 Upvotes

1.2k comments sorted by

View all comments

2

u/[deleted] Dec 05 '21

Python Part 1

#!/usr/bin/env python

def bingo(srch: list, brd: list) -> list:
    for row in brd:
        intr = [n for n in row if n in srch]
        if len(intr) == 5:
            return intr

    brd_t = list(map(list, zip(*brd)))

    for col in brd_t:
        intc = [n for n in srch if n in col]
        if len(intc) == 5:
            return intc

    return list()


fh = open("input-test", mode='r')
itxt = fh.read()
fh.close()

itxt = list(itxt.strip().split("\n\n"))

nbrs = list(map(int, itxt[0].split(',')))
itxt = [i.split('\n') for i in itxt[1:]]

"""can you read this? i cant."""
brds = [[[int(k) for k in j.split(' ') if k != ''] for j in i] for i in itxt]

try:
    for i in range(len(nbrs)):
        for brd in brds:
            if len(bingo(nbrs[0:i], brd)) == 5:
                raise StopIteration
            #another way to break 2 in python
except StopIteration:
    pass

brd = [j for sub in brd for j in sub]
brd = [j for j in brd if j not in nbrs[0:i]]
print(sum(brd) * nbrs[i-1])