day22
This commit is contained in:
parent
ecc905545b
commit
015a2cd884
92
day22.py
Normal file
92
day22.py
Normal file
@ -0,0 +1,92 @@
|
||||
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)
|
||||
1056
inputs/input22
Normal file
1056
inputs/input22
Normal file
File diff suppressed because it is too large
Load Diff
11
inputs/input22_test
Normal file
11
inputs/input22_test
Normal file
@ -0,0 +1,11 @@
|
||||
root@ebhq-gridcenter# df -h
|
||||
Filesystem Size Used Avail Use%
|
||||
/dev/grid/node-x0-y0 10T 8T 2T 80%
|
||||
/dev/grid/node-x0-y1 11T 6T 5T 54%
|
||||
/dev/grid/node-x0-y2 32T 28T 4T 87%
|
||||
/dev/grid/node-x1-y0 9T 7T 2T 77%
|
||||
/dev/grid/node-x1-y1 8T 0T 8T 0%
|
||||
/dev/grid/node-x1-y2 11T 7T 4T 63%
|
||||
/dev/grid/node-x2-y0 10T 6T 4T 60%
|
||||
/dev/grid/node-x2-y1 9T 8T 1T 88%
|
||||
/dev/grid/node-x2-y2 9T 6T 3T 66%
|
||||
Loading…
Reference in New Issue
Block a user