Advertisement
Guest User

AoC 2021 - Day 25

a guest
Dec 28th, 2021
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 KB | None | 0 0
  1. """
  2. Advent of Code 2021 - Day 25
  3. https://adventofcode.com/2021/day/25
  4. """
  5.  
  6. from copy import deepcopy
  7. from typing import List, Optional
  8.  
  9. DAY = '25'
  10.  
  11. FULL_INPUT_FILE = f'../inputs/day{DAY}/input.full.txt'
  12. TEST_INPUT_FILE = f'../inputs/day{DAY}/input.test.txt'
  13.  
  14.  
  15. class CucumberMap:
  16.     def __init__(self, initial_map: List[List[str]]) -> None:
  17.         self.map = initial_map
  18.  
  19.     def move_cucumbers(self) -> bool:
  20.         moved = False
  21.  
  22.         # move east
  23.         new_map: List = deepcopy(self.map)
  24.         for row_num in range(len(self.map)):
  25.             for col_num in range(len(self.map[row_num])):
  26.                 next_i = col_num + 1 if col_num + 1 < len(self.map[row_num]) else 0
  27.                 if self.map[row_num][col_num] == '>' and not self.map[row_num][next_i]:
  28.                     new_map[row_num][next_i] = '>'
  29.                     new_map[row_num][col_num] = None
  30.                     moved = True
  31.         self.map = new_map
  32.  
  33.         # move south
  34.         new_map = deepcopy(self.map)
  35.         for row_num in range(len(self.map)):
  36.             next_row_num = row_num + 1 if row_num + 1 < len(self.map) else 0
  37.             for col_num in range(len(self.map[row_num])):
  38.                 if self.map[row_num][col_num] == 'v' and not self.map[next_row_num][col_num]:
  39.                     new_map[row_num][col_num] = None
  40.                     new_map[next_row_num][col_num] = 'v'
  41.                     moved = True
  42.         self.map = new_map
  43.  
  44.         return moved
  45.  
  46.     def dump(self) -> str:
  47.         return '\n'.join([''.join([c if c else '.' for c in r]) for r in self.map]) + '\n'
  48.  
  49.  
  50. def load_data(infile_path: str) -> List[List[Optional[str]]]:
  51.     data = []
  52.     with open(infile_path, 'r', encoding='ascii') as infile:
  53.         [data.append([p if p != '.' else None for p in line.strip()]) for line in infile]
  54.     return data
  55.  
  56.  
  57. def part_1(infile_path: str) -> int:
  58.     cm = CucumberMap(load_data(infile_path))
  59.     i = 1
  60.     while cm.move_cucumbers():
  61.         i += 1
  62.     return i
  63.  
  64.  
  65. if __name__ == '__main__':
  66.     part1_answer = part_1(TEST_INPUT_FILE)
  67.     print(f'Part 1: {part1_answer}')
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement