-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23b.py
60 lines (48 loc) · 1.4 KB
/
day23b.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
elves = set()
for r, line in enumerate(open('input_day23.txt')):
for c, item in enumerate(line[:-1]):
if item == "#":
elves.add(c + r * 1j)
scanmap = {
-1j: [-1j - 1, -1j, -1j + 1],
1j: [1j - 1, 1j, 1j + 1],
1: [1 - 1j, 1, 1 + 1j],
-1: [-1 - 1j, -1, -1 + 1j]
}
moves = [-1j, 1j, -1, 1]
N = [-1 - 1j, -1j, -1j + 1, 1, 1 + 1j, 1j, 1j - 1, -1]
last = set(elves)
iter = 1
while True:
once = set()
twice = set()
for elf in elves:
if all(elf + x not in elves for x in N):
continue
for move in moves:
if all(elf + x not in elves for x in scanmap[move]):
prop = elf + move
if prop in twice:
pass
elif prop in once:
twice.add(prop)
else:
once.add(prop)
break
ec = set(elves)
for elf in ec:
if all(elf + x not in ec for x in N):
continue
for move in moves:
if all(elf + x not in ec for x in scanmap[move]):
prop = elf + move
if prop not in twice:
elves.remove(elf)
elves.add(prop)
break
moves.append(moves.pop(0))
if last == elves:
break
last = set(elves)
iter += 1
print(iter)