diff options
author | mhsn <mail@mhsn.net> | 2024-12-20 08:56:29 +0000 |
---|---|---|
committer | mhsn <mail@mhsn.net> | 2024-12-20 08:56:29 +0000 |
commit | 7013617101edf1eb4d974c4a5abec15d3f63a29d (patch) | |
tree | 619f1ce2a6666955888e5224b0b3c31cc59aa9a1 | |
parent | 380f83357eedb003ec7aae1ea7244adea6c266a7 (diff) | |
download | aoc-7013617101edf1eb4d974c4a5abec15d3f63a29d.tar.gz aoc-7013617101edf1eb4d974c4a5abec15d3f63a29d.zip |
2024-20 python p1
-rw-r--r-- | 2024/20/python/main.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/2024/20/python/main.py b/2024/20/python/main.py new file mode 100644 index 0000000..c2f3f73 --- /dev/null +++ b/2024/20/python/main.py @@ -0,0 +1,39 @@ +from fileinput import input + +grid = { + complex(idx, idy): c + for idy, line in enumerate(input()) + for idx, c in enumerate(line.strip()) +} +start = next(p for p, c in grid.items() if c == "S") +end = next(p for p, c in grid.items() if c == "E") + +times = {} +q = [(start, 0)] +while q: + curr, t = q.pop() + if curr in times: + continue + times[curr] = t + for d in [1, -1, 1j, -1j]: + x = curr + d + if x not in grid or grid[x] == "#": + continue + q.append((x, t + 1)) + +cheats = [] +for wall in (p for p, c in grid.items() if c == "#"): + for d in [1, -1, 1j, -1j]: + if (x := wall - d) not in grid or grid[x] == "#": + continue + if (y := wall + d) not in grid or grid[y] == "#": + continue + if (saved := times[y] - times[x] - 2) <= 0: + continue + cheats.append(saved) + +silver = sum(1 for c in cheats if c >= 100) +gold = 0 + +print("silver:", silver) +print("gold:", gold) |