2016: d22: ex1: add solution
This commit is contained in:
parent
a845a4012a
commit
0f36a98331
1 changed files with 49 additions and 0 deletions
49
2016/d22/ex1/ex1.py
Executable file
49
2016/d22/ex1/ex1.py
Executable file
|
|
@ -0,0 +1,49 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import itertools
|
||||
import sys
|
||||
from typing import NamedTuple
|
||||
|
||||
|
||||
class Point(NamedTuple):
|
||||
x: int
|
||||
y: int
|
||||
|
||||
|
||||
class Filesystem(NamedTuple):
|
||||
size: int
|
||||
used: int
|
||||
|
||||
@property
|
||||
def avail(self) -> int:
|
||||
return self.size - self.used
|
||||
|
||||
|
||||
def solve(input: str) -> int:
|
||||
def parse_line(input: str) -> tuple[Point, Filesystem]:
|
||||
raw_fs, raw_size, raw_used, raw_avail, _ = input.split()
|
||||
size = int(raw_size.removesuffix("T"))
|
||||
used = int(raw_used.removesuffix("T"))
|
||||
avail = int(raw_avail.removesuffix("T"))
|
||||
assert size == (used + avail) # Sanity check
|
||||
*_, x, y = raw_fs.split("-")
|
||||
return Point(int(x[1:]), int(y[1:])), Filesystem(size, used)
|
||||
|
||||
def parse(input: str) -> dict[Point, Filesystem]:
|
||||
return {node: fs for node, fs in map(parse_line, input.splitlines()[2:])}
|
||||
|
||||
df = parse(input)
|
||||
return sum(
|
||||
a.used <= b.avail
|
||||
for a, b in itertools.permutations(df.values(), 2)
|
||||
if a.used > 0
|
||||
)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
input = sys.stdin.read()
|
||||
print(solve(input))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue