-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.py
134 lines (114 loc) · 3.23 KB
/
day23.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
130
131
132
133
134
from math import inf
def makeInput(file):
with open(file) as f:
lines = f.readlines()
grid = []
for x in lines:
line = list(x.strip())
grid.append(line)
return grid
def willMove(elf,elves):
steps = [-1,0,1]
y,x = elf
for xStep in steps:
for yStep in steps:
if not (xStep == 0 and yStep == 0):
if (y+yStep,x+xStep) in elves:
return True
return False
def willMoveNorth(elf,elves):
y,x = elf
yStep = -1
xSteps = [-1,0,1]
for xStep in xSteps:
if (y+yStep,x+xStep) in elves:
return elf
return (y-1,x)
def willMoveSouth(elf,elves):
y,x = elf
yStep = 1
xSteps = [-1,0,1]
for xStep in xSteps:
if (y+yStep,x+xStep) in elves:
return elf
return (y+1,x)
def willMoveWest(elf,elves):
y,x = elf
xStep = -1
ySteps = [-1,0,1]
for yStep in ySteps:
if (y+yStep,x+xStep) in elves:
return elf
return (y,x-1)
def willMoveEast(elf,elves):
y,x = elf
xStep = 1
ySteps = [-1,0,1]
for yStep in ySteps:
if (y+yStep,x+xStep) in elves:
return elf
return (y,x+1)
file = 'day23.txt'
grid = makeInput(file)
# for row in grid:
# print(row)
elfSet = set()
elfList = []
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '#':
elfSet.add((i,j))
elfList.append((i,j))
dirs = ['N','S','W','E']
moveDict = {'N':willMoveNorth, 'S': willMoveSouth,'W': willMoveWest,'E':willMoveEast}
elvesMoving = True
i = 0
while elvesMoving:
elvesMoving = False
newElfSet = set()
newElfList = []
for elf in elfList:
if not willMove(elf, elfSet): #elf's position will not change
newElfList.append(elf)
newElfSet.add(elf)
dirOrder = [j % 4 for j in range(i,i+4)]
#print(dirOrder)
proposedDict = {}
for elf in elfList:
#print(elf)
if elf not in newElfSet: #elf position will change, check each direction
elfMoved = False
for j in dirOrder:
dirFunc = moveDict[dirs[j]]
newElf = dirFunc(elf,elfSet)
if newElf != elf:
if newElf in proposedDict:
proposedDict[newElf].append(elf)
else:
proposedDict[newElf] = [elf]
elfMoved = True
break
if not elfMoved:
newElfList.append(elf)
newElfSet.add(elf)
for newElf in proposedDict:
if len(proposedDict[newElf]) > 1: #multiple elves proposed same location, no elves move
newElfList += proposedDict[newElf]
newElfSet.update(proposedDict[newElf])
else:
elvesMoving = True
newElfList.append(newElf)
newElfSet.add(newElf)
elfSet = newElfSet
elfList = newElfList
i += 1
xMax = yMax = -inf
xMin = yMin = inf
for elf in elfList:
yMin = min(yMin,elf[0])
yMax = max(yMax,elf[0])
xMin = min(xMin,elf[1])
xMax = max(xMax,elf[1])
print((yMax-yMin + 1) * (xMax-xMin + 1))
print((yMax-yMin + 1) * (xMax-xMin + 1) - len(elfList))
print(i)