-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path22b_aoc.py
More file actions
134 lines (129 loc) · 3.65 KB
/
22b_aoc.py
File metadata and controls
134 lines (129 loc) · 3.65 KB
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
def main():
grid = []
rows = 0
cols = 0
try:
while True:
arg = raw_input()
rows += 1
cols = len(arg)
grid.append(list(arg))
except EOFError:
solve(grid)
def solve(arg):
vc = VirusCarrier(arg)
size = 18
vc.display_grid(size)
# for i in range(100):
# for j in range(100000):
# vc.burst()
# vc.display_grid(size)
# print i+1,"%"
for i in range(100):
for j in range(100000):
vc.burst()
vc.display_grid(size)
print i+1,"%"
print vc.infect_counter
def get_fromgrid(grid,x,y):
rows = len(grid)
cols = len(grid[0])
try:
return grid[rows/2-y][cols/2+x]
except IndexError:
#print "tried to access coords,",x,y
return '.'
UP,DOWN,LEFT,RIGHT = 1,2,3,4
REVERSE = 5
CLEAN = '.'
WEAKENED = 'W'
INFECTED = '#'
FLAGGED = 'F'
class VirusCarrier():
"""Virus Carrier"""
def __init__(self,grid):
self.grid = {}
self.position = (0,0)
self.facing = UP
self.infect_counter = 0
for y in range(-len(grid)/2+1,len(grid)/2+1):
for x in range(-len(grid[0])/2+1,len(grid[0])/2+1):
self.grid[(x,y)] = get_fromgrid(grid,x,y)
def display_grid(self, size):
out = ""
for y in range(size,-size,-1):
for x in range(-size,size):
out += (" %c " % self.get((x,y))
if (x,y)!=self.position
else "[%c]" % self.get((x,y)))
out += "\n"
print out
def get(self, coord):
try:
return self.grid[coord]
except KeyError as e:
self.grid[coord] = '.'
return '.'
def set(self, coord, val):
try:
self.grid[coord] = val
except KeyError as e:
self.grid[coord] = val
def move_fwd(self):
x,y = self.position
if self.facing == UP:
y += 1
elif self.facing == LEFT:
x -= 1
elif self.facing == DOWN:
y -= 1
elif self.facing == RIGHT:
x += 1
self.position = x,y
def turn(self, arg):
if arg == LEFT:
if self.facing == UP:
self.facing = LEFT
elif self.facing == LEFT:
self.facing = DOWN
elif self.facing == DOWN:
self.facing = RIGHT
elif self.facing == RIGHT:
self.facing = UP
if arg == RIGHT:
if self.facing == UP:
self.facing = RIGHT
elif self.facing == RIGHT:
self.facing = DOWN
elif self.facing == DOWN:
self.facing = LEFT
elif self.facing == LEFT:
self.facing = UP
if arg == REVERSE:
if self.facing == UP:
self.facing = DOWN
elif self.facing == RIGHT:
self.facing = LEFT
elif self.facing == DOWN:
self.facing = UP
elif self.facing == LEFT:
self.facing = RIGHT
def burst(self):
pt = self.position
cur_node = self.get(pt)
if cur_node == CLEAN:
self.turn(LEFT)
cur_node = WEAKENED
elif cur_node == WEAKENED:
# does not turn
cur_node = INFECTED
self.infect_counter += 1
elif cur_node == INFECTED:
self.turn(RIGHT)
cur_node = FLAGGED
elif cur_node == FLAGGED:
self.turn(REVERSE)
cur_node = CLEAN
self.set(pt, cur_node)
self.move_fwd()
main()