Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3.8
- """
- """
- import sys
- from pathlib import Path
- import re
- import pytest
- from copy import deepcopy
- INPUTS_DIR = 'inputs'
- INPUT_FILE = 'day05.txt'
- PROJECT_DIR = Path('.')
- FILE_PATH = PROJECT_DIR / INPUTS_DIR / INPUT_FILE
- def read_data(file_path):
- """Read file"""
- for row in open(file_path):
- yield row
- def result(file_path):
- iterator = read_data(file_path)
- result, result_ver2 = create_results(iterator)
- return result, result_ver2
- def create_results(iterator):
- result = ''
- result_ver2 = ''
- stacks = create_stacks(iterator)
- stacks_ver1 = deepcopy(stacks)
- stacks_ver2 = deepcopy(stacks)
- print(stacks)
- for row in iterator:
- if 'move' in row:
- stacks_ver1 = modify_stacks(stacks_ver1, row)
- stacks_ver2 = modify_stacks(stacks_ver2, row, ver=2)
- print(f'{row}{stacks_ver1}\n{stacks_ver2})')
- for stack in stacks_ver1:
- result += stacks_ver1[stack].pop()
- for stack in stacks_ver2:
- result_ver2 += stacks_ver2[stack].pop()
- return result, result_ver2
- def slice_row(row):
- while row:
- yield row[:4]
- row = row[4:]
- def divide_row(row):
- for s in slice_row(row):
- row = row[4:]
- w = re.search(r'\w', s.strip())
- yield str(w.group()) if w else ''
- def update_stacks(stacks, row):
- idx = 0
- for crate in divide_row(row):
- if stacks and len(stacks) < idx+1:
- stacks.append([])
- if crate:
- stacks[idx].append(crate)
- idx += 1
- return stacks
- def create_stacks(iterator):
- stacks = [[]]
- for row in iterator:
- if not row.strip():
- break
- stacks = update_stacks(stacks, row)
- stacks_ids = {stack.pop(): list(reversed(stack)) for stack in stacks}
- return stacks_ids
- def modify_stacks(stacks=None, row=None, ver=1):
- _cnt, _from, _to = parse_move_rows(row)
- _stacks = deepcopy(stacks)
- no = int(_cnt)
- if ver == 1:
- for i in range(no):
- _stacks[_to] += _stacks[_from].pop()
- else:
- idx = len(_stacks[_from]) - no
- _stacks[_to] += _stacks[_from][idx:]
- _stacks[_from] = _stacks[_from][:idx]
- return _stacks
- def parse_move_rows(row):
- return re.findall(r'\d+', row)
- CRATES = """
- [D]
- [N] [C]
- [Z] [M] [P]
- 1 2 3
- move 1 from 2 to 1
- move 3 from 1 to 3
- move 2 from 2 to 1
- move 1 from 1 to 2
- """
- STACKS_VER2_0 = STACKS_0 = {
- '1': ['Z', 'N'],
- '2': ['M', 'C', 'D'],
- '3': ['P']
- }
- def test_proper_crates_stacks():
- """Check if parsed crates stacks are correct"""
- stacks = create_stacks(CRATES.lstrip('\n').split('\n'))
- assert stacks == STACKS_0, f'Incorrect : {stacks} {STACKS_0}'
- STACKS_VER2_1 = STACKS_1 = {
- '1': ['Z', 'N', 'D'],
- '2': ['M', 'C'],
- '3': ['P']
- }
- STACKS_2 = {
- '1': [],
- '2': ['M', 'C'],
- '3': ['P', 'D', 'N', 'Z']
- }
- STACKS_3 = {
- '1': ['C', 'M'],
- '2': [],
- '3': ['P', 'D', 'N', 'Z']
- }
- STACKS_4 = {
- '1': ['C'],
- '2': ['M'],
- '3': ['P', 'D', 'N', 'Z']
- }
- RESULT = 'CMZ'
- STACKS_VER2_2 = {
- '1': [],
- '2': ['M', 'C'],
- '3': ['P', 'Z', 'N', 'D']
- }
- STACKS_VER2_3 = {
- '1': ['M', 'C'],
- '2': [],
- '3': ['P', 'Z', 'N', 'D']
- }
- STACKS_VER2_4 = {
- '1': ['M'],
- '2': ['C'],
- '3': ['P', 'Z', 'N', 'D']
- }
- RESULT_VER2 = 'MCD'
- def test_crates_stacks_movements():
- result = ''
- stacks_name = 'STACKS_'
- stacks_no = 0
- stacks = create_stacks(CRATES.lstrip('\n').split('\n'))
- _globals = globals()
- expected_stacks = _globals.get(f'{stacks_name}{stacks_no}')
- assert stacks == expected_stacks, f'Incorrect : {stacks} {expected_stacks}'
- for row in CRATES.split('\n'):
- if 'move' in row:
- stacks_no += 1
- stacks = modify_stacks(stacks, row)
- expected_stacks = _globals.get(f'{stacks_name}{stacks_no}')
- assert stacks == expected_stacks, f'Incorrect : {stacks} {expected_stacks}'
- for stack in stacks:
- result += stacks[stack].pop()
- assert result == RESULT, f'Incorrect result. Current: {result}. Expected {RESULT}'
- def test_crates_stacks_movements_ver2():
- result = ''
- stacks_name = 'STACKS_VER2_'
- stacks_no = 0
- stacks = create_stacks(CRATES.lstrip('\n').split('\n'))
- _globals = globals()
- expected_stacks_name = f'{stacks_name}{stacks_no}'
- expected_stacks = _globals.get(expected_stacks_name)
- assert expected_stacks, f'No expected stacks {expected_stacks_name}'
- assert stacks == expected_stacks, f'Incorrect : {stacks} {expected_stacks}'
- for row in CRATES.split('\n'):
- if 'move' in row:
- stacks_no += 1
- stacks = modify_stacks(stacks, row, ver=2)
- expected_stacks_name = f'{stacks_name}{stacks_no}'
- expected_stacks = _globals.get(expected_stacks_name)
- assert expected_stacks, f'No expected stacks {expected_stacks_name}'
- assert stacks == expected_stacks, f'Incorrect : {stacks} {expected_stacks}'
- for stack in stacks:
- result += stacks[stack].pop()
- assert result == RESULT_VER2, f'Incorrect result. Current: {result}. Expected {RESULT}'
- """
- [D]
- [N] [C]
- [Z] [M] [P]
- 1 2 3
- In the second step, three crates are moved from stack 1 to stack 3. Crates are moved one at a time, so the first crate to be moved (D) ends up below the second and third crates:
- [Z]
- [N]
- [C] [D]
- [M] [P]
- 1 2 3
- Then, both crates are moved from stack 2 to stack 1. Again, because crates are moved one at a time, crate C ends up below crate M:
- [Z]
- [N]
- [M] [D]
- [C] [P]
- 1 2 3
- Finally, one crate is moved from stack 1 to stack 2:
- [Z]
- [N]
- [D]
- [C] [M] [P]
- 1 2 3
- """
- """
- [D]
- [N] [C]
- [Z] [M] [P]
- 1 2 3
- However, the action of moving three crates from stack 1 to stack 3 means that those three moved crates stay in the same order, resulting in this new configuration:
- [D]
- [N]
- [C] [Z]
- [M] [P]
- 1 2 3
- Next, as both crates are moved from stack 2 to stack 1, they retain their order as well:
- [D]
- [N]
- [C] [Z]
- [M] [P]
- 1 2 3
- Finally, a single crate is still moved from stack 1 to stack 2, but now it's crate C that gets moved:
- [D]
- [N]
- [Z]
- [M] [C] [P]
- 1 2 3
- """
- if __name__ == '__main__':
- FILE_PATH = sys.argv[1] if len(sys.argv) > 1 else FILE_PATH
- print(result(FILE_PATH))
- pytest.main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment