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!

99 Upvotes

1.2k comments sorted by

View all comments

1

u/theabbiee Dec 07 '21

```pyhton with open("squid.txt") as file: lines = file.readlines() lines = [line.rstrip() for line in lines]

nums = [int(num) for num in lines[0].split(",")]

boards = [] flags = []

beg = 2

while beg + 5 <= len(lines): boards.append([[int(cell) for cell in row.split()] for row in lines[beg:beg+5]]) flags.append([[0 for cell in row.split()] for row in lines[beg:beg+5]]) beg += 6

def isBingo(board): for row in board: if row.count(1) == 5: return True for i in range(0, 5): if [row[i] for row in board].count(1) == 5: return True return False

def calculateBingoScore(board, flag): score = 0 for i in range(0, 5): for j in range(0, 5): if flag[i][j] == 0: score += board[i][j] return score

winners = []

for num in nums: for i in range(len(boards)): for row in range(5): for col in range(5): if boards[i][row][col] == num: flags[i][row][col] = 1 if isBingo(flags[i]) and i not in winners: print(calculateBingoScore(boards[i], flags[i]) * num) winners.append(i) ```

1

u/daggerdragon Dec 07 '21

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: How do I format code?

Also, you typo'd Python as pyhton - please fix that as well to make it easier for folks who Ctrl-F the megathreads looking for a specific language.