diff options
author | mhsn <mail@mhsn.net> | 2025-09-12 20:20:58 +0100 |
---|---|---|
committer | mhsn <mail@mhsn.net> | 2025-09-12 20:36:09 +0100 |
commit | 6b733982f9f240c1c97f1fa705bfbe4cd93c640e (patch) | |
tree | dad623203c8bb40f372e0ea14479c2f564efda79 /2024/06/python.py | |
parent | 9299c4ef655b88eb8b571864c939c78b27f77723 (diff) | |
download | aoc-6b733982f9f240c1c97f1fa705bfbe4cd93c640e.tar.gz aoc-6b733982f9f240c1c97f1fa705bfbe4cd93c640e.zip |
simplify python file structure
Diffstat (limited to '2024/06/python.py')
-rwxr-xr-x | 2024/06/python.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/2024/06/python.py b/2024/06/python.py new file mode 100755 index 0000000..52b61d0 --- /dev/null +++ b/2024/06/python.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +from fileinput import input + +obstacles = set() +seen = set() + +xmax, ymax = 0, 0 +for idy, line in enumerate(input()): + ymax = max(ymax, idy) + for idx, c in enumerate(line.strip()): + xmax = max(xmax, idx) + match c: + case "^": + pos = complex(idx, idy) + dir = 0 - 1j + case "#": + obstacles.add(complex(idx, idy)) + + +def is_loop(pos, obst): + seen_ = set() + dir = 0 - 1j + while 0 <= pos.real <= xmax and 0 <= pos.imag <= ymax: + if (pos, dir) in seen_: + return True + seen_.add((pos, dir)) + if pos + dir in obstacles or pos + dir == obst: + # Rotate cw + dir *= 1j + continue + pos += dir + return False + + +gold = 0 +for idx in range(xmax + 1): + for idy in range(ymax + 1): + if complex(idx, idy) == pos: + continue + else: + gold += is_loop(pos, complex(idx, idy)) + + +while 0 <= pos.real <= xmax and 0 <= pos.imag <= ymax: + seen.add(pos) + if pos + dir in obstacles: + # Rotate cw + dir *= 1j + continue + pos += dir + + +silver = len(seen) + +print("silver:", silver) +print("gold:", gold) |