Skip to content

Commit

Permalink
Upgrade python tools and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
pareronia committed Nov 28, 2024
1 parent 1090db8 commit 7002b7c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 56 deletions.
11 changes: 5 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
advent-of-code-data==1.3.2
advent-of-code-ocr==1.0.0
bandit[toml]==1.7.8
flake8==7.0.0
ipython==8.23.0
bandit[toml]==1.8.0
flake8==7.1.1
ipython==8.29.0
isort==5.13.2
junitparser==3.1.2
numpy==1.26.4
junitparser==3.2.0
prettyprinter==0.18.0
vulture==2.11
vulture==2.13
99 changes: 49 additions & 50 deletions src/main/python/AoC2015_06.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
from __future__ import annotations

from enum import Enum
from typing import Callable, NamedTuple
from typing import Callable
from typing import NamedTuple

import aocd
import numpy as np
import numpy.typing as npt

from aoc import my_aocd
from aoc.geometry import Position
from aoc.grid import Cell


class Action(Enum):
Expand All @@ -24,8 +22,8 @@ class Action(Enum):

class Instruction(NamedTuple):
action: Action
start: Position
end: Position
start: Cell
end: Cell

@classmethod
def from_input(cls, input_: str) -> Instruction:
Expand All @@ -42,20 +40,20 @@ def from_input(cls, input_: str) -> Instruction:
else:
raise ValueError("Invalid input")
start_splits = action_and_start_splits[1].split(",")
start = Position.of(int(start_splits[0]), int(start_splits[1]))
start = Cell(int(start_splits[0]), int(start_splits[1]))
end_splits = splits[1].split(",")
end = Position.of(int(end_splits[0]), int(end_splits[1]))
end = Cell(int(end_splits[0]), int(end_splits[1]))
return Instruction(action, start, end)


class Grid:
def __init__(
self,
turn_on: Callable[[npt.NDArray[np.int_], Position, Position], None],
turn_off: Callable[[npt.NDArray[np.int_], Position, Position], None],
toggle: Callable[[npt.NDArray[np.int_], Position, Position], None],
turn_on: Callable[[list[list[int]], Cell, Cell], None],
turn_off: Callable[[list[list[int]], Cell, Cell], None],
toggle: Callable[[list[list[int]], Cell, Cell], None],
):
self.lights = np.zeros((1000, 1000), np.byte)
self.lights = [[0 for _ in range(1000)] for _ in range(1000)]
self.turn_on = turn_on
self.turn_off = turn_off
self.toggle = toggle
Expand All @@ -65,39 +63,41 @@ def process_instructions(self, instructions: list[Instruction]) -> None:
action = (
self.turn_on
if instruction.action == Action.TURN_ON
else self.turn_off
if instruction.action == Action.TURN_OFF
else self.toggle
else (
self.turn_off
if instruction.action == Action.TURN_OFF
else self.toggle
)
)
action(self.lights, instruction.start, instruction.end)

def get_total_light_value(self) -> int:
return int(np.sum(self.lights))
return sum(
self.lights[r][c]
for r in range(len(self.lights))
for c in range(len(self.lights[0]))
)


def _parse(inputs: tuple[str, ...]) -> list[Instruction]:
return [Instruction.from_input(line) for line in inputs]


def part_1(inputs: tuple[str, ...]) -> int:
def turn_on(
lights: npt.NDArray[np.int_], start: Position, end: Position
) -> None:
lights[start.x : end.x + 1, start.y : end.y + 1] = 1 # noqa E203

def turn_off(
lights: npt.NDArray[np.int_], start: Position, end: Position
) -> None:
lights[start.x : end.x + 1, start.y : end.y + 1] = 0 # noqa E203

def toggle(
lights: npt.NDArray[np.int_], start: Position, end: Position
) -> None:
lights[
start.x : end.x + 1, start.y : end.y + 1 # noqa E203
] = np.logical_not(
lights[start.x : end.x + 1, start.y : end.y + 1] # noqa E203
)
def turn_on(lights: list[list[int]], start: Cell, end: Cell) -> None:
for r in range(start.row, end.row + 1):
for c in range(start.col, end.col + 1):
lights[r][c] = 1

def turn_off(lights: list[list[int]], start: Cell, end: Cell) -> None:
for r in range(start.row, end.row + 1):
for c in range(start.col, end.col + 1):
lights[r][c] = 0

def toggle(lights: list[list[int]], start: Cell, end: Cell) -> None:
for r in range(start.row, end.row + 1):
for c in range(start.col, end.col + 1):
lights[r][c] = 0 if lights[r][c] == 1 else 1

lights = Grid(
lambda lights, start, end: turn_on(lights, start, end),
Expand All @@ -109,21 +109,20 @@ def toggle(


def part_2(inputs: tuple[str, ...]) -> int:
def turn_on(
lights: npt.NDArray[np.int_], start: Position, end: Position
) -> None:
lights[start.x : end.x + 1, start.y : end.y + 1] += 1 # noqa E203

def turn_off(
lights: npt.NDArray[np.int_], start: Position, end: Position
) -> None:
lights[start.x : end.x + 1, start.y : end.y + 1] -= 1 # noqa E203
lights[lights < 0] = 0

def toggle(
lights: npt.NDArray[np.int_], start: Position, end: Position
) -> None:
lights[start.x : end.x + 1, start.y : end.y + 1] += 2 # noqa E203
def turn_on(lights: list[list[int]], start: Cell, end: Cell) -> None:
for r in range(start.row, end.row + 1):
for c in range(start.col, end.col + 1):
lights[r][c] += 1

def turn_off(lights: list[list[int]], start: Cell, end: Cell) -> None:
for r in range(start.row, end.row + 1):
for c in range(start.col, end.col + 1):
lights[r][c] = max(lights[r][c] - 1, 0)

def toggle(lights: list[list[int]], start: Cell, end: Cell) -> None:
for r in range(start.row, end.row + 1):
for c in range(start.col, end.col + 1):
lights[r][c] += 2

lights = Grid(
lambda lights, start, end: turn_on(lights, start, end),
Expand Down

0 comments on commit 7002b7c

Please sign in to comment.