-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9f8a11
commit a8d07c9
Showing
4 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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^<<^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters