-
Notifications
You must be signed in to change notification settings - Fork 0
/
day05.py
64 lines (51 loc) · 2.18 KB
/
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
from collections import deque
import re
from src.common.utils import SolverFunctions
title = 'Day 5: Supply Stacks'
parser_method = 'strings_per_line'
handle_data = 'paragraph'
class SolveTheDay(SolverFunctions):
@staticmethod
def helper(data):
crates_data = data[0][:-1]
operations_data = data[1]
operation_number = 1
operation = None
operations = {operation_number: operation}
crates = {}
for letters_position in crates_data:
for key, letter in enumerate(range(1, len(letters_position), 4)):
key += 1
if letters_position[letter] == " ":
continue
if key not in crates:
crates[key] = deque(letters_position[letter])
else:
crates[key].appendleft(letters_position[letter])
for operation in operations_data:
operations[operation_number] = operation
operation_number += 1
return crates, operations
@classmethod
def level_1(cls, data):
crates, operations = cls.helper(data)
for operation in operations.values():
# operation = operation.replace(' ', '').replace('move', '').replace('from', '').replace('to', '')
# _move, _from, _to = operation.strip()
_move, _from, _to = map(int, re.findall(r'\d+', operation))
for counter in range(1, _move+1):
crates[_to].append(crates[_from].pop())
arranged_crates = dict(sorted(crates.items()))
return ''.join(crate.pop() for crate in arranged_crates.values())
@classmethod
def level_2(cls, data):
crates, operations = cls.helper(data)
for operation in operations.values():
_move, _from, _to = map(int, re.findall(r'\d+', operation))
temp_movement = deque()
for counter in range(1, _move + 1):
temp_movement.extendleft(crates[_from].pop())
crates[_to].extend(temp_movement)
arranged_crates = dict(sorted(crates.items()))
top_crates = ''.join(crate.pop() for crate in arranged_crates.values())
return top_crates