-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6c2031
commit 0ca7d51
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |