From 270586e4100ca2eff3aec9ace128d8cdfa6923a4 Mon Sep 17 00:00:00 2001 From: Vasile-Hij Date: Mon, 19 Jun 2023 00:00:36 +0300 Subject: [PATCH] automate scripts and files closes #9 --- day8.txt | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 day8.txt diff --git a/day8.txt b/day8.txt new file mode 100644 index 0000000..a7d962e --- /dev/null +++ b/day8.txt @@ -0,0 +1,58 @@ +def add_2(a, b): + return (a[0] + b[0], a[1] + b[1]) + + +def quantify(iterable, pred=bool) -> int: + return sum(1 for item in iterable if pred(item)) + + +class Matrix2D(dict): + def __init__(self, grid=(), directions=four_directions, skip=(), default=KeyError): + self.directions = directions + self.default = default + + self.update( + { + (x, y): value + for y, row in enumerate(grid) + for x, value in enumerate(row) + if value not in skip + } + ) + + + + +from common.util import get_functions + + +def start_day(): + name = '--- Day 8: Treetop Tree House ---' + parser_function = 'find_digits' + display_lines_or_paragraph = 'lines' + return name, parser_function, display_lines_or_paragraph + + + +def helper(_data): + quantify, add_2, Matrix2D, four_directions = get_functions( + 'quantify', 'add_2', 'Matrix2D', 'four_directions' + ) + + def go_in_direction(start, direction, grid): + while True: + start = add_2(start, direction) + if start not in grid: + return + yield start + + def visible_from_outside(grid) -> int: + def is_visible(loc) -> bool: + return any( + all(grid[p] < grid[loc] for p in go_in_direction(loc, dir, grid)) + for dir in four_directions + ) + + return quantify(grid, is_visible) + + return visible_from_outside(Matrix2D(_data)) \ No newline at end of file