Skip to content

Commit

Permalink
[2019/11] solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 27, 2023
1 parent e73b11d commit 205dd7e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
Binary file added .aoc_tiles/tiles/2019/11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 71 additions & 0 deletions 2019/11/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from GhostyUtils import aoc
from GhostyUtils.intcode.cpu import IntCode
from GhostyUtils.grid import Grid
from GhostyUtils.vec2 import Vec2, Dir


class Robot:
def __init__(self, pos: Vec2, dir: Dir, grid: dict):
self.pos = pos
self.dir = dir
self.next = 0
self.grid = grid
self.cpu = IntCode(aoc.read(), input=self.camera, output=self.control)

def run(self):
self.cpu.process()

def camera(self):
if tuple(self.pos) in self.grid:
return self.grid[tuple(self.pos)]
return 0

def control(self, output: int):
if self.next == 0:
self.paint(output)
elif self.next == 1:
self.turn(output)
self.move()
self.next = (self.next + 1) % 2

def paint(self, color: int):
self.grid[tuple(self.pos)] = color

def turn(self, lr: int):
turns = [Dir.UP, Dir.RIGHT, Dir.DOWN, Dir.LEFT]
cur_idx = turns.index(self.dir)
turn_idx = (cur_idx+(1 if lr == 1 else -1)) % len(turns)
self.dir = turns[turn_idx]

def move(self):
self.pos += self.dir


def make_grid(grid: dict) -> Grid:
tl = Vec2(min(grid.keys(), key=lambda pos: pos[0])[0],
min(grid.keys(), key=lambda pos: pos[1])[1])
br = Vec2(max(grid.keys(), key=lambda pos: pos[0])[0],
max(grid.keys(), key=lambda pos: pos[1])[1])
width = br.x - tl.x + 1
height = br.y - tl.y + 1
new_grid = Grid(' ' * width for _ in range(height))
for coord, paint in grid.items():
new_grid[tuple(coord - tl)] = '#' if paint else ' '
return new_grid


def main():
grid = {}
robot = Robot(Vec2(0, 0), Dir.UP, grid)
robot.run()
print('p1:', len(grid))
# print(make_grid(grid))

grid = {(0, 0): 1}
robot = Robot(Vec2(0, 0), Dir.UP, grid)
robot.run()
print(make_grid(grid))


if __name__ == "__main__":
main()
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ My solutions to the yearly Advents of Code
<img src=".aoc_tiles/tiles/2022/15.png" width="161px">
</a>
<h1 align="center">
2019 - 20
2019 - 22
</h1>
<a href="2019/1/script.py">
<img src=".aoc_tiles/tiles/2019/01.png" width="161px">
Expand Down Expand Up @@ -161,6 +161,9 @@ My solutions to the yearly Advents of Code
<a href="2019/10/script.py">
<img src=".aoc_tiles/tiles/2019/10.png" width="161px">
</a>
<a href="2019/11/script.py">
<img src=".aoc_tiles/tiles/2019/11.png" width="161px">
</a>
<h1 align="center">
2015 - 50 ⭐
</h1>
Expand Down

0 comments on commit 205dd7e

Please sign in to comment.