123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- input = []
- with open("24.input") as f:
- for line in f.readlines():
- seq = []
- i = iter(list(line.strip()))
- c1 = next(i, None)
- while c1 != None:
- if c1 in ['s', 'n']:
- seq.append(c1 + next(i))
- else:
- seq.append(c1)
- c1 = next(i, None)
- input.append(seq)
- def get_pos(seq):
- pos = [0, 0]
- for move in seq:
- if move == 'w':
- pos[0] -= 2
- elif move == 'e':
- pos[0] += 2
- else:
- if move[0] == 's':
- pos[1] += 1
- else:
- pos[1] -= 1
- if move[1] == 'w':
- pos[0] -= 1
- else:
- pos[0] += 1
- return tuple(pos)
- counts = {}
- for seq in input:
- pos = get_pos(seq)
- if pos not in counts:
- counts[pos] = 0
- counts[pos] += 1
- print("Answer 1:", len([v for v in counts.values() if v % 2 == 1]))
- def count_adjacent(map, pos):
- x, y = pos
- adjacent = [(-2, 0), (2, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
- return len([1 for (off_x, off_y) in adjacent if map.get((x + off_x, y + off_y), False)])
- map = {k: v % 2 == 1 for (k, v) in counts.items()}
- max_x = max([abs(x) for (x, y) in map.keys()]) + 1
- max_y = max([abs(y) for (x, y) in map.keys()]) + 1
- for i in range(0, 100):
- next_map = {}
- for x in range(-max_x, max_x + 1):
- for y in range(-max_y, max_y + 1):
- if (x + y) % 2 == 1:
- continue
- count = count_adjacent(map, (x, y))
- if map.get((x, y), False) == True:
- next_map[(x, y)] = count in [1, 2]
- else:
- next_map[(x, y)] = count == 2
- map = next_map
- max_y += 1
- max_x += 2
- #print("Day {}: {}".format(i + 1, sum([1 for v in map.values() if v])))
- print("Answer 2:", sum([1 for v in map.values() if v]))
|