Guest User

aoc_2022_day_05

a guest
Dec 5th, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.64 KB | None | 0 0
  1. #!/usr/bin/env python3.8
  2. """
  3.  
  4. """
  5. import sys
  6. from pathlib import Path
  7. import re
  8. import pytest
  9. from copy import deepcopy
  10.  
  11. INPUTS_DIR = 'inputs'
  12. INPUT_FILE = 'day05.txt'
  13. PROJECT_DIR = Path('.')
  14. FILE_PATH = PROJECT_DIR / INPUTS_DIR / INPUT_FILE
  15.  
  16.  
  17. def read_data(file_path):
  18.     """Read file"""
  19.     for row in open(file_path):
  20.         yield row
  21.  
  22.  
  23. def result(file_path):
  24.     iterator = read_data(file_path)
  25.     result, result_ver2 = create_results(iterator)
  26.     return result, result_ver2
  27.  
  28.  
  29. def create_results(iterator):
  30.     result = ''
  31.     result_ver2 = ''
  32.     stacks = create_stacks(iterator)
  33.     stacks_ver1 = deepcopy(stacks)
  34.     stacks_ver2 = deepcopy(stacks)
  35.     print(stacks)
  36.     for row in iterator:
  37.         if 'move' in row:
  38.             stacks_ver1 = modify_stacks(stacks_ver1, row)
  39.             stacks_ver2 = modify_stacks(stacks_ver2, row, ver=2)
  40.         print(f'{row}{stacks_ver1}\n{stacks_ver2})')
  41.     for stack in stacks_ver1:
  42.         result += stacks_ver1[stack].pop()
  43.     for stack in stacks_ver2:
  44.         result_ver2 += stacks_ver2[stack].pop()
  45.     return result, result_ver2
  46.  
  47.  
  48. def slice_row(row):
  49.     while row:
  50.         yield row[:4]
  51.         row = row[4:]
  52.  
  53.  
  54. def divide_row(row):
  55.     for s in slice_row(row):
  56.         row = row[4:]
  57.         w = re.search(r'\w', s.strip())
  58.         yield str(w.group()) if w else ''
  59.  
  60.  
  61. def update_stacks(stacks, row):
  62.     idx = 0
  63.     for crate in divide_row(row):
  64.         if stacks and len(stacks) < idx+1:
  65.             stacks.append([])
  66.         if crate:
  67.             stacks[idx].append(crate)
  68.         idx += 1
  69.     return stacks
  70.  
  71.  
  72. def create_stacks(iterator):
  73.     stacks = [[]]
  74.     for row in iterator:
  75.         if not row.strip():
  76.             break
  77.         stacks = update_stacks(stacks, row)
  78.     stacks_ids = {stack.pop(): list(reversed(stack)) for stack in stacks}
  79.     return stacks_ids
  80.  
  81.  
  82. def modify_stacks(stacks=None, row=None, ver=1):
  83.     _cnt, _from, _to = parse_move_rows(row)
  84.     _stacks = deepcopy(stacks)
  85.     no = int(_cnt)
  86.     if ver == 1:
  87.         for i in range(no):
  88.             _stacks[_to] += _stacks[_from].pop()
  89.     else:
  90.         idx = len(_stacks[_from]) - no
  91.         _stacks[_to] += _stacks[_from][idx:]
  92.         _stacks[_from] = _stacks[_from][:idx]
  93.     return _stacks
  94.  
  95.  
  96. def parse_move_rows(row):
  97.     return re.findall(r'\d+', row)
  98.  
  99.  
  100. CRATES = """
  101.    [D]    
  102. [N] [C]    
  103. [Z] [M] [P]
  104. 1   2   3
  105.  
  106. move 1 from 2 to 1
  107. move 3 from 1 to 3
  108. move 2 from 2 to 1
  109. move 1 from 1 to 2
  110. """
  111. STACKS_VER2_0 = STACKS_0 = {
  112.     '1': ['Z', 'N'],
  113.     '2': ['M', 'C', 'D'],
  114.     '3': ['P']
  115. }
  116.  
  117.  
  118. def test_proper_crates_stacks():
  119.     """Check if parsed crates stacks are correct"""
  120.     stacks = create_stacks(CRATES.lstrip('\n').split('\n'))
  121.     assert stacks == STACKS_0, f'Incorrect : {stacks} {STACKS_0}'
  122.  
  123.  
  124. STACKS_VER2_1 = STACKS_1 = {
  125.     '1': ['Z', 'N', 'D'],
  126.     '2': ['M', 'C'],
  127.     '3': ['P']
  128. }
  129.  
  130. STACKS_2 = {
  131.     '1': [],
  132.     '2': ['M', 'C'],
  133.     '3': ['P', 'D', 'N', 'Z']
  134. }
  135.  
  136. STACKS_3 = {
  137.     '1': ['C', 'M'],
  138.     '2': [],
  139.     '3': ['P', 'D', 'N', 'Z']
  140. }
  141.  
  142. STACKS_4 = {
  143.     '1': ['C'],
  144.     '2': ['M'],
  145.     '3': ['P', 'D', 'N', 'Z']
  146. }
  147.  
  148. RESULT = 'CMZ'
  149.  
  150. STACKS_VER2_2 = {
  151.     '1': [],
  152.     '2': ['M', 'C'],
  153.     '3': ['P', 'Z', 'N', 'D']
  154. }
  155. STACKS_VER2_3 = {
  156.     '1': ['M', 'C'],
  157.     '2': [],
  158.     '3': ['P', 'Z', 'N', 'D']
  159. }
  160.  
  161. STACKS_VER2_4 = {
  162.     '1': ['M'],
  163.     '2': ['C'],
  164.     '3': ['P', 'Z', 'N', 'D']
  165. }
  166.  
  167. RESULT_VER2 = 'MCD'
  168.  
  169.  
  170. def test_crates_stacks_movements():
  171.     result = ''
  172.     stacks_name = 'STACKS_'
  173.     stacks_no = 0
  174.     stacks = create_stacks(CRATES.lstrip('\n').split('\n'))
  175.     _globals = globals()
  176.     expected_stacks = _globals.get(f'{stacks_name}{stacks_no}')
  177.     assert stacks == expected_stacks, f'Incorrect : {stacks} {expected_stacks}'
  178.     for row in CRATES.split('\n'):
  179.         if 'move' in row:
  180.             stacks_no += 1
  181.             stacks = modify_stacks(stacks, row)
  182.             expected_stacks = _globals.get(f'{stacks_name}{stacks_no}')
  183.             assert stacks == expected_stacks, f'Incorrect : {stacks} {expected_stacks}'
  184.     for stack in stacks:
  185.         result += stacks[stack].pop()
  186.     assert result == RESULT, f'Incorrect result. Current: {result}. Expected {RESULT}'
  187.  
  188.  
  189. def test_crates_stacks_movements_ver2():
  190.     result = ''
  191.     stacks_name = 'STACKS_VER2_'
  192.     stacks_no = 0
  193.     stacks = create_stacks(CRATES.lstrip('\n').split('\n'))
  194.     _globals = globals()
  195.     expected_stacks_name = f'{stacks_name}{stacks_no}'
  196.     expected_stacks = _globals.get(expected_stacks_name)
  197.     assert expected_stacks, f'No expected stacks {expected_stacks_name}'
  198.     assert stacks == expected_stacks, f'Incorrect : {stacks} {expected_stacks}'
  199.     for row in CRATES.split('\n'):
  200.         if 'move' in row:
  201.             stacks_no += 1
  202.             stacks = modify_stacks(stacks, row, ver=2)
  203.             expected_stacks_name = f'{stacks_name}{stacks_no}'
  204.             expected_stacks = _globals.get(expected_stacks_name)
  205.             assert expected_stacks, f'No expected stacks {expected_stacks_name}'
  206.             assert stacks == expected_stacks, f'Incorrect : {stacks} {expected_stacks}'
  207.     for stack in stacks:
  208.         result += stacks[stack].pop()
  209.     assert result == RESULT_VER2, f'Incorrect result. Current: {result}. Expected {RESULT}'
  210.  
  211.  
  212. """
  213. [D]        
  214. [N] [C]    
  215. [Z] [M] [P]
  216. 1   2   3
  217. 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:
  218.  
  219.        [Z]
  220.        [N]
  221.    [C] [D]
  222.    [M] [P]
  223. 1   2   3
  224. 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:
  225.  
  226.        [Z]
  227.        [N]
  228. [M]     [D]
  229. [C]     [P]
  230. 1   2   3
  231. Finally, one crate is moved from stack 1 to stack 2:
  232.  
  233.        [Z]
  234.        [N]
  235.        [D]
  236. [C] [M] [P]
  237. 1   2   3
  238. """
  239.  
  240. """
  241. [D]        
  242. [N] [C]    
  243. [Z] [M] [P]
  244. 1   2   3
  245. 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:
  246.  
  247.        [D]
  248.        [N]
  249.    [C] [Z]
  250.    [M] [P]
  251. 1   2   3
  252. Next, as both crates are moved from stack 2 to stack 1, they retain their order as well:
  253.  
  254.        [D]
  255.        [N]
  256. [C]     [Z]
  257. [M]     [P]
  258. 1   2   3
  259. Finally, a single crate is still moved from stack 1 to stack 2, but now it's crate C that gets moved:
  260.  
  261.        [D]
  262.        [N]
  263.        [Z]
  264. [M] [C] [P]
  265. 1   2   3
  266. """
  267.  
  268. if __name__ == '__main__':
  269.     FILE_PATH = sys.argv[1] if len(sys.argv) > 1 else FILE_PATH
  270.     print(result(FILE_PATH))
  271.     pytest.main(sys.argv)
  272.  
Advertisement
Add Comment
Please, Sign In to add comment