day13_part2.py (1693B)
1 #!/usr/bin/env python 2 """Advent of Code 2021, day 13 (part 1): Transparent Oragami 3 Fold a grid of dots repeatedly to reveal a pattern, and read the pattern""" 4 5 # Applying the folds, printing the result to the terminal, and reading it in? 6 # Fairly easy. It would be a taller order to hack together an OCR-like process, 7 # but I'm reminded that "good programmers are lazy" and calling this done. 8 9 from typing import Tuple, List 10 from functools import reduce 11 import numpy as np 12 from day13_part1 import (EXAMPLE_INPUT, 13 parse_input, 14 fold_dotgrid) 15 from utils import get_puzzle_input 16 17 def apply_all_folds(dotgrid: np.ndarray, 18 instructions: List[Tuple[int, int]]) -> np.ndarray: 19 """Apply all the folds in the instructions and return the final dotgrid""" 20 return reduce(fold_dotgrid, instructions, dotgrid) 21 22 def prettify_dotgrid(dotgrid: np.ndarray) -> str: 23 """Return a string representation of the solution (spanning multiple 24 lines)""" 25 dotgrid_str = np.empty_like(dotgrid, dtype=str) 26 dotgrid_str[:, :] = " " 27 dotgrid_str[dotgrid] = "#" 28 return '\n'.join([''.join(l) for l in dotgrid_str]) + '\n' 29 30 def solve_puzzle(input_string: str) -> str: 31 """Returns a printable version of the puzzle solution""" 32 return prettify_dotgrid( 33 apply_all_folds(*parse_input(input_string)) 34 ) 35 36 def main() -> None: 37 """Run when the file is called as a script""" 38 assert solve_puzzle(EXAMPLE_INPUT) == \ 39 "#####\n# #\n# #\n# #\n#####\n \n \n" 40 print("Thermal imaging code:", 41 solve_puzzle(get_puzzle_input(13)), 42 sep='\n') 43 44 if __name__ == "__main__": 45 main()