-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_05.py
51 lines (38 loc) · 1.72 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
from typing import NamedTuple
from src.lib.solution import SolutionABC
class Move(NamedTuple):
start: int
end: int
number: int
class Solution(SolutionABC):
def common_parse(self) -> (list[list[str]], list[Move]):
stack, movements = self._data.split("\n\n")
stacks: list[list[str]] = [[] for _ in stack.splitlines()]
for line in stack.splitlines()[:-1][::-1]: # Skipping first line and reversing order
for ind in range(0, len(line), 4): # Each group is 4 characters wide
group = line[ind: ind + 4]
if group[1] != " ":
stacks[ind // 4].append(group[1])
moves: list[Move] = []
for line in movements.splitlines(): # eg: move 5 from 4 to 5
numbers = line.split()
number = int(numbers[1]) # eg: 5
start = int(numbers[3]) - 1 # eg: 4 || Converting 1-index from input to 0-index
end = int(numbers[5]) - 1 # eg: 5 || Converting 1-index from input to 0-index
moves.append(Move(start, end, number))
return stacks, moves
def common_solve(self, reverse: bool) -> str:
stacks, moves = self.common_parse()
for move in moves:
work_load = []
for _ in range(move.number):
crate = stacks[move.start].pop()
work_load.append(crate)
work_load = work_load[::-1] if reverse else work_load
stacks[move.end] += work_load
result = "".join([stack[-1] for stack in stacks])
return result
def _part_a(self) -> str:
return self.common_solve(reverse=False) # RLFNRTNFB
def _part_b(self) -> str:
return self.common_solve(reverse=True) # MHQTLJRLB