Skip to content

Commit

Permalink
[2024/14] p1 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 14, 2024
1 parent d10f235 commit a47cef8
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
Binary file added .aoc_tiles/tiles/2024/14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions 2024/14/example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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
84 changes: 84 additions & 0 deletions 2024/14/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from GhostyUtils import aoc
from GhostyUtils.vec2 import Vec2
from GhostyUtils.grid import Grid
from collections import Counter
import math


aoc.argparser.add_argument("-d", "--dimensions", type=str, default="101,103",
help="width,height")
aoc.argparser.add_argument("-s", "--seconds", type=int, default=100)


class Rectangle:
def __init__(self, top_left: Vec2, bottom_right: Vec2) -> 'Rectangle':
self.tl = top_left
self.br = bottom_right

def contains(self, other: Vec2) -> bool:
return ((self.tl.x <= other.x < self.br.x) and
(self.tl.y <= other.y < self.br.y))


class Robot:
def __init__(self, position: Vec2, velocity: Vec2) -> 'Robot':
self.pos = position
self.v = velocity

def __str__(self) -> str:
return f"p={self.pos.x},{self.pos.y} v={self.v.x},{self.v.y}"

def step(self, grid: Grid):
self.pos += self.v
self.pos.x %= grid.width()
self.pos.y %= grid.height()


def count_quadrants(robots: list[Robot], grid: Grid) -> list[int]:
q_width = grid.width() // 2
q_height = grid.height() // 2
quadrants = [
Rectangle(Vec2(0, 0), Vec2(q_width, q_height)),
Rectangle(Vec2(q_width + 1, 0), Vec2(grid.width(), q_height)),
Rectangle(Vec2(0, q_height + 1), Vec2(q_width, grid.height())),
Rectangle(Vec2(q_width + 1, q_height + 1), Vec2(grid.width(), grid.height())),
]

count = [0]*4
for robot in robots:
for q, quad in enumerate(quadrants):
count[q] += 1 if quad.contains(robot.pos) else 0

return count


def render_robots(robots: list[Robot], grid: Grid) -> str:
return grid.render_with_overlays([Counter(robot.pos.as_tuple() for robot in robots)])


def main():
inputs = aoc.read_lines()

robots = []
for line in inputs:
robots.append(Robot(*(Vec2.from_str(vec[2:], ',') for vec in line.split())))

dimensions = Vec2.from_str(aoc.args.dimensions, ',')
grid = Grid(['.' * dimensions.x] * dimensions.y)

print("Initial state:")
print(render_robots(robots, grid))
print()
for s in range(aoc.args.seconds):
for robot in robots:
robot.step(grid)

print(f"After {s+1} second{'s' if s > 1 else ''}:")
print(render_robots(robots, grid))
print()

print(f"p1: {math.prod(count_quadrants(robots, grid))}")


if __name__ == "__main__":
main()
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code

<!-- AOC TILES BEGIN -->
<h1 align="center">
Advent of Code - 187/476
Advent of Code - 188/478
</h1>
<h1 align="center">
2024 - 26 ⭐ - Python
2024 - 27 ⭐ - Python
</h1>
<a href="2024/1/script.py">
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
Expand Down Expand Up @@ -47,6 +47,9 @@ My solutions to the yearly Advents of Code
<a href="2024/13/script.py">
<img src=".aoc_tiles/tiles/2024/13.png" width="161px">
</a>
<a href="2024/14/script.py">
<img src=".aoc_tiles/tiles/2024/14.png" width="161px">
</a>
<h1 align="center">
2023 - 47 ⭐ - Python
</h1>
Expand Down

0 comments on commit a47cef8

Please sign in to comment.