Skip to content

Commit a47cef8

Browse files
committed
[2024/14] p1 solved
1 parent d10f235 commit a47cef8

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

.aoc_tiles/tiles/2024/14.png

4.16 KB
Loading

2024/14/example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
p=0,4 v=3,-3
2+
p=6,3 v=-1,-3
3+
p=10,3 v=-1,2
4+
p=2,0 v=2,-1
5+
p=0,0 v=1,3
6+
p=3,0 v=-2,-2
7+
p=7,6 v=-1,-3
8+
p=3,0 v=-1,-2
9+
p=9,3 v=2,3
10+
p=7,3 v=-1,2
11+
p=2,4 v=2,-3
12+
p=9,5 v=-3,-3

2024/14/script.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from GhostyUtils import aoc
2+
from GhostyUtils.vec2 import Vec2
3+
from GhostyUtils.grid import Grid
4+
from collections import Counter
5+
import math
6+
7+
8+
aoc.argparser.add_argument("-d", "--dimensions", type=str, default="101,103",
9+
help="width,height")
10+
aoc.argparser.add_argument("-s", "--seconds", type=int, default=100)
11+
12+
13+
class Rectangle:
14+
def __init__(self, top_left: Vec2, bottom_right: Vec2) -> 'Rectangle':
15+
self.tl = top_left
16+
self.br = bottom_right
17+
18+
def contains(self, other: Vec2) -> bool:
19+
return ((self.tl.x <= other.x < self.br.x) and
20+
(self.tl.y <= other.y < self.br.y))
21+
22+
23+
class Robot:
24+
def __init__(self, position: Vec2, velocity: Vec2) -> 'Robot':
25+
self.pos = position
26+
self.v = velocity
27+
28+
def __str__(self) -> str:
29+
return f"p={self.pos.x},{self.pos.y} v={self.v.x},{self.v.y}"
30+
31+
def step(self, grid: Grid):
32+
self.pos += self.v
33+
self.pos.x %= grid.width()
34+
self.pos.y %= grid.height()
35+
36+
37+
def count_quadrants(robots: list[Robot], grid: Grid) -> list[int]:
38+
q_width = grid.width() // 2
39+
q_height = grid.height() // 2
40+
quadrants = [
41+
Rectangle(Vec2(0, 0), Vec2(q_width, q_height)),
42+
Rectangle(Vec2(q_width + 1, 0), Vec2(grid.width(), q_height)),
43+
Rectangle(Vec2(0, q_height + 1), Vec2(q_width, grid.height())),
44+
Rectangle(Vec2(q_width + 1, q_height + 1), Vec2(grid.width(), grid.height())),
45+
]
46+
47+
count = [0]*4
48+
for robot in robots:
49+
for q, quad in enumerate(quadrants):
50+
count[q] += 1 if quad.contains(robot.pos) else 0
51+
52+
return count
53+
54+
55+
def render_robots(robots: list[Robot], grid: Grid) -> str:
56+
return grid.render_with_overlays([Counter(robot.pos.as_tuple() for robot in robots)])
57+
58+
59+
def main():
60+
inputs = aoc.read_lines()
61+
62+
robots = []
63+
for line in inputs:
64+
robots.append(Robot(*(Vec2.from_str(vec[2:], ',') for vec in line.split())))
65+
66+
dimensions = Vec2.from_str(aoc.args.dimensions, ',')
67+
grid = Grid(['.' * dimensions.x] * dimensions.y)
68+
69+
print("Initial state:")
70+
print(render_robots(robots, grid))
71+
print()
72+
for s in range(aoc.args.seconds):
73+
for robot in robots:
74+
robot.step(grid)
75+
76+
print(f"After {s+1} second{'s' if s > 1 else ''}:")
77+
print(render_robots(robots, grid))
78+
print()
79+
80+
print(f"p1: {math.prod(count_quadrants(robots, grid))}")
81+
82+
83+
if __name__ == "__main__":
84+
main()

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code
33

44
<!-- AOC TILES BEGIN -->
55
<h1 align="center">
6-
Advent of Code - 187/476
6+
Advent of Code - 188/478
77
</h1>
88
<h1 align="center">
9-
2024 - 26 ⭐ - Python
9+
2024 - 27 ⭐ - Python
1010
</h1>
1111
<a href="2024/1/script.py">
1212
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
@@ -47,6 +47,9 @@ My solutions to the yearly Advents of Code
4747
<a href="2024/13/script.py">
4848
<img src=".aoc_tiles/tiles/2024/13.png" width="161px">
4949
</a>
50+
<a href="2024/14/script.py">
51+
<img src=".aoc_tiles/tiles/2024/14.png" width="161px">
52+
</a>
5053
<h1 align="center">
5154
2023 - 47 ⭐ - Python
5255
</h1>

0 commit comments

Comments
 (0)