Refactor code for API (#3)
Co-authored-by: Mikael CAPELLE <mikael.capelle@thalesaleniaspace.com> Co-authored-by: Mikaël Capelle <capelle.mikael@gmail.com> Reviewed-on: #3
This commit is contained in:
@@ -1,33 +1,32 @@
|
||||
import sys
|
||||
from typing import Literal, cast
|
||||
from typing import Any, Iterator, Literal, cast
|
||||
|
||||
import numpy as np
|
||||
import parse # type: ignore
|
||||
|
||||
lines = sys.stdin.read().splitlines()
|
||||
from ..base import BaseSolver
|
||||
|
||||
lights_1 = np.zeros((1000, 1000), dtype=bool)
|
||||
lights_2 = np.zeros((1000, 1000), dtype=int)
|
||||
for line in lines:
|
||||
action, sx, sy, ex, ey = cast(
|
||||
tuple[Literal["turn on", "turn off", "toggle"], int, int, int, int],
|
||||
parse.parse("{} {:d},{:d} through {:d},{:d}", line), # type: ignore
|
||||
)
|
||||
ex, ey = ex + 1, ey + 1
|
||||
|
||||
match action:
|
||||
case "turn on":
|
||||
lights_1[sx:ex, sy:ey] = True
|
||||
lights_2[sx:ex, sy:ey] += 1
|
||||
case "turn off":
|
||||
lights_1[sx:ex, sy:ey] = False
|
||||
lights_2[sx:ex, sy:ey] = np.maximum(lights_2[sx:ex, sy:ey] - 1, 0)
|
||||
case "toggle":
|
||||
lights_1[sx:ex, sy:ey] = ~lights_1[sx:ex, sy:ey]
|
||||
lights_2[sx:ex, sy:ey] += 2
|
||||
class Solver(BaseSolver):
|
||||
def solve(self, input: str) -> Iterator[Any]:
|
||||
lights_1 = np.zeros((1000, 1000), dtype=bool)
|
||||
lights_2 = np.zeros((1000, 1000), dtype=int)
|
||||
for line in input.splitlines():
|
||||
action, sx, sy, ex, ey = cast(
|
||||
tuple[Literal["turn on", "turn off", "toggle"], int, int, int, int],
|
||||
parse.parse("{} {:d},{:d} through {:d},{:d}", line), # type: ignore
|
||||
)
|
||||
ex, ey = ex + 1, ey + 1
|
||||
|
||||
answer_1 = lights_1.sum()
|
||||
print(f"answer 1 is {answer_1}")
|
||||
match action:
|
||||
case "turn on":
|
||||
lights_1[sx:ex, sy:ey] = True
|
||||
lights_2[sx:ex, sy:ey] += 1
|
||||
case "turn off":
|
||||
lights_1[sx:ex, sy:ey] = False
|
||||
lights_2[sx:ex, sy:ey] = np.maximum(lights_2[sx:ex, sy:ey] - 1, 0)
|
||||
case "toggle":
|
||||
lights_1[sx:ex, sy:ey] = ~lights_1[sx:ex, sy:ey]
|
||||
lights_2[sx:ex, sy:ey] += 2
|
||||
|
||||
answer_2 = lights_2.sum()
|
||||
print(f"answer 2 is {answer_2}")
|
||||
yield lights_1.sum()
|
||||
yield lights_2.sum()
|
||||
|
Reference in New Issue
Block a user