2024 - Day 14 (Restroom Redoubt)
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
#!/usr/bin/env python3
#Advent of Code
#2024 Day 14, 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
import re
def solve(puzzle_input: list[str], width: int = 101, height: int = 103) -> int:
robot_pattern: re.Pattern = re.compile(r"p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)")
x_middle: int = int((width - 1) / 2)
y_middle: int = int((height - 1) / 2)
top_left_count: int = 0
top_right_count: int = 0
bot_left_count: int = 0
bot_right_count: int = 0
for px, py, vx, vy in map(lambda robot: map(int, re.fullmatch(robot_pattern, robot).groups()), puzzle_input):
final_x: int = (px + (vx * 100)) % width
final_y: int = (py + (vy * 100)) % height
if final_x < x_middle and final_y < y_middle:
top_left_count += 1
elif final_x > x_middle and final_y < y_middle:
top_right_count += 1
elif final_x < x_middle and final_y > y_middle:
bot_left_count += 1
elif final_x > x_middle and final_y > y_middle:
bot_right_count += 1
return top_left_count * top_right_count * bot_left_count * bot_right_count
def main() -> tuple[str, int]:
puzzle_input = util.read.as_lines()
return "The safety factor after 100 seconds is {}.", solve(puzzle_input)
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
return self.assertEqual(solve(["p=0,4 v=3,-3",
"p=6,3 v=-1,-3",
"p=10,3 v=-1,2",
"p=2,0 v=2,-1",
"p=0,0 v=1,3",
"p=3,0 v=-2,-2",
"p=7,6 v=-1,-3",
"p=3,0 v=-1,-2",
"p=9,3 v=2,3",
"p=7,3 v=-1,2",
"p=2,4 v=2,-3",
"p=9,5 v=-3,-3"], 11, 7), 12)
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
#!/usr/bin/env python3
#Advent of Code
#2024 Day 14, 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
import os
import re
from typing import Self
def solve(puzzle_input: list[str], width: int = 101, height: int = 103) -> int:
class Robot:
robot_pattern: re.Pattern = re.compile(r"p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)")
px: int
py: int
vx: int
vy: int
def __init__(self: Self, description: str) -> None:
self.px, self.py, self.vx, self.vy = map(int, re.fullmatch(Robot.robot_pattern, description).groups())
def move(self: Self) -> None:
self.px = (self.px + self.vx) % width
self.py = (self.py + self.vy) % height
robots: list[Robot] = list(map(Robot, puzzle_input))
seconds: int = 0
# Solution determined after stepping through outputs one at a time and some research on the subreddit
#
# This isn't a very rigorous approach, but I found that the number of unique positions (individually in x and y)
# normally hovered around 100, and dropped to < 90 periodically. Therefore, I assumed that when both x and y dropped
# at the same time, we probably had found the Easter egg.
while True:
robot_locs: set[tuple[int, int]] = {(robot.px, robot.py) for robot in robots}
unique_x: int = len(set(robot[0] for robot in robot_locs))
unique_y: int = len(set(robot[1] for robot in robot_locs))
if unique_x < 90 and unique_y < 90:
return seconds
for robot in robots:
robot.move()
seconds += 1
def main() -> tuple[str, int]:
puzzle_input = util.read.as_lines()
return "The fewest number of seconds for the robots to display the Easter egg is {}.", solve(puzzle_input)
class AOC_Tests(unittest.TestCase):
@unittest.skip("No test cases")
def test_ex1(self):
pass
if __name__ == "__main__":
run(main)