From 0756cf74c4a1955e0b2ad8e472220acd5771fa8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sat, 9 Dec 2023 12:36:03 +0100 Subject: [PATCH] 2021 day 6. --- 2021/day4.py | 9 +++++++-- 2021/day6.py | 18 +++++++++++++----- 2021/inputs/day6.txt | 1 + 2021/tests/day6.txt | 1 + 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/2021/day4.py b/2021/day4.py index 73da546..2e86096 100644 --- a/2021/day4.py +++ b/2021/day4.py @@ -13,12 +13,15 @@ boards = np.asarray( ] ) -# part 1 -marked = np.zeros_like(boards, dtype=bool) +# (round, score) for each board (-1 when not found) winning_rounds: list[tuple[int, int]] = [(-1, -1) for _ in range(len(boards))] +marked = np.zeros_like(boards, dtype=bool) + for round, number in enumerate(numbers): + # mark boards marked[boards == number] = True + # check each board for winning for index in range(len(boards)): if winning_rounds[index][0] > 0: continue @@ -29,9 +32,11 @@ for round, number in enumerate(numbers): number * int(np.sum(boards[index][~marked[index]])), ) + # all boards are winning - break if np.all(marked.all(axis=1) | marked.all(axis=2)): break +# part 1 (_, score) = min(winning_rounds, key=lambda w: w[0]) print(f"answer 1 is {score}") diff --git a/2021/day6.py b/2021/day6.py index 992bf35..5847186 100644 --- a/2021/day6.py +++ b/2021/day6.py @@ -1,13 +1,21 @@ import sys -from collections import defaultdict -from dataclasses import dataclass -lines = sys.stdin.read().splitlines() +values = [int(c) for c in sys.stdin.read().strip().split(",")] + +days = 256 +lanterns = {day: 0 for day in range(days)} +for value in values: + for day in range(value, days, 7): + lanterns[day] += 1 + +for day in range(days): + for day2 in range(day + 9, days, 7): + lanterns[day2] += lanterns[day] # part 1 -answer_1 = ... +answer_1 = sum(v for k, v in lanterns.items() if k < 80) + len(values) print(f"answer 1 is {answer_1}") # part 2 -answer_2 = ... +answer_2 = sum(lanterns.values()) + len(values) print(f"answer 2 is {answer_2}") diff --git a/2021/inputs/day6.txt b/2021/inputs/day6.txt index e69de29..aa0969e 100644 --- a/2021/inputs/day6.txt +++ b/2021/inputs/day6.txt @@ -0,0 +1 @@ +2,3,1,3,4,4,1,5,2,3,1,1,4,5,5,3,5,5,4,1,2,1,1,1,1,1,1,4,1,1,1,4,1,3,1,4,1,1,4,1,3,4,5,1,1,5,3,4,3,4,1,5,1,3,1,1,1,3,5,3,2,3,1,5,2,2,1,1,4,1,1,2,2,2,2,3,2,1,2,5,4,1,1,1,5,5,3,1,3,2,2,2,5,1,5,2,4,1,1,3,3,5,2,3,1,2,1,5,1,4,3,5,2,1,5,3,4,4,5,3,1,2,4,3,4,1,3,1,1,2,5,4,3,5,3,2,1,4,1,4,4,2,3,1,1,2,1,1,3,3,3,1,1,2,2,1,1,1,5,1,5,1,4,5,1,5,2,4,3,1,1,3,2,2,1,4,3,1,1,1,3,3,3,4,5,2,3,3,1,3,1,4,1,1,1,2,5,1,4,1,2,4,5,4,1,5,1,5,5,1,5,5,2,5,5,1,4,5,1,1,3,2,5,5,5,4,3,2,5,4,1,1,2,4,4,1,1,1,3,2,1,1,2,1,2,2,3,4,5,4,1,4,5,1,1,5,5,1,4,1,4,4,1,5,3,1,4,3,5,3,1,3,1,4,2,4,5,1,4,1,2,4,1,2,5,1,1,5,1,1,3,1,1,2,3,4,2,4,3,1 diff --git a/2021/tests/day6.txt b/2021/tests/day6.txt index e69de29..55129f1 100644 --- a/2021/tests/day6.txt +++ b/2021/tests/day6.txt @@ -0,0 +1 @@ +3,4,3,1,2