From 8dad3c87396178c69791fb4e3ae7f627d5ae2259 Mon Sep 17 00:00:00 2001 From: kamoshi <18511281+kamoshi@users.noreply.github.com> Date: Thu, 3 Dec 2020 06:33:42 +0100 Subject: [PATCH] Create day03.py --- 2020/Python/day03.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 2020/Python/day03.py diff --git a/2020/Python/day03.py b/2020/Python/day03.py new file mode 100644 index 0000000..8d66db7 --- /dev/null +++ b/2020/Python/day03.py @@ -0,0 +1,38 @@ +lines = [] + +with open("input.txt") as file: + for line in file: + lines.append(line.rstrip()) + + +def check_tree(data: list[str], x: int, y: int) -> bool: + width = len(data[0]) + return data[y][x % width] == '#' + + +def count_trees_by_slope(data: list[str], slope_x: int, slope_y: int) -> int: + curr_x, curr_y = 0, 0 + total_tree = 0 + while True: + curr_x += slope_x + curr_y += slope_y + if not curr_y < len(data): + break + total_tree += check_tree(data, curr_x, curr_y) + return total_tree + + +def solve_p1(): + print(count_trees_by_slope(data=lines, slope_x=3, slope_y=1)) + + +def solve_p2(): + checked = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)] + result = 1 + for (x, y) in checked: + result *= count_trees_by_slope(data=lines, slope_x=x, slope_y=y) + print(result) + + +solve_p1() +solve_p2()