You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.1 KiB
Python
54 lines
1.1 KiB
Python
"""Solution for 2020/24"""
|
|
|
|
from collections import defaultdict
|
|
|
|
instructions = []
|
|
with open('input', 'r') as f:
|
|
for line in f.readlines():
|
|
line = line.strip()
|
|
i = 0
|
|
instruction = []
|
|
while i < len(line):
|
|
if line[i] in ['w', 'e']:
|
|
instruction.append(line[i])
|
|
else:
|
|
instruction.append(line[i:i+2])
|
|
i += 1
|
|
i += 1
|
|
instructions.append(instruction)
|
|
|
|
|
|
print(instructions)
|
|
|
|
MOVEMENTS = {
|
|
'e': (1, 0),
|
|
'w': (-1, 0),
|
|
'ne': (0, 1),
|
|
'sw': (0, -1),
|
|
'nw': (-1, 1),
|
|
'se': (1, -1),
|
|
}
|
|
|
|
|
|
def parse_instruction(instruction):
|
|
coord = (0, 0)
|
|
for move in instruction:
|
|
dx, dy = MOVEMENTS[move]
|
|
coord = (coord[0] + dx, coord[1] + dy)
|
|
return coord
|
|
|
|
|
|
def main():
|
|
flipped_tiles = defaultdict(int)
|
|
|
|
for instruction in instructions:
|
|
coord = parse_instruction(instruction)
|
|
flipped_tiles[coord] += 1
|
|
|
|
print(''.join(instruction), coord)
|
|
blacks = [x[0] for x in flipped_tiles.items() if x[1] % 2 == 1]
|
|
print('flipped', blacks)
|
|
print(len(blacks))
|
|
|
|
main()
|