Skip to content

Commit

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


class Arcade:
def __init__(self, use_curses: bool):
self.cpu = IntCode(aoc.read(), input=self.controls, output=self.draw)

self.use_curses = use_curses
if use_curses:
self._init_curses()
self.reset()

def _init_curses(self):
self.screen = curses.initscr()
curses.cbreak()
curses.noecho()
curses.curs_set(0)
self.screen.keypad(True)

def reset(self):
self.cpu.reset()
self.grid = Grid(' ')
self.input_data = []
self.score = 0
self.playing = False

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

def draw(self, value: int):
self.input_data.append(value)
if len(self.input_data) != 3:
return

if self.input_data[0] != -1:
self.draw_tile(tuple(self.input_data))
else:
self.score = self.input_data[2]
self.draw_score()
self.input_data.clear()

def draw_tile(self, instr: tuple):
x, y, tile_idx = instr
self.grid.expand_for(Vec2(x, y), 0)
tile = ' #=-o'[tile_idx]
self.grid[(x, y)] = tile

if not self.use_curses:
return

self.screen.addch(y, x, ord(tile))

if self.playing:
self.screen.refresh()
time.sleep(8 / 1000)
elif (y, x) == (self.grid.width() - 1, self.grid.height() - 1):
self.screen.refresh()

def draw_score(self):
if not self.use_curses:
return

self.screen.addstr(0, 13, f"[ Score: {self.score:5} ]")

def controls(self) -> int:
if not self.playing:
self.playing = True

ball = Vec2(self.grid.find('o'))
paddle = Vec2(self.grid.find('-'))
if paddle.x < ball.x:
return 1
elif paddle.x > ball.x:
return -1
return 0


def main():
arcade = Arcade(use_curses=False)

arcade.process()
p1 = sum(1 for v in arcade.grid.find_all('='))

arcade.reset()
arcade.cpu.memory[0] = 2

arcade.process()
if arcade.use_curses:
curses.endwin()

print('p1:', p1)
print('p2:', arcade.score)


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 - 24
2019 - 26
</h1>
<a href="2019/1/script.py">
<img src=".aoc_tiles/tiles/2019/01.png" width="161px">
Expand Down Expand Up @@ -167,6 +167,9 @@ My solutions to the yearly Advents of Code
<a href="2019/12/script.py">
<img src=".aoc_tiles/tiles/2019/12.png" width="161px">
</a>
<a href="2019/13/script.py">
<img src=".aoc_tiles/tiles/2019/13.png" width="161px">
</a>
<h1 align="center">
2015 - 50 ⭐
</h1>
Expand Down
9 changes: 9 additions & 0 deletions utils/GhostyUtils/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ def find_all(self, target: str) -> Iterable[tuple]:
if element == target:
yield (x, y)

def expand_for(self, pos: Vec2, fill: str = None):
if pos.x >= self._width:
for row in self.grid:
row.extend(fill for _ in range(pos.x + 1 - self._width))
self._width = pos.x + 1
if pos.y >= self._height:
self.grid.extend([fill for _ in range(self._width)] for _ in range(pos.y + 1 - self._height))
self._height = pos.y + 1

def by_rows(self, *, reverse: bool = False) -> Iterable[Sequence]:
for row in (self.grid if not reverse else reversed(self.grid)):
yield row
Expand Down

0 comments on commit 534c6a0

Please sign in to comment.