2020 - Day 24 (Lobby Layout)
View puzzle description on AdventOfCode.com
Part 1
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/env python3
#Advent of Code
#2020 Day 24, Part 1
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
from collections import defaultdict
import re
def solve(puzzle_input):
direction = re.compile(r"e|se|sw|w|nw|ne")
floor = defaultdict(bool)
def follow_steps(steps):
x = 0.0
y = 0
for step in steps:
if step == "e": x += 1.0
elif step == "se": x, y = x + 0.5, y + 1
elif step == "sw": x, y = x - 0.5, y + 1
elif step == "w": x -= 1.0
elif step == "nw": x, y = x - 0.5, y - 1
elif step == "ne": x, y = x + 0.5, y - 1
return (x, y)
for line in puzzle_input:
steps = re.findall(direction, line)
coords = follow_steps(steps)
floor[coords] = not floor[coords]
return sum(floor.values())
def main():
puzzle_input = util.read.as_lines()
tiles = solve(puzzle_input)
print("The number of tiles left with the black side up is " + str(tiles) + ".")
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
return self.assertEqual(solve(["sesenwnenenewseeswwswswwnenewsewsw",
"neeenesenwnwwswnenewnwwsewnenwseswesw",
"seswneswswsenwwnwse",
"nwnwneseeswswnenewneswwnewseswneseene",
"swweswneswnenwsewnwneneseenw",
"eesenwseswswnenwswnwnwsewwnwsene",
"sewnenenenesenwsewnenwwwse",
"wenwwweseeeweswwwnwwe",
"wsweesenenewnwwnwsenewsenwwsesesenwne",
"neeswseenwwswnwswswnw",
"nenwswwsewswnenenewsenwsenwnesesenew",
"enewnwewneswsewnwswenweswnenwsenwsw",
"sweneswneswneneenwnewenewwneswswnese",
"swwesenesewenwneswnwwneseswwne",
"enesenwswwswneneswsenwnewswseenwsese",
"wnwnesenesenenwwnenwsewesewsesesew",
"nenewswnwewswnenesenwnesewesw",
"eneswnwswnwsenenwnwnwwseeswneewsenese",
"neswnwewnwnwseenwseesewsenwsweewe",
"wseweeenwnesenwwwswnew"]), 10)
if __name__ == "__main__":
run(main)
Part 2
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
#!/usr/bin/env python3
#Advent of Code
#2020 Day 24, Part 2
#Solution by James C. (https://github.com/JamesMCo)
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
from collections import defaultdict
from functools import cache
import math
import re
def solve(puzzle_input):
direction = re.compile(r"e|se|sw|w|nw|ne")
floor = defaultdict(bool)
def follow_steps(steps):
x = 0.0
y = 0
for step in steps:
if step == "e": x += 1.0
elif step == "se": x, y = x + 0.5, y + 1
elif step == "sw": x, y = x - 0.5, y + 1
elif step == "w": x -= 1.0
elif step == "nw": x, y = x - 0.5, y - 1
elif step == "ne": x, y = x + 0.5, y - 1
return (x, y)
floor["min_x"] = floor["min_y"] = None
floor["max_x"] = floor["max_y"] = None
for line in puzzle_input:
steps = re.findall(direction, line)
coords = follow_steps(steps)
floor[coords] = not floor[coords]
if floor["min_x"] == None: floor["min_x"] = coords[0]
else: floor["min_x"] = min(math.floor(floor["min_x"]), coords[0])
if floor["max_x"] == None: floor["max_x"] = coords[0]
else: floor["max_x"] = max(math.ceil(floor["max_x"]), coords[0])
if floor["min_y"] == None: floor["min_y"] = coords[1]
else: floor["min_y"] = min(math.floor(floor["min_y"]), coords[1])
if floor["max_y"] == None: floor["max_y"] = coords[1]
else: floor["max_y"] = max(math.ceil(floor["max_y"]), coords[1])
@cache
def neighbours(x, y):
return [(x + dx, y + dy) for dx, dy in [(1.0, 0), (0.5, 1), (-0.5, 1), (-1.0, 0), (-0.5, -1), (0.5, -1)]]
def day(prev_floor):
new_floor = defaultdict(bool)
for y in range(prev_floor["min_y"] - 1, prev_floor["max_y"] + 2):
for x in range(prev_floor["min_x"] - 1, prev_floor["max_x"] + 2):
if y % 2 == 1: x = x - 0.5
else: x = float(x)
n = sum(prev_floor[(nx, ny)] for nx, ny in neighbours(x, y))
if prev_floor[(x, y)] and (n == 0 or n > 2):
new_floor[(x, y)] = False
elif (not prev_floor[(x, y)]) and n == 2:
new_floor[(x, y)] = True
else:
new_floor[(x, y)] = prev_floor[(x, y)]
new_floor["min_x"] = prev_floor["min_x"] - 1
new_floor["max_x"] = prev_floor["max_x"] + 1
new_floor["min_y"] = prev_floor["min_y"] - 1
new_floor["max_y"] = prev_floor["max_y"] + 1
return new_floor
for d in range(100):
floor = day(floor)
return sum(floor.values())
def main():
puzzle_input = util.read.as_lines()
tiles = solve(puzzle_input)
print("The number of tiles left with the black side up after 100 days is " + str(tiles) + ".")
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
return self.assertEqual(solve(["sesenwnenenewseeswwswswwnenewsewsw",
"neeenesenwnwwswnenewnwwsewnenwseswesw",
"seswneswswsenwwnwse",
"nwnwneseeswswnenewneswwnewseswneseene",
"swweswneswnenwsewnwneneseenw",
"eesenwseswswnenwswnwnwsewwnwsene",
"sewnenenenesenwsewnenwwwse",
"wenwwweseeeweswwwnwwe",
"wsweesenenewnwwnwsenewsenwwsesesenwne",
"neeswseenwwswnwswswnw",
"nenwswwsewswnenenewsenwsenwnesesenew",
"enewnwewneswsewnwswenweswnenwsenwsw",
"sweneswneswneneenwnewenewwneswswnese",
"swwesenesewenwneswnwwneseswwne",
"enesenwswwswneneswsenwnewswseenwsese",
"wnwnesenesenenwwnenwsewesewsesesew",
"nenewswnwewswnenesenwnesewesw",
"eneswnwswnwsenenwnwnwwseeswneewsenese",
"neswnwewnwnwseenwseesewsenwsweewe",
"wseweeenwnesenwwwswnew"]), 2208)
if __name__ == "__main__":
run(main)