Skip to content

Commit

Permalink
2015 day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
Spacerulerwill committed Jan 8, 2025
1 parent a6c2031 commit 0ca7d51
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
35 changes: 35 additions & 0 deletions puzzles/Y2015/D6/p1.py
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 35 additions & 0 deletions puzzles/Y2015/D6/p2.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 0ca7d51

Please sign in to comment.