diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-12-07 14:28:02 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-12-07 14:28:02 +0100 |
| commit | 01c2cd358b3f693181fa86e82414b3e32f9a890e (patch) | |
| tree | 1782399c0d80c4ff5f9fdd225b939806723a7c02 /2025/07/b.py | |
| parent | eb233235c876600bafa39a538d18ce11cfc72b39 (diff) | |
| download | aoc-01c2cd358b3f693181fa86e82414b3e32f9a890e.tar.gz aoc-01c2cd358b3f693181fa86e82414b3e32f9a890e.zip | |
Use defaultdict
Diffstat (limited to '2025/07/b.py')
| -rw-r--r-- | 2025/07/b.py | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/2025/07/b.py b/2025/07/b.py index 4457654..0a8672e 100644 --- a/2025/07/b.py +++ b/2025/07/b.py | |||
| @@ -1,15 +1,15 @@ | |||
| 1 | import fileinput | 1 | import fileinput |
| 2 | from collections import defaultdict | ||
| 2 | 3 | ||
| 3 | with fileinput.input() as lines: | 4 | with fileinput.input() as lines: |
| 4 | rows = [line[:-1] for line in lines] | 5 | rows = [line[:-1] for line in lines] |
| 5 | 6 | ||
| 6 | pos = {rows[0].find('S'): 1} | 7 | pos = {rows[0].find('S'): 1} |
| 7 | for row in rows[1:]: | 8 | for row in rows[1:]: |
| 8 | newpos = {} | 9 | newpos = defaultdict(int) |
| 9 | for p, w in pos.items(): | 10 | for p, w in pos.items(): |
| 10 | for pp in [p-1,p+1] if row[p] == '^' else [p]: | 11 | for pp in [p-1,p+1] if row[p] == '^' else [p]: |
| 11 | t = newpos.get(pp, 0) | 12 | newpos[pp] += w |
| 12 | newpos[pp] = t + w | ||
| 13 | pos = dict(newpos) | 13 | pos = dict(newpos) |
| 14 | 14 | ||
| 15 | print(sum(pos.values())) | 15 | print(sum(pos.values())) |
