commit 65d4a43f44d74070b4fe8e886383abe1aaa6f54a
parent b60a609063ddd42cbdfef99bd278d5c95e51eb38
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date: Mon, 13 Dec 2021 16:30:13 -0500
Solution to day 13, part 2
Diffstat:
1 file changed, 45 insertions(+), 0 deletions(-)
diff --git a/day13_part2.py b/day13_part2.py
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+"""Advent of Code 2021, day 13 (part 1): Transparent Oragami
+Fold a grid of dots repeatedly to reveal a pattern, and read the pattern"""
+
+# Applying the folds, printing the result to the terminal, and reading it in?
+# Fairly easy. It would be a taller order to hack together an OCR-like process,
+# but I'm reminded that "good programmers are lazy" and calling this done.
+
+from typing import Tuple, List
+from functools import reduce
+import numpy as np
+from day13_part1 import (EXAMPLE_INPUT,
+ parse_input,
+ fold_dotgrid)
+from utils import get_puzzle_input
+
+def apply_all_folds(dotgrid: np.ndarray,
+ instructions: List[Tuple[int, int]]) -> np.ndarray:
+ """Apply all the folds in the instructions and return the final dotgrid"""
+ return reduce(fold_dotgrid, instructions, dotgrid)
+
+def prettify_dotgrid(dotgrid: np.ndarray) -> str:
+ """Return a string representation of the solution (spanning multiple
+ lines)"""
+ dotgrid_str = np.empty_like(dotgrid, dtype=str)
+ dotgrid_str[:, :] = " "
+ dotgrid_str[dotgrid] = "#"
+ return '\n'.join([''.join(l) for l in dotgrid_str]) + '\n'
+
+def solve_puzzle(input_string: str) -> str:
+ """Returns a printable version of the puzzle solution"""
+ return prettify_dotgrid(
+ apply_all_folds(*parse_input(input_string))
+ )
+
+def main() -> None:
+ """Run when the file is called as a script"""
+ assert solve_puzzle(EXAMPLE_INPUT) == \
+ "#####\n# #\n# #\n# #\n#####\n \n \n"
+ print("Thermal imaging code:",
+ solve_puzzle(get_puzzle_input(13)),
+ sep='\n')
+
+if __name__ == "__main__":
+ main()