/ Python / 2017 / 22.py
22.py
 1  from lib import *
 2  
 3  input = read_input(2017, 22)
 4  
 5  lines = input.splitlines()
 6  
 7  
 8  infected = set()
 9  x, y = len(lines[0]) // 2, len(lines) // 2
10  dx, dy = 0, -1
11  out = 0
12  for i, line in enumerate(lines):
13      for j, c in enumerate(line):
14          if c == "#":
15              infected.add((j, i))
16  
17  for _ in range(10000):
18      inf = (x, y) in infected
19      if inf:
20          dx, dy = -dy, dx
21          infected.remove((x, y))
22      else:
23          dx, dy = dy, -dx
24          infected.add((x, y))
25          out += 1
26      x += dx
27      y += dy
28  
29  print(out)
30  
31  
32  state = {}
33  x, y = len(lines[0]) // 2, len(lines) // 2
34  dx, dy = 0, -1
35  out = 0
36  for i, line in enumerate(lines):
37      for j, c in enumerate(line):
38          if c == "#":
39              state[(j, i)] = 2
40  
41  for _ in range(10000000):
42      s = state.get((x, y), 0)
43      if s == 0:
44          dx, dy = dy, -dx
45      elif s == 2:
46          dx, dy = -dy, dx
47      elif s == 3:
48          dx, dy = -dx, -dy
49      if s == 1:
50          out += 1
51      if s == 3:
52          state.pop((x, y))
53      else:
54          state[(x, y)] = s + 1
55      x += dx
56      y += dy
57  
58  print(out)