From 0ca7d516c2ac981764003808786acfddc847d619 Mon Sep 17 00:00:00 2001 From: William Redding Date: Wed, 8 Jan 2025 20:27:55 +0000 Subject: [PATCH] 2015 day 6 --- puzzles/Y2015/D6/p1.py | 35 +++++++++++++++++++++++++++++++++++ puzzles/Y2015/D6/p2.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 puzzles/Y2015/D6/p1.py create mode 100644 puzzles/Y2015/D6/p2.py diff --git a/puzzles/Y2015/D6/p1.py b/puzzles/Y2015/D6/p1.py new file mode 100644 index 0000000..8ca9db0 --- /dev/null +++ b/puzzles/Y2015/D6/p1.py @@ -0,0 +1,35 @@ +import re +from typing import Any + + +def turn_on(current: bool) -> bool: + return True + + +def turn_off(current: bool) -> bool: + return False + + +def toggle(current: bool) -> bool: + return not current + + +def puzzle(input: str) -> Any: + lights = [[False for _ in range(1000)] for _ in range(1000)] + lines = [line.strip() for line in input.strip().splitlines()] + for line in lines: + for regex, function in [ + (r"turn on (\d+),(\d+) through (\d+),(\d+)", turn_on), + (r"turn off (\d+),(\d+) through (\d+),(\d+)", turn_off), + (r"toggle (\d+),(\d+) through (\d+),(\d+)", toggle), + ]: + match = re.match(regex, line) + if match: + x0, y0 = int(match.group(1)), int(match.group(2)) + x1, y1 = int(match.group(3)), int(match.group(4)) + + for x in range(x0, x1 + 1): + for y in range(y0, y1 + 1): + lights[y][x] = function(lights[y][x]) + break + return sum(sum(row) for row in lights) diff --git a/puzzles/Y2015/D6/p2.py b/puzzles/Y2015/D6/p2.py new file mode 100644 index 0000000..9e81a82 --- /dev/null +++ b/puzzles/Y2015/D6/p2.py @@ -0,0 +1,35 @@ +import re +from typing import Any + + +def turn_on(current: int) -> int: + return current + 1 + + +def turn_off(current: int) -> int: + return max(current - 1, 0) + + +def flip(current: int) -> int: + return current + 2 + + +def puzzle(input: str) -> Any: + lights = [[0 for _ in range(1000)] for _ in range(1000)] + lines = [line.strip() for line in input.strip().splitlines()] + for line in lines: + for regex, function in [ + (r"turn on (\d+),(\d+) through (\d+),(\d+)", turn_on), + (r"turn off (\d+),(\d+) through (\d+),(\d+)", turn_off), + (r"toggle (\d+),(\d+) through (\d+),(\d+)", flip), + ]: + match = re.match(regex, line) + if match: + x0, y0 = int(match.group(1)), int(match.group(2)) + x1, y1 = int(match.group(3)), int(match.group(4)) + + for x in range(x0, x1 + 1): + for y in range(y0, y1 + 1): + lights[y][x] = function(lights[y][x]) + break + return sum(sum(row) for row in lights)