-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay22.py
129 lines (107 loc) · 4.79 KB
/
Day22.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
# Notation for direction will be as follows:
# 0
# 3 1
# 2
# Turning to the rights = (d+1)%4
# Turning to the left = (d-1)%4
def move(d, cX, cY):
if (d == 0):
cX = cX-1
if (d == 1):
cY = cY+1
if (d == 2):
cX = cX+1
if (d == 3):
cY = cY-1
return cX, cY
def work(mp, d, x, y):
#print("Current Pos = ", mp[x][y])
if (mp[x][y] == '#'):
d = (d+1)%4
mp[x][y] = '.'
else:
d = (d-1)%4
global becameInfected
becameInfected += 1
mp[x][y] = '#'
return mp, d
def expandMap(mp, cX, cY):
# If they are at the top of the map
if (cX == 0):
# Generate a new top line of clean nodes.
topLine = []
for i in range(len(mp[0])):
topLine.append('.')
mp.insert(0, topLine)
cX = 1
# If they are at the bottom of the map
if (cX == len(mp)):
# Generate a new bottom line of clean nodes.
bottomLine = []
for i in range(len(mp[0])):
bottomLine.append('.')
mp.insert(0, bottomLine)
# cX stays the same.
# If they are at the left of the map
if (cY == 0):
# Add a clean node to the start every line.
for i in range(len(mp)):
mp[i].insert(0, '.')
cY = 1
# If they are at the right of the map
if (cY == len(mp[0])):
# Add a clean node to the end every line
for i in range(len(mp)):
mp[i].append('.')
# cY stays in the same place.
return mp, cX, cY
def mapPrint(mp, cX, cY):
outStr = ""
for i in range(len(mp)):
for j in range(len(mp[0])):
if (i == cX and j == cY):
outStr += "@"
else:
outStr += mp[i][j]
outStr += "\n"
print(outStr)
mp = [\
[".",".",".","#","#",".","#",".","#",".","#","#","#","#",".",".",".","#","#","#",".",".",".",".","."],\
[".",".","#",".",".","#","#",".","#",".",".",".","#",".","#","#",".","#","#",".","#",".",".","#","."],\
[".","#",".","#",".","#",".","#","#","#",".",".",".",".","#",".",".",".","#","#","#",".",".",".","."],\
[".","#",".",".",".",".","#",".",".","#","#","#","#",".",".",".",".",".","#","#",".","#",".",".","#"],\
["#","#",".","#",".","#",".","#",".","#",".",".","#",".",".","#",".",".",".",".",".","#","#","#","."],\
["#",".",".",".","#","#",".",".",".",".","#","#",".","#","#",".","#",".","#","#",".","#","#",".","."],\
[".",".",".",".",".","#","#","#",".",".","#","#","#",".","#","#","#",".",".",".","#","#","#","#","#"],\
["#","#","#","#","#","#",".","#","#","#","#",".",".","#",".","#",".",".",".",".",".",".","#","#","."],\
["#",".",".","#","#","#",".","#","#","#","#",".",".","#","#","#","#",".",".",".",".",".",".",".","."],\
["#",".",".","#","#","#","#","#","#",".","#","#",".",".",".",".","#","#","#","#",".",".",".","#","#"],\
[".",".",".","#",".","#","#",".","#",".",".",".","#",".","#",".","#",".","#",".",".","#","#",".","#"],\
["#","#","#","#",".","#","#","#",".",".","#","#","#","#","#",".",".",".",".",".","#","#","#","#","."],\
["#",".","#",".","#",".",".",".",".","#",".","#","#","#","#",".",".",".","#","#","#","#",".",".","."],\
["#","#",".",".",".","#",".",".","#","#",".","#","#",".",".",".",".","#",".",".",".","#",".",".","."],\
[".",".",".",".",".",".","#","#",".",".","#","#",".",".","#",".",".","#",".",".","#","#","#","#","."],\
[".","#","#",".",".","#","#",".","#","#",".",".","#","#","#","#",".",".","#","#",".",".",".",".","#"],\
[".","#",".",".","#",".",".","#","#",".","#",".",".","#","#",".",".","#",".",".",".","#",".",".","."],\
["#",".","#",".","#","#",".",".",".",".",".","#","#",".",".","#","#",".","#","#","#","#","#",".","."],\
["#","#",".","#",".",".",".",".",".",".",".","#",".",".",".",".","#",".",".","#","#","#",".","#","."],\
["#","#",".",".",".","#",".",".",".","#",".",".",".",".","#","#","#",".",".","#",".","#",".","#","."],\
["#",".",".",".",".","#","#",".",".",".","#",".","#",".","#",".","#","#",".",".","#",".",".","#","#"],\
["#",".",".","#",".",".",".",".","#","#","#","#","#",".",".",".",".",".","#",".","#","#",".","#","."],\
[".","#",".",".",".","#",".",".","#",".",".","#","#","#",".",".",".",".","#","#","#",".",".","#","."],\
[".",".","#","#",".","#","#","#",".","#",".","#",".",".",".",".",".","#","#","#",".",".",".",".","."],\
["#",".","#",".","#",".","#",".","#",".","#","#",".","#","#",".",".",".","#","#",".","#","#",".","#"]]
#mp = [[".", ".", "#"], ["#", ".", "."], [".", ".", "."]]
direction = 0
becameInfected = 0
cX = len(mp)//2
cY = len(mp[0])//2
for i in range(10001):
mapPrint(mp, cX, cY)
if (i % 100 == 0):
print("Tick", i)
mp, direction = work(mp, direction, cX, cY)
cX, cY = move(direction, cX, cY)
mp, cX, cY = expandMap(mp, cX, cY)
print("Total nodes that became infected =", becameInfected-1)
# 5069 is too low...