Skip to content

Commit a8d07c9

Browse files
committed
[2024/15] p1 solved
1 parent a9f8a11 commit a8d07c9

File tree

4 files changed

+117
-2
lines changed

4 files changed

+117
-2
lines changed

.aoc_tiles/tiles/2024/15.png

4.7 KB
Loading

2024/15/example

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
##########
2+
#..O..O.O#
3+
#......O.#
4+
#.OO..O.O#
5+
#..O@..O.#
6+
#O#..O...#
7+
#O..O..O.#
8+
#.OO.O.OO#
9+
#....O...#
10+
##########
11+
12+
<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
13+
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
14+
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
15+
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
16+
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
17+
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
18+
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
19+
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
20+
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
21+
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^

2024/15/script.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from GhostyUtils import aoc
2+
from GhostyUtils.grid import Grid
3+
from GhostyUtils.vec2 import Vec2, Dir
4+
5+
6+
class Robot:
7+
def __init__(self, pos: Vec2) -> 'Robot':
8+
self.pos = Vec2(pos)
9+
10+
def move(self, dir: Dir, grid: Grid) -> bool:
11+
if grid[self.pos + dir].move(dir, grid):
12+
grid[self.pos] = Air(self.pos)
13+
self.pos += dir
14+
grid[self.pos] = self
15+
return True
16+
return False
17+
18+
def __str__(self) -> str:
19+
return '@'
20+
21+
22+
class Box:
23+
def __init__(self, pos: Vec2) -> 'Box':
24+
self.pos = Vec2(pos)
25+
26+
def move(self, dir: Dir, grid: Grid) -> bool:
27+
if grid[self.pos + dir].move(dir, grid):
28+
grid[self.pos] = Air(self.pos)
29+
self.pos += dir
30+
grid[self.pos] = self
31+
return True
32+
return False
33+
34+
def __str__(self) -> str:
35+
return 'O'
36+
37+
38+
class Wall:
39+
def __init__(self, pos: Vec2) -> 'Wall':
40+
self.pos = Vec2(pos)
41+
42+
def move(self, dir: Dir, grid: Grid) -> bool:
43+
return False
44+
45+
def __str__(self) -> str:
46+
return '#'
47+
48+
49+
class Air:
50+
def __init__(self, pos: Vec2) -> 'Air':
51+
pass
52+
53+
def move(self, dir: Dir, grid: Grid) -> bool:
54+
return True
55+
56+
def __str__(self) -> str:
57+
return '.'
58+
59+
60+
def convert(cell: str, pos: Vec2) -> Robot | Box | Wall | Air:
61+
return {'@': Robot, '#': Wall, 'O': Box, '.': Air}[cell](pos)
62+
63+
64+
def main():
65+
warehouse, instructions = aoc.read_sections()
66+
warehouse = Grid(warehouse.splitlines())
67+
robot = None
68+
boxes = []
69+
for cell, pos in warehouse.by_cell():
70+
warehouse[pos] = convert(cell, pos)
71+
if type(warehouse[pos]) is Box:
72+
boxes.append(warehouse[pos])
73+
elif type(warehouse[pos]) is Robot:
74+
robot = warehouse[pos]
75+
76+
if aoc.args.verbose or aoc.args.progress:
77+
print(warehouse)
78+
79+
for instr in instructions:
80+
if instr == '\n':
81+
continue
82+
robot.move(Dir.map_nswe('^v<>')[instr], warehouse)
83+
84+
if aoc.args.verbose or aoc.args.progress:
85+
print(warehouse)
86+
87+
print(f"p1: {sum(box.pos.y * 100 + box.pos.x for box in boxes)}")
88+
89+
90+
if __name__ == "__main__":
91+
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 - 189/478
6+
Advent of Code - 190/480
77
</h1>
88
<h1 align="center">
9-
2024 - 28 ⭐ - Python
9+
2024 - 29 ⭐ - Python
1010
</h1>
1111
<a href="2024/1/script.py">
1212
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
@@ -50,6 +50,9 @@ My solutions to the yearly Advents of Code
5050
<a href="2024/14/script.py">
5151
<img src=".aoc_tiles/tiles/2024/14.png" width="161px">
5252
</a>
53+
<a href="2024/15/script.py">
54+
<img src=".aoc_tiles/tiles/2024/15.png" width="161px">
55+
</a>
5356
<h1 align="center">
5457
2023 - 47 ⭐ - Python
5558
</h1>

0 commit comments

Comments
 (0)