From 0f36a9833115e876bdba28e371ed9fe397a33d64 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 20 May 2025 00:51:34 +0100 Subject: [PATCH] 2016: d22: ex1: add solution --- 2016/d22/ex1/ex1.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 2016/d22/ex1/ex1.py diff --git a/2016/d22/ex1/ex1.py b/2016/d22/ex1/ex1.py new file mode 100755 index 0000000..4e9adc9 --- /dev/null +++ b/2016/d22/ex1/ex1.py @@ -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()