diff options
author | Marvin Borner | 2023-12-22 16:12:48 +0100 |
---|---|---|
committer | Marvin Borner | 2023-12-22 16:12:48 +0100 |
commit | 6878b3a3ecb9371485c911338a0fef44048591d6 (patch) | |
tree | 5c1861d2e942056e4138c2c35079862a5fe26b64 /2023/22 | |
parent | 4f5178755ae81ebf25afc5d8cdf47ae5a78f76e9 (diff) |
screw gravity
Diffstat (limited to '2023/22')
-rw-r--r-- | 2023/22/solve.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/2023/22/solve.py b/2023/22/solve.py new file mode 100644 index 0000000..1e37d4c --- /dev/null +++ b/2023/22/solve.py @@ -0,0 +1,41 @@ +L = [ + (a := l.strip().split("~")) + and (b := [[int(x) for x in y.split(",")] for y in a]) + and [range(c[0], c[1] + 1) for c in (zip(*b))] + for l in open("input").readlines() +] + +L = sorted(L, key=lambda c: list(c)[2].start) + + +def gravity(L, ignore): + res = 0 + mx = {} + for i, (xs, ys, zs) in enumerate(L): + if i == ignore: + continue + m = 0 + for x in xs: + for y in ys: + if mx.get((x, y), 0) > m: + m = mx.get((x, y)) + for x in xs: + for y in ys: + mx[(x, y)] = m - zs.start + zs.stop + L[i] = (xs, ys, range(m, m - zs.start + zs.stop)) + if m < zs.start: + res += 1 + return res + + +gravity(L, -1) + +part1 = 0 +part2 = 0 +for i in range(len(L)): + c = gravity(L.copy(), i) + if c == 0: + part1 += 1 + part2 += c + +print(part1, part2) |