-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc2022_day05.py
66 lines (48 loc) · 1.76 KB
/
aoc2022_day05.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
"""
Advent of Code 2022
Day 05: Supply Stacks
"""
import re
import pytest
def parse_input(file_name):
with open(file_name, "r", encoding="ascii") as data_file:
crates = [[] for _ in range(9)]
moves = []
for line in data_file.read().splitlines():
if line == "":
continue
if line[0] == "m":
moves.append(tuple(map(int, re.findall(r"\d+", line))))
else:
for i in range(1, len(line), 4):
if line[i].isupper():
crates[i // 4].append(line[i])
crates = [crate[::-1] for crate in crates if crate]
return crates, moves
def day05_part1(data):
crates = [list(crate) for crate in data[0]]
moves = data[1]
for quantity, source, dest in moves:
for _ in range(quantity):
crates[dest - 1].append(crates[source - 1].pop())
return "".join(crate[-1] for crate in crates)
def day05_part2(data):
crates = [list(crate) for crate in data[0]]
moves = data[1]
for quantity, source, dest in moves:
crates[dest - 1].extend(crates[source - 1][-quantity:])
del crates[source - 1][-quantity:]
return "".join(crate[-1] for crate in crates)
@pytest.fixture(autouse=True, name="test_data")
def fixture_test_data():
return parse_input("data/day05_test.txt")
def test_day05_part1(test_data):
assert day05_part1(test_data) == "CMZ"
def test_day05_part2(test_data):
assert day05_part2(test_data) == "MCD"
if __name__ == "__main__":
input_data = parse_input("data/day05.txt")
print("Day 05 Part 1:")
print(day05_part1(input_data)) # Correct answer is JRVNHHCSJ
print("Day 05 Part 2:")
print(day05_part2(input_data)) # Correct answer is GNFBSBJLH