-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
day_23.py
129 lines (112 loc) · 3.3 KB
/
day_23.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from collections import Counter
from itertools import count
import aoc_helper
from aoc_helper import (
Grid,
PrioQueue,
SparseGrid,
decode_text,
extract_ints,
extract_iranges,
extract_ranges,
extract_uints,
frange,
irange,
iter,
list,
map,
range,
tail_call,
)
raw = aoc_helper.fetch(23, 2022)
def parse_raw(raw):
data = SparseGrid(bool)
for y, row in enumerate(raw.splitlines()):
for x, char in enumerate(row):
data[x, y] = char == "#"
return data
data = parse_raw(raw)
def decide(data, x, y, turn):
if not any(data[x + i, y + j] for i in (-1, 0, 1) for j in (-1, 0, 1) if i or j):
return x, y
options = [
(
[data[x - 1, y - 1], data[x, y - 1], data[x + 1, y - 1]],
x,
y - 1,
),
([data[x - 1, y + 1], data[x, y + 1], data[x + 1, y + 1]], x, y + 1),
([data[x - 1, y - 1], data[x - 1, y], data[x - 1, y + 1]], x - 1, y),
(
[data[x + 1, y - 1], data[x + 1, y], data[x + 1, y + 1]],
x + 1,
y,
),
]
# print(x, y)
for option in options[turn - 4 :] + options[:turn]:
# print(option)
if not any(option[0]):
return option[1], option[2]
return x, y
def part_one(_data: SparseGrid[bool]):
data = SparseGrid(bool)
data.data.update(_data.data)
for round in range(10):
# print(f"Round {round}")
# data.pretty_print(lambda I: ".#"[I], [False])
elf_decisions = {
(x, y): decide(data, x, y, round % 4)
for (x, y), is_elf in list(data.items())
if is_elf
}
chosen_per_square = Counter(elf_decisions.values())
for (x, y), (new_x, new_y) in elf_decisions.items():
if chosen_per_square[new_x, new_y] == 1:
data[x, y] = False
data[new_x, new_y] = True
x, y, max_x, max_y = data.bounds([False])
# data.pretty_print(lambda I: ".#"[I], [False])
return (max_y - y + 1) * (max_x - x + 1) - sum(data.values())
aoc_helper.lazy_test(
day=23,
year=2022,
parse=parse_raw,
solution=part_one,
test_data=(
"""..............
..............
.......#......
.....###.#....
...#...#.#....
....#...##....
...#.###......
...##.#.##....
....#..#......
..............
..............
..............""",
110,
),
)
def part_two(_data):
data = SparseGrid(bool)
data.data.update(_data.data)
for round in count(1):
# print(f"Round {round}")
# data.pretty_print(lambda I: ".#"[I], [False])
elf_decisions = {
(x, y): decide(data, x, y, round % 4)
for (x, y), is_elf in list(data.items())
if is_elf
}
if all(from_ == to for (from_, to) in elf_decisions.items()):
return round
chosen_per_square = Counter(elf_decisions.values())
for (x, y), (new_x, new_y) in elf_decisions.items():
if chosen_per_square[new_x, new_y] == 1:
data[x, y] = False
data[new_x, new_y] = True
# aoc_helper.lazy_test(day=23, year=2022, parse=parse_raw, solution=part_two)
aoc_helper.lazy_submit(day=23, year=2022, solution=part_one, data=data)
aoc_helper.lazy_submit(day=23, year=2022, solution=part_two, data=data)