-
Notifications
You must be signed in to change notification settings - Fork 0
/
day24_part1.py
executable file
·70 lines (60 loc) · 2.04 KB
/
day24_part1.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
#!/usr/bin/python3
import loader
inp = loader.string_list("input_day24")
def parse_direction_line(direction_line_text):
directions = []
i = 0
while i < len(direction_line_text):
if direction_line_text[i] in ('e','w'):
directions.append(direction_line_text[i])
i += 1
continue
elif direction_line_text[i:i+2] in ('nw','ne','sw','se'):
directions.append(direction_line_text[i:i+2])
i += 2
continue
else:
raise RuntimeError(f"Error parsing line {direction_line_text} at offset {i}.")
return directions
parsed_directions = [parse_direction_line(line) for line in inp]
def get_canonical_coordinates(direction_list):
"""
Takes a direction list as produced by parse_direction_line.
Returns a 2-tuple (x,y) where x and y are integers identifying a hexagon.
x is the number of 'e' moves necessary and y is the number of 'ne' moves necessary to reach it.
If x or y is negative, that means a corresponding number of 'w' and/or 'sw' moves are necessary to reach it.
"""
x = 0
y = 0
for move in direction_list:
if move == "e":
x += 1
continue
elif move == "w":
x -= 1
continue
elif move == "ne":
y += 1
continue
elif move == "sw":
y -= 1
continue
elif move == "nw":
x -= 1
y += 1
continue
elif move == "se":
x += 1
y -= 1
continue
else:
raise ValueError(f"Unknown move {move}")
return (x,y)
indicated_tiles_canonical_coordinates = [get_canonical_coordinates(direction_list) for direction_list in parsed_directions]
black_tiles = 0
for identified_tile_coords in set(indicated_tiles_canonical_coordinates):
if len([coords for coords in indicated_tiles_canonical_coordinates if coords == identified_tile_coords]) % 2 == 0:
continue
else:
black_tiles += 1
print(black_tiles)