From 6b733982f9f240c1c97f1fa705bfbe4cd93c640e Mon Sep 17 00:00:00 2001 From: mhsn Date: Fri, 12 Sep 2025 20:20:58 +0100 Subject: simplify python file structure --- 2024/09/python.py | 62 +++++++++++++++++++++++++++++++++++++++++++ 2024/09/python/main.py | 60 ----------------------------------------- 2024/09/python/pyproject.toml | 6 ----- 3 files changed, 62 insertions(+), 66 deletions(-) create mode 100755 2024/09/python.py delete mode 100644 2024/09/python/main.py delete mode 100644 2024/09/python/pyproject.toml (limited to '2024/09') diff --git a/2024/09/python.py b/2024/09/python.py new file mode 100755 index 0000000..1cf450e --- /dev/null +++ b/2024/09/python.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 + +from fileinput import input + +disk = list(input())[0].strip() + +id_ = -1 +blocks = [] +tail = 0 +file = True +for c in disk: + blocks.append([tail, tail + int(c), (id_ := id_ + 1) if file else -1]) + tail += int(c) + file = not file + + +def expand(bs): + return [x for a, b, x in sorted(bs) for _ in range(b - a)] + + +def debug(bs): + print("".join(str(b) if b != -1 else "." for b in expand(bs))) + + +# Silver +expanded = expand(blocks) +tail = len(expanded) - 1 +fill = expanded.index(-1) +while fill < tail: + expanded[fill] = expanded[tail] + expanded[tail] = -1 + while expanded[tail] == -1: + tail -= 1 + while expanded[fill] != -1: + fill += 1 +silver = sum(idx * n for idx, n in enumerate(expanded) if n != -1) + + +# Gold +for move in blocks[::-1]: + print(move) + ma, mb, mx = move + if mx == -1: + continue + mlen = mb - ma + for tidx, to in enumerate(blocks): + ta, tb, tx = to + if ta >= ma: + break + if tx != -1: + continue + if (tlen := tb - ta) < mlen: + continue + move[2] = -1 + to[2] = mx + to[1] = ta + mlen + blocks.insert(tidx + 1, [ta + mlen, tb, -1]) + break +gold = sum(idx * n for idx, n in enumerate(expand(blocks)) if n != -1) + +print("silver:", silver) +print("gold:", gold) diff --git a/2024/09/python/main.py b/2024/09/python/main.py deleted file mode 100644 index d435163..0000000 --- a/2024/09/python/main.py +++ /dev/null @@ -1,60 +0,0 @@ -from fileinput import input - -disk = list(input())[0].strip() - -id_ = -1 -blocks = [] -tail = 0 -file = True -for c in disk: - blocks.append([tail, tail + int(c), (id_ := id_ + 1) if file else -1]) - tail += int(c) - file = not file - - -def expand(bs): - return [x for a, b, x in sorted(bs) for _ in range(b - a)] - - -def debug(bs): - print("".join(str(b) if b != -1 else "." for b in expand(bs))) - - -# Silver -expanded = expand(blocks) -tail = len(expanded) - 1 -fill = expanded.index(-1) -while fill < tail: - expanded[fill] = expanded[tail] - expanded[tail] = -1 - while expanded[tail] == -1: - tail -= 1 - while expanded[fill] != -1: - fill += 1 -silver = sum(idx * n for idx, n in enumerate(expanded) if n != -1) - - -# Gold -for move in blocks[::-1]: - print(move) - ma, mb, mx = move - if mx == -1: - continue - mlen = mb - ma - for tidx, to in enumerate(blocks): - ta, tb, tx = to - if ta >= ma: - break - if tx != -1: - continue - if (tlen := tb - ta) < mlen: - continue - move[2] = -1 - to[2] = mx - to[1] = ta + mlen - blocks.insert(tidx + 1, [ta + mlen, tb, -1]) - break -gold = sum(idx * n for idx, n in enumerate(expand(blocks)) if n != -1) - -print("silver:", silver) -print("gold:", gold) diff --git a/2024/09/python/pyproject.toml b/2024/09/python/pyproject.toml deleted file mode 100644 index 66c042a..0000000 --- a/2024/09/python/pyproject.toml +++ /dev/null @@ -1,6 +0,0 @@ -[project] -name = "aoc-2024-09" -version = "0.1.0" -description = "advent of code 2024-09" -requires-python = ">=3.13" -dependencies = [] -- cgit v1.2.3