Skip to content

Commit

Permalink
[2024/15] p1 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 15, 2024
1 parent a9f8a11 commit a8d07c9
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
Binary file added .aoc_tiles/tiles/2024/15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions 2024/15/example
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
##########
#..O..O.O#
#......O.#
#.OO..O.O#
#..O@..O.#
#O#..O...#
#O..O..O.#
#.OO.O.OO#
#....O...#
##########

<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^
91 changes: 91 additions & 0 deletions 2024/15/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from GhostyUtils import aoc
from GhostyUtils.grid import Grid
from GhostyUtils.vec2 import Vec2, Dir


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

def move(self, dir: Dir, grid: Grid) -> bool:
if grid[self.pos + dir].move(dir, grid):
grid[self.pos] = Air(self.pos)
self.pos += dir
grid[self.pos] = self
return True
return False

def __str__(self) -> str:
return '@'


class Box:
def __init__(self, pos: Vec2) -> 'Box':
self.pos = Vec2(pos)

def move(self, dir: Dir, grid: Grid) -> bool:
if grid[self.pos + dir].move(dir, grid):
grid[self.pos] = Air(self.pos)
self.pos += dir
grid[self.pos] = self
return True
return False

def __str__(self) -> str:
return 'O'


class Wall:
def __init__(self, pos: Vec2) -> 'Wall':
self.pos = Vec2(pos)

def move(self, dir: Dir, grid: Grid) -> bool:
return False

def __str__(self) -> str:
return '#'


class Air:
def __init__(self, pos: Vec2) -> 'Air':
pass

def move(self, dir: Dir, grid: Grid) -> bool:
return True

def __str__(self) -> str:
return '.'


def convert(cell: str, pos: Vec2) -> Robot | Box | Wall | Air:
return {'@': Robot, '#': Wall, 'O': Box, '.': Air}[cell](pos)


def main():
warehouse, instructions = aoc.read_sections()
warehouse = Grid(warehouse.splitlines())
robot = None
boxes = []
for cell, pos in warehouse.by_cell():
warehouse[pos] = convert(cell, pos)
if type(warehouse[pos]) is Box:
boxes.append(warehouse[pos])
elif type(warehouse[pos]) is Robot:
robot = warehouse[pos]

if aoc.args.verbose or aoc.args.progress:
print(warehouse)

for instr in instructions:
if instr == '\n':
continue
robot.move(Dir.map_nswe('^v<>')[instr], warehouse)

if aoc.args.verbose or aoc.args.progress:
print(warehouse)

print(f"p1: {sum(box.pos.y * 100 + box.pos.x for box in boxes)}")


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 - 189/478
Advent of Code - 190/480
</h1>
<h1 align="center">
2024 - 28 ⭐ - Python
2024 - 29 ⭐ - Python
</h1>
<a href="2024/1/script.py">
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
Expand Down Expand Up @@ -50,6 +50,9 @@ My solutions to the yearly Advents of Code
<a href="2024/14/script.py">
<img src=".aoc_tiles/tiles/2024/14.png" width="161px">
</a>
<a href="2024/15/script.py">
<img src=".aoc_tiles/tiles/2024/15.png" width="161px">
</a>
<h1 align="center">
2023 - 47 ⭐ - Python
</h1>
Expand Down

0 comments on commit a8d07c9

Please sign in to comment.