diff options
author | mhsn <mail@mhsn.net> | 2024-12-06 12:00:00 +0000 |
---|---|---|
committer | mhsn <mail@mhsn.net> | 2024-12-06 12:00:00 +0000 |
commit | 8c7880165d47d0646ca5f0d317f57649575e73d9 (patch) | |
tree | 1b8a2ee05ccdf0e255504a116ada7787e5bdea3b | |
parent | 71aa0ee1fdbbc07a730346e46872681b26671287 (diff) | |
download | aoc-8c7880165d47d0646ca5f0d317f57649575e73d9.tar.gz aoc-8c7880165d47d0646ca5f0d317f57649575e73d9.zip |
2024-06 python p1,p2
-rw-r--r-- | 2024/06/python/main.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/2024/06/python/main.py b/2024/06/python/main.py new file mode 100644 index 0000000..5fd6c3d --- /dev/null +++ b/2024/06/python/main.py @@ -0,0 +1,55 @@ +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) |