This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_05.py
70 lines (60 loc) · 2.22 KB
/
day_05.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import copy
import re
from collections import OrderedDict
from pathlib import Path
from typing import List, Tuple, cast
from constants import INPUTS_DIR, UTF_8
INPUT_PATH = Path(INPUTS_DIR) / "day-05.txt"
# INPUT_PATH = Path(INPUTS_DIR) / "example.txt"
def parse(
start_config: str,
instructions_block: str,
) -> Tuple[OrderedDict[int, List[str]], List[Tuple[int, int, int]]]:
# top chunk
start_config = start_config.split("\n")
cols = OrderedDict()
for match in re.finditer(r"\d+", start_config[-1]):
col = int(match.group())
loc = match.start()
this_col = []
for i in range(len(start_config) - 2, -1, -1):
line = start_config[i]
try: # in case line-final spaces get trimmed
sym = line[loc]
except IndexError:
break
if sym.isspace():
break
this_col.append(sym)
cols[col] = this_col
# bottom chunk
instructions = [
cast(
Tuple[int, int, int],
tuple(map(int, re.fullmatch(r"move (\d+) from (\d+) to (\d+)", instruction).groups()))
)
for instruction in instructions_block.strip().split("\n")
]
return cols, instructions
def main1(cols: OrderedDict[int, List[str]], instructions: List[Tuple[int, int, int]]) -> List[str]:
cols = copy.deepcopy(cols)
for count, start, end in instructions:
for i in range(count):
val = cols[start].pop()
cols[end].append(val)
return [col[-1] for col in cols.values()]
def main2(cols: OrderedDict[int, List[str]], instructions: List[Tuple[int, int, int]]) -> List[str]:
cols = copy.deepcopy(cols)
for count, start, end in instructions:
stack = cols[start][-count:]
del cols[start][-count:]
cols[end] += stack
return [col[-1] for col in cols.values()]
if __name__ == "__main__":
with open(INPUT_PATH, "r", encoding=UTF_8) as f:
start_config_, instructions_block_ = f.read().split("\n\n")
cols_, instructions_ = parse(start_config_, instructions_block_)
ans = main1(cols_, instructions_)
print("part 1:", "".join(ans))
ans = main2(cols_, instructions_)
print("part 2:", "".join(ans))