93 lines
2.1 KiB
Python
93 lines
2.1 KiB
Python
from tools.aoc import AOCDay
|
|
from tools.coordinate import Coordinate
|
|
from tools.grid import Grid
|
|
from typing import Any
|
|
|
|
|
|
class Node:
|
|
def __init__(self, size: int, used: int):
|
|
self.size = size
|
|
self.used = used
|
|
|
|
@property
|
|
def avail(self) -> int:
|
|
return self.size - self.used
|
|
|
|
|
|
class Day(AOCDay):
|
|
inputs = [
|
|
[
|
|
(1024, "input22"),
|
|
],
|
|
[
|
|
(230, "input22"),
|
|
],
|
|
]
|
|
|
|
def parse_input(self) -> Grid:
|
|
grid = Grid(default=None)
|
|
for line in self.getInput():
|
|
if not line.startswith("/dev/grid"):
|
|
continue
|
|
|
|
node_path, size, used, avail, use_perc = line.split()
|
|
node_name = node_path.split("/")[-1]
|
|
str_x, str_y = node_name.split("-")[-2:]
|
|
grid.set(Coordinate(int(str_x[1:]), int(str_y[1:])), Node(int(size[:-1]), int(used[:-1])))
|
|
|
|
return grid
|
|
|
|
def part1(self) -> Any:
|
|
grid = self.parse_input()
|
|
ans = 0
|
|
|
|
for c1 in grid.getActiveCells():
|
|
for c2 in grid.getActiveCells():
|
|
if c1 == c2 or grid.get(c1).used == 0:
|
|
continue
|
|
|
|
if grid.get(c1).used <= grid.get(c2).avail:
|
|
ans += 1
|
|
|
|
return ans
|
|
|
|
def part2(self) -> Any:
|
|
grid = self.parse_input()
|
|
moves = 0
|
|
|
|
pos = None
|
|
for c in grid.getActiveCells():
|
|
if grid.get(c).used == 0:
|
|
pos = c
|
|
break
|
|
|
|
# move to the "wall"
|
|
while grid.get(pos + (0, -1)).used < 100:
|
|
pos += (0, -1)
|
|
moves += 1
|
|
|
|
# move left to get around the "wall"
|
|
while grid.get(pos + (0, -1)).used > 100:
|
|
pos += (-1, 0)
|
|
moves += 1
|
|
|
|
# move up to top
|
|
moves += pos.y
|
|
pos += (0, -pos.y)
|
|
|
|
# move to the right
|
|
moves += grid.maxX - pos.x - 1
|
|
|
|
# move data to the left
|
|
moves += (grid.maxX - 1) * 5
|
|
|
|
# last step
|
|
moves += 1
|
|
|
|
return moves
|
|
|
|
|
|
if __name__ == "__main__":
|
|
day = Day(2016, 22)
|
|
day.run(verbose=True)
|