advent-of-code

advent of code (partial solutions)
Log | Files | Refs | README

commit 4b1085f718dd91c4fa5f4cbacef5dcb160941df9
parent 57bcf9b90a8671683ca7da2b025e524e477a0e24
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Fri,  3 Dec 2021 21:42:22 -0800

ugly aoc04

Diffstat:
A2021/aoc04.py | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+), 0 deletions(-)

diff --git a/2021/aoc04.py b/2021/aoc04.py @@ -0,0 +1,51 @@ +import sys +from collections import Counter + +lines = sys.stdin.read()[:-1].split("\n\n") + +nums = map(int, lines[0].split(",")) + + +def card_to_arr(card): + return [*map(lambda x: [*map(int, x.split())], card.split("\n"))] + +def check_win(card): + for row in card: + if all(x == -1 for x in row): + return True + for i in range(5): + win = True + for row in card: + if row[i] != -1: + win = False + if win: + return True + return False + +cards = [*map(card_to_arr, lines[1:])] + +def mark_value(n, card, val): + for row in card: + for c in range(len(row)): + if row[c] == n: + row[c] = val + +def print_cards(): + for card in cards: + for line in card: + print(line) + print("") + +for n in nums: + print(n) + for card in cards: + mark_value(n, card, -1) + if check_win(card): + print("WIN") + mark_value(-1, card, 0) + print(sum(map(sum, card)) * n) + for i in range(5): + card[i][i] = -2 # invalidate card + if n == 13: + print_cards() +