-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday24.py
118 lines (104 loc) · 3.42 KB
/
day24.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from aocd.models import Puzzle
def solve_puzzle_one(input):
# https://www.redblobgames.com/grids/hexagons/#coordinates
print(input)
cube = set()
for line in input:
x, y, z = 0, 0, 0
for direction in line:
if direction == "e":
x += 1
y -= 1
elif direction == "se":
y -= 1
z += 1
elif direction == "sw":
x -= 1
z += 1
elif direction == "w":
x -= 1
y += 1
elif direction == "nw":
y += 1
z -= 1
elif direction == "ne":
x += 1
z -= 1
if (x, y, z) in cube:
cube.remove((x, y, z))
else:
cube.add((x, y, z))
print("part1: " + str(len(cube)))
return cube
def solve_puzzle_two(input):
cube = solve_puzzle_one(input)
for _ in range(100):
relevant = set()
new_cube = set()
for (x, y, z) in cube:
for (dx, dy, dz) in [(0, 0, 0), (1, -1, 0), (0, -1, 1), (-1, 0, 1), (-1, 1, 0), (0, 1, -1), (1, 0, -1)]:
relevant.add((x + dx, y + dy, z + dz))
for (x, y, z) in relevant:
neighbours = 0
for (dx, dy, dz) in [(1, -1, 0), (0, -1, 1), (-1, 0, 1), (-1, 1, 0), (0, 1, -1), (1, 0, -1)]:
if (x + dx, y + dy, z + dz) in cube:
neighbours += 1
if (x, y, z) in cube:
if not (neighbours == 0 or neighbours > 2):
new_cube.add((x, y, z))
elif neighbours == 2:
new_cube.add((x, y, z))
cube = new_cube
print("part2: " + str(len(cube)))
def parse_input(data):
arr = []
for line in data.splitlines():
directions = []
while line:
if line.startswith("e"):
directions.append("e")
line = line[1:]
elif line.startswith("se"):
directions.append("se")
line = line[2:]
elif line.startswith("sw"):
directions.append("sw")
line = line[2:]
elif line.startswith("w"):
directions.append("w")
line = line[1:]
elif line.startswith("nw"):
directions.append("nw")
line = line[2:]
elif line.startswith("ne"):
directions.append("ne")
line = line[2:]
arr.append(directions)
return arr
test_input = """sesenwnenenewseeswwswswwnenewsewsw
neeenesenwnwwswnenewnwwsewnenwseswesw
seswneswswsenwwnwse
nwnwneseeswswnenewneswwnewseswneseene
swweswneswnenwsewnwneneseenw
eesenwseswswnenwswnwnwsewwnwsene
sewnenenenesenwsewnenwwwse
wenwwweseeeweswwwnwwe
wsweesenenewnwwnwsenewsenwwsesesenwne
neeswseenwwswnwswswnw
nenwswwsewswnenenewsenwsenwnesesenew
enewnwewneswsewnwswenweswnenwsenwsw
sweneswneswneneenwnewenewwneswswnese
swwesenesewenwneswnwwneseswwne
enesenwswwswneneswsenwnewswseenwsese
wnwnesenesenenwwnenwsewesewsesesew
nenewswnwewswnenesenwnesewesw
eneswnwswnwsenenwnwnwwseeswneewsenese
neswnwewnwnwseenwseesewsenwsweewe
wseweeenwnesenwwwswnew"""
if __name__ == '__main__':
puzzle = Puzzle(year=2020, day=24)
input = puzzle.input_data
if False:
input = test_input
# solve_puzzle_one(parse_input(input.strip()))
solve_puzzle_two(parse_input(input.strip()))