Created
December 14, 2022 17:47
-
-
Save jsbueno/3316d89751df3f022bbe9e365cd3a9ef to your computer and use it in GitHub Desktop.
Advent of code 2022 - day 14
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
""" | |
SPOILER ALERT! | |
Problem at: https://adventofcode.com/2022/day/14 | |
I guess, no special comments this time. | |
Just that terminedia.V2 is truly great! | |
""" | |
import terminedia as TM | |
import time | |
class Cave: | |
skip_rock = 1 | |
def __init__(self, data, floor = False): | |
self.floor = floor | |
self.data = {} | |
self.parse(data) | |
self.counter = 0 | |
def parse(self, data): | |
self.offsetx = 500 | |
self.width = 1 | |
lines = data.splitlines() | |
minx = maxx = 500 | |
maxy = 0 | |
for line in lines: | |
coords = line.split(" -> ") | |
path = [TM.V2(int(p) for p in coord.split(",")) for coord in coords] | |
prev = None | |
for coord in path: | |
minx = min(coord.x, minx) | |
maxx = max(coord.x, maxx) | |
maxy = max(coord.y, maxy) | |
if prev is None: | |
prev = coord | |
continue | |
if prev.x == coord.x: | |
origin, dest = sorted([prev, coord], key=lambda c: c.y) | |
for y in range(origin.y, dest.y + 1): | |
self[origin.x, y] = "#" | |
else: | |
origin, dest = sorted([prev, coord], key=lambda c: c.x) | |
for x in range(origin.x, dest.x + 1): | |
self[x, origin.y] = "#" | |
prev = coord | |
#self.offsetx = minx - 1 | |
#self.width = maxx - minx + 3 | |
self.height = maxy + 2 | |
def __setitem__(self, pos, v): | |
self.offsetx = min(self.offsetx, pos[0] - 1) | |
self.width = max(self.width, pos[0] - self.offsetx + 3) | |
self.data[pos] = v | |
def __getitem__(self, pos): | |
if not self.floor and (not (self.offsetx <= pos[0] < self.offsetx + self.width) or not (0 <= pos[1] < self.height)): | |
raise KeyError(f"{pos} is outside measured boundarires") | |
if self.floor and pos[1] >= self.height: | |
return "#" | |
return self.data.get(pos, ".") | |
def get(self, pos, sentinel=None): | |
try: | |
return self[pos] | |
except KeyError: | |
return sentinel | |
def __delitem__(self, pos): | |
try: | |
del self.data[pos] | |
except KeyError: | |
pass | |
def __iter__(self): | |
for y in range(self.height + self.floor): | |
for x in range(self.offsetx, self.offsetx + self.width): | |
yield TM.V2(x, y) | |
def __repr__(self): | |
output = [] | |
prev = next(iter(self)) | |
line = "" | |
for pos in self: | |
if prev.y != pos.y: | |
output.append(line) | |
line = "" | |
line += self[pos] | |
prev = pos | |
if len(output) < 90: | |
return "\n".join(output) + "\n" | |
realout = [] | |
offset = len(output) // 2 | |
for i in range(offset): | |
realout.append(output[i] + " " + output[i + offset]) | |
if len(output) % 2: | |
realout.append(" " * (len(line) + 5) + output[-1]) | |
return "\n".join(realout) + "\n" | |
def display_hook(self, pos): | |
if self.counter % self.skip_rock: | |
return | |
try: | |
orig = self[pos] | |
self[pos] = "*" | |
output = repr(self) | |
TM.cls() | |
print(output) | |
finally: | |
if orig != ".": | |
self[pos] = orig | |
else: | |
del self[pos] | |
time.sleep(getattr(self, "fps", 0.03)) | |
def clear(self): | |
#not actually needed by the problem _at_all_ | |
for k, v in list(self.data.items()): | |
if v != "#": | |
del self.data[k] | |
def run_rock(self, pos): | |
pos = TM.V2(pos) | |
if self[pos] != ".": | |
raise ValueError("Can't have another rock at {pos}") | |
self.display_hook(pos) | |
try: | |
for delta in [(0, 1), (-1, 1), (1, 1)]: | |
if self[next_:=pos + delta] == ".": | |
return self.run_rock(next_) | |
self[pos] = "o" | |
return True | |
except KeyError: | |
return False | |
return True | |
def do_it(self): | |
self.counter = 0 | |
try: | |
while self.run_rock((500,0)): | |
self.counter += 1 | |
except ValueError: | |
pass | |
return self.counter |
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
....................................................... ..................#ooooo#.............................. | |
....................................................... ..................#ooo#o#.............................. | |
....................................................... ..................#ooo#o#o............................. | |
....................................................... ..................#ooo#o#oo............................ | |
....................................................... ..................#ooo#o#ooo........................... | |
....................................................... ..................#ooo#o#oooo.......................... | |
....................................................... ..................#o#o#o#ooooo......................... | |
....................................................... ..................#o#o#o#oooooo........................ | |
.................oo.................................... ..................#o#o#o#ooooooo....................... | |
................oooo................................... ..................#######oooooooo...................... | |
...............oooooo.................................. ........................oooooooooo..................... | |
..............oooooooo................................. .......................oooooooooooo.................... | |
.............oooooooooo................................ ......................######oooooooo................... | |
............ooooooooooo#............................... ...........................oooooooooo.................. | |
...........#############............................... ...................######.######oooooo................. | |
.......................oo.............................. ...............................oooooooo................ | |
......................oooo............................. ................######.######.######oooo............... | |
.....................######............................ ...................................oooooo.............. | |
...........................o........................... .............######.######.######.######oo............. | |
..........................ooo.......................... .......................................oooo............ | |
..................######.######........................ ..........######.######.######.######.######........... | |
........................o.............................. ............................................o.......... | |
.......................ooo............................. ...........................................ooo......... | |
...............######.######.######.................... ..........................................#ooo#........ | |
.....................o................................. ..........................................#ooo#........ | |
....................ooo................................ ..........................................#ooo#........ | |
...................ooooo............................... .........................................o#ooo#........ | |
..................ooooooo.............................. ........................................###ooo#######.. | |
.................#oooooooo.....#....................... ........................................#.ooooo.....#.. | |
................o###############....................... ........................................#ooooooo....#.. | |
...............ooo..................................... ........................................#oooooooo...#.. | |
..............ooooo.................................... ........................................#ooooooooo..#.. | |
.............######o................................... .......................................o#oooooooooo.#.. | |
............o.....ooo.................................. ......................................oo#ooooooooooo#.. | |
...........ooo...ooooo................................. .....................................ooo#############.. | |
..........######o######................................ ....................................ooooo.............. | |
.........o.....ooo..................................... ...................................ooooooo............. | |
........ooo...ooooo.................................... ..................................ooo#####o............ | |
.......######o######.######............................ .................................ooooo...ooo........... | |
............ooo........................................ ................................oo#####.#####.......... | |
...........ooooo....................................... ...............................oooo.................... | |
..........ooooooo.....#................................ ..............................o#####.#####.#####....... | |
........###############................................ .............................ooo....................... | |
.....oo................................................ ............................#####.#####.#####.#####.... | |
....oooo............................................... ....................................................... | |
...#ooo#o.............................................. ....................................................... | |
...#ooo#oo............................................. ........................#....#......................... | |
.###ooo####............................................ ........................#....#......................... | |
.#.ooooo..#............................................ ....................#####....######.................... | |
.#ooooooo.#............................................ ....................#.............#.................... | |
.#oooooooo#............................................ ....................#.............#.................... | |
.#oooooooo#............................................ ....................#......*......#.................... | |
.#oooooooo#............................................ ....................#.....ooo.....#.................... | |
.#oooooooo#............................................ ....................###############.................... | |
.##########............................................ ....................................................... | |
....................................................... ....................................................... | |
....................................................... ................#....#................................. | |
....................................................... ................#....#................................. | |
.......#............................................... ...........######....##................................ | |
.......#..o#........................................... ...........#..........#................................ | |
.......#.oo#........................................... ...........#..........#................................ | |
.......#o#o#o.......................................... ...........#..........#................................ | |
.......#o#o#oo......................................... ...........#..........#................................ | |
.......#o#o#ooo........................................ ...........#..........#................................ | |
.......#o#o#o#oo....................................... ...........#..........#................................ | |
.......#o#o#o#ooo...................................... ...........#..........#................................ | |
.......#o#o#o#oooo..................................... ...........############................................ | |
.......#######ooooo.................................... ....................................................... | |
.............ooooooo................................... ....................................................... | |
............ooooooooo.................................. ....................................................... | |
...........oooooo#ooo#................................. ...................#................................... | |
..........o#ooooo#ooo#................................. ...................#######............................. | |
.......#.oo#ooooo#ooo#................................. ....................................................... | |
.......#ooo#ooooo#ooo#................................. ....................................................... | |
.......#ooo#ooooo#ooo#................................. ................#...................................... | |
.....#.#ooo#ooo#o#ooo#................................. ................#.......#.............................. | |
.....#.#o#o#o#o#o#ooo#................................. ................#.....#.#.............................. | |
.....#.#o#o#o#o#o#ooo#................................. ................#.....#.#.......#...................... | |
.....#.#o#o#o#o#o#o#o#................................. ................#.#...#.#.......#...................... | |
.....#.#o#o#o#o#o#o#o#................................. ................#.#.#.#.#.#.#...#...................... | |
.....#################................................. ................#.#.#.#.#.#.#...#...................... | |
.....................oo................................ ................#.#.#.#.#.#.#...#...................... | |
....................oooo............................... ................#.#.#.#.#.#.#...#...................... | |
..................#ooooo#.............................. ................#.#.#.#.#.#.#.#.#.#.................... | |
................###################.................... | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment