-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunstable_diffusion.py
122 lines (83 loc) · 2.49 KB
/
unstable_diffusion.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
from utils import read_input, run, run_test
from operator import mul
from collections import defaultdict
FNAME = "23/input.txt"
def add(c1, c2):
return tuple(a + b for a, b in zip(c1, c2))
E = (0, 1)
S = (1, 0)
W = (0, -1)
N = (-1, 0)
SE = add(S, E)
SW = add(S, W)
NW = add(N, W)
NE = add(N, E)
def parse_input(input_file):
data = read_input(input_file, parse_chunk=lambda l: l)
elves = set()
for i, row in enumerate(data):
for j, char in enumerate(row):
if char == '#':
elves.add((i, j))
return elves
##########
# PART 1 #
##########
def are_adjacent_free(elves, pos, directions):
return not elves.intersection({add(pos, d) for d in directions})
def starting_directions():
return [
(N, NE, NW),
(S, SE, SW),
(W, NW, SW),
(E, NE, SE),
]
DIRECTIONS = starting_directions()
def propose_move(elves, pos):
if not are_adjacent_free(elves, pos, [N, E, S, W, NE, SE, SW, NW]):
for directions in DIRECTIONS:
if are_adjacent_free(elves, pos, directions):
# print("moving")
return add(pos, directions[0])
return pos
def smallest_rectangle(elves):
def key(i):
return lambda e: e[i]
height = max(elves, key=key(0))[0] - min(elves, key=key(0))[0] + 1
width = max(elves, key=key(1))[1] - min(elves, key=key(1))[1] + 1
return width * height
def simulate_round(elves):
proposed_positions = defaultdict(set)
for e in elves:
proposed_positions[propose_move(elves, e)].add(e)
new_elves = set()
for new_pos, old_positions in proposed_positions.items():
if len(old_positions) == 1:
new_elves.add(new_pos)
else:
new_elves |= old_positions
DIRECTIONS.append(DIRECTIONS.pop(0))
return new_elves
def part_one(input_file):
elves = parse_input(input_file)
for _ in range(10):
elves = simulate_round(elves)
return smallest_rectangle(elves) - len(elves)
##########
# PART 2 #
##########
def part_two(input_file):
global DIRECTIONS
DIRECTIONS = starting_directions()
elves = parse_input(input_file)
round_no = 1
while True:
new_elves = simulate_round(elves)
if new_elves == elves:
break
elves = new_elves
round_no += 1
return round_no
if __name__ == '__main__':
# run_test(part_one, '23/test_input.txt', 25, 'Example input')
run(part_one, part_two, FNAME)