Skip to content

Commit ca5ee8f

Browse files
committed
day 14
1 parent 6a24b5e commit ca5ee8f

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
|[11](https://adventofcode.com/2020/day/11)|Seating System|[py](/day11/main.py)|
1616
|[12](https://adventofcode.com/2020/day/12)|Rain Risk|[py](/day12/main.py)|
1717
|[13](https://adventofcode.com/2020/day/13)|Shuttle Search|[py](/day13/main.py)|
18-
|[14](https://adventofcode.com/2020/day/14)|-|-|
18+
|[14](https://adventofcode.com/2020/day/14)|Docking Data|[py](/day14/main.py)|
1919
|[15](https://adventofcode.com/2020/day/15)|-|-|
2020
|[16](https://adventofcode.com/2020/day/16)|-|-|
2121
|[17](https://adventofcode.com/2020/day/17)|-|-|

day14/main.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from itertools import product
2+
from parse import search
3+
4+
def possible_addrs(mask, addr):
5+
mask2 = "".join(v if m == "0" else m for m, v in zip(mask, f"{addr:036b}"))
6+
res = []
7+
for t in product("01", repeat=mask2.count("X")):
8+
it = iter(t)
9+
res.append(int("".join(next(it) if c == "X" else c for c in mask2), 2))
10+
return res
11+
12+
with open("input.txt") as f:
13+
lines = [x.strip() for x in f]
14+
15+
for is_part1 in [True, False]:
16+
mask = ""
17+
mem = {}
18+
for line in lines:
19+
if line.startswith("mask"):
20+
mask = line.split(" = ")[1]
21+
else:
22+
arg1, arg2 = search("mem[{:d}] = {:d}", line).fixed
23+
if is_part1:
24+
mem[arg1] = int("".join(v if m == "X" else m for m, v in zip(mask, f"{arg2:036b}")), 2)
25+
else:
26+
for addr in possible_addrs(mask, arg1):
27+
mem[addr] = arg2
28+
29+
print(sum(mem.values()))

0 commit comments

Comments
 (0)