Solve 2021 day 4 and 5
This commit is contained in:
52
2021/d4.py
Normal file
52
2021/d4.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
from lib import get_data
|
||||||
|
from lib import ints
|
||||||
|
|
||||||
|
data = get_data(__file__).strip()
|
||||||
|
|
||||||
|
boards = []
|
||||||
|
numbers = None
|
||||||
|
for i, p in enumerate(data.split("\n\n")):
|
||||||
|
if i == 0:
|
||||||
|
numbers = ints(p)
|
||||||
|
else:
|
||||||
|
board = []
|
||||||
|
for line in p.splitlines():
|
||||||
|
board.append(ints(line))
|
||||||
|
boards.append(board)
|
||||||
|
|
||||||
|
assert numbers is not None
|
||||||
|
first_won = None
|
||||||
|
last_won = None
|
||||||
|
boards_done = list()
|
||||||
|
for n in numbers:
|
||||||
|
for i in range(len(boards)):
|
||||||
|
if i in boards_done:
|
||||||
|
continue
|
||||||
|
board = boards[i]
|
||||||
|
board = [[f if n != f else None for f in row] for row in board]
|
||||||
|
boards[i] = board
|
||||||
|
for row in board:
|
||||||
|
if all(cell is None for cell in row):
|
||||||
|
if not i in boards_done:
|
||||||
|
boards_done.append(i)
|
||||||
|
for col in zip(*board):
|
||||||
|
if all(cell is None for cell in col):
|
||||||
|
if not i in boards_done:
|
||||||
|
boards_done.append(i)
|
||||||
|
|
||||||
|
if len(boards_done) > 0 and first_won is None:
|
||||||
|
first_won = (i, n)
|
||||||
|
|
||||||
|
if len(boards_done) == len(boards):
|
||||||
|
last_won = (i, n)
|
||||||
|
break
|
||||||
|
if len(boards_done) == len(boards):
|
||||||
|
break
|
||||||
|
|
||||||
|
assert first_won is not None
|
||||||
|
assert last_won is not None
|
||||||
|
|
||||||
|
for i, n in [first_won, last_won]:
|
||||||
|
board = boards[i]
|
||||||
|
a = sum([sum([n if n is not None else 0 for n in row]) for row in board])
|
||||||
|
print(a * n)
|
||||||
39
2021/d5.py
Normal file
39
2021/d5.py
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
from lib import get_data
|
||||||
|
from lib import ints
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
data = get_data(__file__).strip()
|
||||||
|
|
||||||
|
counts = defaultdict(int)
|
||||||
|
for line in data.splitlines():
|
||||||
|
x1, y1, x2, y2 = ints(line)
|
||||||
|
if x1 == x2 or y1 == y2:
|
||||||
|
if x1 == x2:
|
||||||
|
for y in range(min(y1, y2), max(y1, y2) + 1):
|
||||||
|
counts[(x1, y)] += 1
|
||||||
|
elif y1 == y2:
|
||||||
|
for x in range(min(x1, x2), max(x1, x2) + 1):
|
||||||
|
counts[(x, y1)] += 1
|
||||||
|
else:
|
||||||
|
assert False
|
||||||
|
|
||||||
|
r = sum(1 for v in counts.values() if v > 1)
|
||||||
|
print(r)
|
||||||
|
|
||||||
|
|
||||||
|
for line in data.splitlines():
|
||||||
|
x1, y1, x2, y2 = ints(line)
|
||||||
|
if x1 == x2 or y1 == y2:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
dx = 1 if x2 > x1 else -1
|
||||||
|
dy = 1 if y2 > y1 else -1
|
||||||
|
|
||||||
|
while (x1, y1) != (x2, y2):
|
||||||
|
counts[(x1, y1)] += 1
|
||||||
|
x1 += dx
|
||||||
|
y1 += dy
|
||||||
|
counts[(x2, y2)] += 1
|
||||||
|
|
||||||
|
r = sum(1 for v in counts.values() if v > 1)
|
||||||
|
print(r)
|
||||||
@@ -175,7 +175,9 @@ Solutions and utility script for Advent of Code challenges in Python.
|
|||||||
- Day 1: 4:01 (Haha. Why am I so bad?!?!)
|
- Day 1: 4:01 (Haha. Why am I so bad?!?!)
|
||||||
- Day 2: 6:36 (Okay. I might as well stop doing these.)
|
- Day 2: 6:36 (Okay. I might as well stop doing these.)
|
||||||
- Day 3: 23:46 (How long can I take when I try to take long?)
|
- Day 3: 23:46 (How long can I take when I try to take long?)
|
||||||
- Day 4:
|
- Day 4: 22:18 (No way. Such stupid bugs.)
|
||||||
|
- Day 5: 13:30 (Decent, but obviously too slow.)
|
||||||
|
- Day 6:
|
||||||
|
|
||||||
## AoC 2022
|
## AoC 2022
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user