diff --git a/.aoc_tiles/tiles/2024/15.png b/.aoc_tiles/tiles/2024/15.png new file mode 100644 index 0000000..728586e Binary files /dev/null and b/.aoc_tiles/tiles/2024/15.png differ diff --git a/2024/15/example b/2024/15/example new file mode 100644 index 0000000..84cf1fb --- /dev/null +++ b/2024/15/example @@ -0,0 +1,21 @@ +########## +#..O..O.O# +#......O.# +#.OO..O.O# +#..O@..O.# +#O#..O...# +#O..O..O.# +#.OO.O.OO# +#....O...# +########## + +^v>^vv^v>v<>v^v<<><>>v^v^>^<<<><^ +vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<^<^^>>>^<>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^v^^<^^vv< +<>^^^^>>>v^<>vvv^>^^^vv^^>v<^^^^v<>^>vvvv><>>v^<<^^^^^ +^><^><>>><>^^<<^^v>>><^^>v>>>^v><>^v><<<>vvvv>^<><<>^>< +^>><>^v<><^vvv<^^<><^v<<<><<<^^<^>>^<<<^>>^v^>>^v>vv>^<<^v<>><<><<>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^ +<><^^>^^^<>^vv<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<> +^^>vv<^v^v^<>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<>< +v^^>>><<^^<>>^v^v^<<>^<^v^v><^<<<><<^vv>>v>v^<<^ diff --git a/2024/15/script.py b/2024/15/script.py new file mode 100644 index 0000000..74f3400 --- /dev/null +++ b/2024/15/script.py @@ -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() diff --git a/README.md b/README.md index 56f00fa..a84999d 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,10 @@ My solutions to the yearly Advents of Code

- Advent of Code - 189/478 ⭐ + Advent of Code - 190/480 ⭐

- 2024 - 28 ⭐ - Python + 2024 - 29 ⭐ - Python

@@ -50,6 +50,9 @@ My solutions to the yearly Advents of Code + + +

2023 - 47 ⭐ - Python