diff options
| author | mhsn <mail@mhsn.net> | 2026-03-18 21:48:13 +0000 |
|---|---|---|
| committer | mhsn <mail@mhsn.net> | 2026-03-18 21:48:13 +0000 |
| commit | 86bac31392a76da84817eec020d2b84d099b3cc1 (patch) | |
| tree | e2ee52db59b86b914d5b4bcceb19c9b5d899fff4 /2024/22/python.py | |
| parent | 62fe361fc42dea75deaf7ac31c0ba6ba80e26a9c (diff) | |
| download | puzzles-86bac31392a76da84817eec020d2b84d099b3cc1.tar.gz puzzles-86bac31392a76da84817eec020d2b84d099b3cc1.zip | |
Diffstat (limited to '2024/22/python.py')
| -rwxr-xr-x | 2024/22/python.py | 59 |
1 files changed, 0 insertions, 59 deletions
diff --git a/2024/22/python.py b/2024/22/python.py deleted file mode 100755 index 98d8898..0000000 --- a/2024/22/python.py +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env python3 - -from collections import Counter, deque -from fileinput import input -from itertools import islice - -lines = [int(line.strip()) for line in input()] - - -def secrets(secret): - x = secret - while True: - yield x - x ^= x << 6 - x &= 0xFFFFFF - x ^= x >> 5 - x &= 0xFFFFFF - x ^= x << 11 - x &= 0xFFFFFF - - -def prices(it): - for s in it: - yield s % 10 - - -def diff(it): - prev = next(it) - for curr in it: - yield curr, curr - prev - prev = curr - - -def to_seqs(it, n=4): - head = map(lambda x: x[1], islice(it, n - 1)) - q = deque(head, maxlen=n) - for price, diff in it: - q.append(diff) - yield price, tuple(q) - - -def seq_sells(it): - sells = {} - for price, diff in it: - if diff in sells: - continue - sells[diff] = price - return sells - - -best_seqs = Counter() -for x in lines: - best_seqs += seq_sells(to_seqs(islice(diff(prices(secrets(x))), 2000))) - -silver = sum(next(islice(secrets(x), 2000, None)) for x in lines) -gold = best_seqs.most_common(1)[0][1] - -print("silver:", silver) -print("gold:", gold) |
