Skip to content

Commit 843f9c9

Browse files
committed
ruff fixes
1 parent af748b0 commit 843f9c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+646
-582
lines changed

justfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ java_unittest_cmd := "org.junit.platform.console.ConsoleLauncher"
1717
julia_source_dir := "src/main/julia"
1818
pmd_cache_dir := ".cache/pmd"
1919
pmd_html_dir := "htmlpmd"
20+
ruff_files := env("RUFF_FILES", "src/main/python")
2021
rust_source_dir := "src/main/rust"
2122
rust_srcs := "import pathlib; print(' '.join(map(str, list(pathlib.Path('src/main/rust').rglob('*.rs')))))"
2223
rust_dst := "build/rs"
@@ -117,7 +118,7 @@ vim-file-debug file *type:
117118
# Linting: ruff check
118119
[group("linting")]
119120
ruff-check: (msg-blue "Running ruff check")
120-
@{{ ruff }} --quiet check "{{ source_dir }}"
121+
@{{ ruff }} --quiet check {{ ruff_files }}
121122

122123
# Linting: vulture - unused code
123124
[group("linting")]
@@ -127,7 +128,7 @@ vulture: (msg-blue "Running vulture")
127128
# Linting: ruff format check
128129
[group("linting")]
129130
ruff-format-check: (msg-blue "Running ruff format check")
130-
@{{ ruff }} format --quiet --check "{{ source_dir }}"
131+
@{{ ruff }} format --quiet --check {{ ruff_files }}
131132

132133
# Linting: mypy
133134
[group("linting")]
@@ -168,7 +169,7 @@ rustfmt-check: (msg-blue "Running rustfmt check against rust source files...")
168169

169170
# Linting: all
170171
[group("linting")]
171-
lint: vulture shellcheck rustfmt-check ruff-check
172+
lint: vulture shellcheck rustfmt-check ruff-check ruff-format-check
172173

173174
# Run python
174175
[group("python")]

pyproject.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,16 @@ force_single_line = true
2929
skip_gitignore = true
3030
include_trailing_comma = true
3131

32-
[tool.black]
32+
[tool.ruff]
3333
line-length = 79
3434

35+
[tool.ruff.lint]
36+
select = ["ALL"]
37+
ignore = ["COM", "D1", "PLR2004", "PT", "S101", "T20"]
38+
39+
[tool.ruff.lint.isort]
40+
force-single-line = true
41+
3542
[tool.mypy]
3643
strict = true
3744

src/main/python/AoC2015_01.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,19 @@
2828

2929
class Solution(SolutionBase[Input, Output1, Output2]):
3030
def parse_input(self, input_data: InputData) -> Input:
31-
return list(input_data)[0]
31+
return next(iter(input_data))
3232

33-
def part_1(self, input: Input) -> Output1:
34-
return len(input) - 2 * input.count(")")
33+
def part_1(self, string: Input) -> Output1:
34+
return len(string) - 2 * string.count(")")
3535

36-
def part_2(self, input: Input) -> Output2:
36+
def part_2(self, string: Input) -> Output2:
3737
sum_ = 0
38-
for i, c in enumerate(input):
38+
for i, c in enumerate(string):
3939
sum_ += 1 if c == "(" else -1
4040
if sum_ == -1:
4141
return i + 1
42-
raise RuntimeError("Unreachable")
42+
msg = "Unreachable"
43+
raise RuntimeError(msg)
4344

4445
@aoc_samples(
4546
(

src/main/python/AoC2015_03.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626

2727
class Solution(SolutionBase[Input, Output1, Output2]):
2828
def parse_input(self, input_data: InputData) -> Input:
29-
return [Heading.from_str(ch) for ch in list(input_data)[0]]
29+
return [Heading.from_str(ch) for ch in next(iter(input_data))]
3030

3131
def count_unique_positions(self, positions: list[Position]) -> int:
32-
return len({p for p in positions})
32+
return len(set(positions))
3333

3434
def add_navigation_instruction(
3535
self, navigation: Navigation, c: Heading
@@ -41,19 +41,19 @@ def part_1(self, inputs: Input) -> Output1:
4141
for c in inputs:
4242
self.add_navigation_instruction(navigation, c)
4343
return self.count_unique_positions(
44-
navigation.get_visited_positions(True)
44+
navigation.get_visited_positions(include_start_position=True)
4545
)
4646

4747
def part_2(self, inputs: Input) -> Output2:
48-
santa_navigation = Navigation(START, Heading.NORTH)
49-
robot_navigation = Navigation(START, Heading.NORTH)
48+
santa_nav = Navigation(START, Heading.NORTH)
49+
robot_nav = Navigation(START, Heading.NORTH)
5050
for i, c in enumerate(inputs):
5151
self.add_navigation_instruction(
52-
robot_navigation if i % 2 else santa_navigation, c
52+
robot_nav if i % 2 else santa_nav, c
5353
)
5454
return self.count_unique_positions(
55-
santa_navigation.get_visited_positions(True)
56-
+ robot_navigation.get_visited_positions(True)
55+
santa_nav.get_visited_positions(include_start_position=True)
56+
+ robot_nav.get_visited_positions(include_start_position=True)
5757
)
5858

5959
@aoc_samples(

src/main/python/AoC2015_04.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
class Solution(SolutionBase[Input, Output1, Output2]):
2020
def parse_input(self, input_data: InputData) -> Input:
21-
return list(input_data)[0]
21+
return next(iter(input_data))
2222

2323
def find_md5_starting_with_zeroes(self, seed: str, zeroes: int) -> int:
2424
i = 0
@@ -28,7 +28,7 @@ def find_md5_starting_with_zeroes(self, seed: str, zeroes: int) -> int:
2828
i += 1
2929
spinner(i)
3030
str2hash = seed + str(i)
31-
val = md5(str2hash.encode()).hexdigest() # nosec
31+
val = md5(str2hash.encode()).hexdigest() # noqa: S324
3232
return i
3333

3434
def part_1(self, seed: Input) -> Output1:

src/main/python/AoC2015_06.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,39 +45,29 @@ def from_string(cls, action: str) -> Action:
4545
case _:
4646
return Action.TOGGLE
4747

48-
def apply(
48+
def apply( # noqa: C901
4949
self, lights: list[list[int]], start: Cell, end: Cell, mode: Mode
5050
) -> None:
51-
match self:
52-
case Action.TURN_ON:
53-
match mode:
54-
case Mode.MODE_1:
55-
for r in range(start.row, end.row + 1):
56-
for c in range(start.col, end.col + 1):
51+
for r in range(start.row, end.row + 1):
52+
for c in range(start.col, end.col + 1):
53+
match self:
54+
case Action.TURN_ON:
55+
match mode:
56+
case Mode.MODE_1:
5757
lights[r][c] = 1
58-
case Mode.MODE_2:
59-
for r in range(start.row, end.row + 1):
60-
for c in range(start.col, end.col + 1):
58+
case Mode.MODE_2:
6159
lights[r][c] += 1
62-
case Action.TURN_OFF:
63-
match mode:
64-
case Mode.MODE_1:
65-
for r in range(start.row, end.row + 1):
66-
for c in range(start.col, end.col + 1):
60+
case Action.TURN_OFF:
61+
match mode:
62+
case Mode.MODE_1:
6763
lights[r][c] = 0
68-
case Mode.MODE_2:
69-
for r in range(start.row, end.row + 1):
70-
for c in range(start.col, end.col + 1):
64+
case Mode.MODE_2:
7165
lights[r][c] = max(lights[r][c] - 1, 0)
72-
case Action.TOGGLE:
73-
match mode:
74-
case Mode.MODE_1:
75-
for r in range(start.row, end.row + 1):
76-
for c in range(start.col, end.col + 1):
66+
case Action.TOGGLE:
67+
match mode:
68+
case Mode.MODE_1:
7769
lights[r][c] = 0 if lights[r][c] == 1 else 1
78-
case Mode.MODE_2:
79-
for r in range(start.row, end.row + 1):
80-
for c in range(start.col, end.col + 1):
70+
case Mode.MODE_2:
8171
lights[r][c] += 2
8272

8373

src/main/python/AoC2015_07.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,15 @@ def parse_input(self, input_data: InputData) -> Input:
3939
if first.isnumeric():
4040
wires[second] = int(first)
4141
continue
42-
else:
43-
in1, op, in2 = splits[0], "SET", None
42+
in1, op, in2 = splits[0], "SET", None
4443
elif len(splits) == 2:
4544
in1, op, in2 = splits[1], "NOT", None
4645
else:
4746
in1, op, in2 = splits[0], splits[1], splits[2]
4847
gates.append((in1, op, in2, second))
4948
return wires, gates
5049

51-
def solve(self, wires: Wires, gates: list[Gate], wire: str) -> int:
50+
def solve(self, wires: Wires, gates: list[Gate], wire: str) -> int: # noqa: C901
5251
def exec_op(in1: str, op: str, in2: str | None, out: str) -> None:
5352
match op:
5453
case "SET":
@@ -84,12 +83,12 @@ def exec_op(in1: str, op: str, in2: str | None, out: str) -> None:
8483
q.append((in1, op, in2, out))
8584
return wires[wire]
8685

87-
def part_1(self, input: Input) -> Output1:
88-
wires, gates = input
86+
def part_1(self, inputs: Input) -> Output1:
87+
wires, gates = inputs
8988
return self.solve(wires.copy(), gates, "a")
9089

91-
def part_2(self, input: Input) -> Output2:
92-
wires, gates = input
90+
def part_2(self, inputs: Input) -> Output2:
91+
wires, gates = inputs
9392
wires_2 = wires.copy()
9493
wires_2["b"] = self.solve(wires.copy(), gates, "a")
9594
return self.solve(wires_2, gates, "a")

src/main/python/AoC2015_08.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ class Solution(SolutionBase[Input, Output1, Output2]):
4747
def parse_input(self, input_data: InputData) -> Input:
4848
return input_data
4949

50-
def solve(self, input: Input, mode: Mode) -> int:
51-
return sum(mode.overhead(s) for s in input)
50+
def solve(self, inputs: Input, mode: Mode) -> int:
51+
return sum(mode.overhead(s) for s in inputs)
5252

53-
def part_1(self, input: Input) -> Output1:
54-
return self.solve(input, Mode.DECODE)
53+
def part_1(self, inputs: Input) -> Output1:
54+
return self.solve(inputs, Mode.DECODE)
5555

56-
def part_2(self, input: Input) -> Output2:
57-
return self.solve(input, Mode.ENCODE)
56+
def part_2(self, inputs: Input) -> Output2:
57+
return self.solve(inputs, Mode.ENCODE)
5858

5959
@aoc_samples(
6060
(

src/main/python/AoC2015_09.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
import itertools
99
import sys
1010
from collections import defaultdict
11-
from typing import Iterator
11+
from typing import TYPE_CHECKING
1212
from typing import NamedTuple
1313

14+
if TYPE_CHECKING:
15+
from collections.abc import Iterator
16+
1417
from aoc.common import InputData
1518
from aoc.common import SolutionBase
1619
from aoc.common import aoc_samples

src/main/python/AoC2015_10.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111

112112

113113
class Solution(SolutionBase[Input, Output1, Output2]):
114-
"""https://github.com/maneatingape/advent-of-code-rust/blob/3a95adb336f4e9af3ca509b17aa60d6360510290/src/year2015/day10.rs""" # noqa E501
114+
"""https://github.com/maneatingape/advent-of-code-rust/blob/3a95adb336f4e9af3ca509b17aa60d6360510290/src/year2015/day10.rs."""
115115

116116
def parse_input(self, input_data: InputData) -> Input:
117117
elements = [line.split() for line in ELEMENTS.splitlines()]
@@ -121,7 +121,7 @@ def parse_input(self, input_data: InputData) -> Input:
121121
for i, sp in enumerate(elements):
122122
sequence[i] = sp[0]
123123
decays[i] = [indices[x] for x in sp[4:]]
124-
return sequence, decays, list(input_data)[0]
124+
return sequence, decays, next(iter(input_data))
125125

126126
def _solve(self, string: str, iterations: int) -> str:
127127
current = string
@@ -139,17 +139,17 @@ def _solve(self, string: str, iterations: int) -> str:
139139
return current
140140

141141
def solve(self, inputs: Input, iterations: int) -> int:
142-
sequence, decays, input = inputs
142+
sequence, decays, input_ = inputs
143143
current = [0 for _ in range(N)]
144-
current[sequence.index(input)] = 1
144+
current[sequence.index(input_)] = 1
145145
for _ in range(iterations):
146146
nxt = [0 for _ in range(N)]
147147
for i, c in enumerate(current):
148148
if c > 0:
149149
for d in decays[i]:
150150
nxt[d] += c
151151
current = nxt
152-
return sum(c * len(s) for c, s in zip(current, sequence))
152+
return sum(c * len(s) for c, s in zip(current, sequence, strict=True))
153153

154154
def part_1(self, inputs: Input) -> Output1:
155155
return self.solve(inputs, iterations=40)

0 commit comments

Comments
 (0)