-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path22a_aoc.py
104 lines (99 loc) · 2.78 KB
/
22a_aoc.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
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)
vc.display_grid(5)
for i in range(10000):
vc.burst()
vc.display_grid(5)
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
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
def burst(self):
pt = self.position
if self.get(pt)=='#':
self.turn(RIGHT)
self.set(pt, '.')
else:
self.turn(LEFT)
self.set(pt, '#')
self.infect_counter += 1
self.move_fwd()
main()