diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-12-10 18:15:14 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-12-10 18:15:14 +0100 |
| commit | b52eef5b9399ab3888fcfe1bc9ca23c7a9f7b026 (patch) | |
| tree | df3c5525c55e5d21ce07b9bab2359468cd9827a3 /2025/10/a.py | |
| parent | 4dc2097b38697614dba7a58bf2ec53e1e54e52fe (diff) | |
| download | aoc-b52eef5b9399ab3888fcfe1bc9ca23c7a9f7b026.tar.gz aoc-b52eef5b9399ab3888fcfe1bc9ca23c7a9f7b026.zip | |
Day 10 2025
Diffstat (limited to '2025/10/a.py')
| -rw-r--r-- | 2025/10/a.py | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/2025/10/a.py b/2025/10/a.py index 5fdb11b..52fa0b0 100644 --- a/2025/10/a.py +++ b/2025/10/a.py | |||
| @@ -1,6 +1,37 @@ | |||
| 1 | import fileinput | 1 | import fileinput |
| 2 | 2 | ||
| 3 | def readline(line): | ||
| 4 | m = 0 | ||
| 5 | for i in range(1, line.index(']')): | ||
| 6 | if line[i] == '#': | ||
| 7 | m |= 1 << (i-1) | ||
| 8 | |||
| 9 | b = [] | ||
| 10 | j = line.index(']') | ||
| 11 | while '(' in line[j:]: | ||
| 12 | i = line[j:].index('(') + j + 1 | ||
| 13 | j = line[i:].index(')') + i | ||
| 14 | x = 0 | ||
| 15 | for y in line[i:j].split(','): | ||
| 16 | x |= 1 << int(y) | ||
| 17 | b.append(x) | ||
| 18 | |||
| 19 | return m, b | ||
| 20 | |||
| 21 | def works(m, b, s): | ||
| 22 | x = 0 | ||
| 23 | for j in range(len(s)): | ||
| 24 | if s[j] == '1': | ||
| 25 | x ^= b[j] | ||
| 26 | return x == m | ||
| 27 | |||
| 28 | def sol(m, b): | ||
| 29 | s = 99999999 | ||
| 30 | for i in range(2**len(b)): | ||
| 31 | if works(m, b, bin(i)[2:].rjust(len(b), '0')): | ||
| 32 | s = min(s, i.bit_count()) | ||
| 33 | return s | ||
| 34 | |||
| 3 | with fileinput.input() as lines: | 35 | with fileinput.input() as lines: |
| 4 | for line in lines: | 36 | print(sum(sol(*readline(line)) for line in lines)) |
| 5 | ... | ||
| 6 | 37 | ||
