Skip to content

Commit 5a0373c

Browse files
committed
Linting
1 parent e96be93 commit 5a0373c

13 files changed

+53
-49
lines changed

pyproject.toml

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ ignore = [
2828
"E731", # lambda-assignment (precludes a concise functional style)
2929
"PLC0415", # import-outside-top-level (sometimes imports in a function are necessary)
3030
"PLR2004", # magic-value-comparison (demands too many constants)
31+
"DOC201", # docstring-missing-returns (excessive documentation)
32+
"DOC402", # docstring-missing-yields (excessive documentation)
3133
]
3234
allowed-confusables = [
3335
"а", "б", "в", "г", "е", "з", "и", "к", "м", "н", "о", "р", "с", "у", "ф", "х",

year2015/aoc20.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def prime_powers_decomp(n):
1313
"""
1414
if n <= 1:
1515
yield [1]
16-
raise StopIteration
16+
return
1717
# check powers of 2
1818
powers, cur_pow = [1], 1
1919
while n % 2 == 0:

year2018/aoc06_vis.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@
3131
im = Image.new('RGB', IMG_SIZE, 'white')
3232
px = im.load()
3333
for x, y in grid:
34-
px[x - min_x, y - min_y] = color_map[grid[(x, y)]]
34+
px[x - min_x, y - min_y] = color_map[grid[x, y]]
3535

3636
im.save('aoc06.png', 'PNG')

year2018/aoc15.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ def fromstring(cls, s: str, elf_power: int = 3):
4545
power = elf_power if ch == 'E' else 3
4646
unit = Unit((i, j), ch, power=power)
4747
units.append(unit)
48-
map_[(i, j)] = unit
48+
map_[i, j] = unit
4949
else:
50-
map_[(i, j)] = ch
50+
map_[i, j] = ch
5151
return cls(
5252
height=len(lines), width=len(lines[0]), map=map_, units=units
5353
)
5454

5555
def __str__(self, *, hp: bool = False):
5656
"""Get a text representation of the map."""
5757
map_ = [
58-
''.join(str(self.map[(i, j)]) for j in range(self.width))
58+
''.join(str(self.map[i, j]) for j in range(self.width))
5959
for i in range(self.height)
6060
]
6161
if hp:

year2018/aoc22.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ def generate(self, extra: int = 0):
3131
if y == 0
3232
else y * 48271
3333
if x == 0
34-
else self.erosion[(y - 1, x)] * self.erosion[(y, x - 1)]
34+
else self.erosion[y - 1, x] * self.erosion[y, x - 1]
3535
)
36-
self.erosion[(y, x)] = (geoindex + self.depth) % 20183
37-
self.cave[(y, x)] = '.=|'[self.erosion[(y, x)] % 3]
36+
self.erosion[y, x] = (geoindex + self.depth) % 20183
37+
self.cave[y, x] = '.=|'[self.erosion[y, x] % 3]
3838

3939
def __str__(self):
4040
y_max, x_max = self.corner
@@ -44,7 +44,7 @@ def __str__(self):
4444
if (y, x) == (0, 0)
4545
else 'T'
4646
if (y, x) == self.target
47-
else self.cave[(y, x)]
47+
else self.cave[y, x]
4848
for x in range(x_max + 1)
4949
)
5050
for y in range(y_max + 1)
@@ -62,7 +62,7 @@ def adj(self, y: int, x: int) -> Iterable[Point]:
6262
def calc_risk(m: Map) -> int:
6363
y_t, x_t = m.target
6464
return sum(
65-
'.=|'.index(m.cave[(y, x)])
65+
'.=|'.index(m.cave[y, x])
6666
for y in range(y_t + 1)
6767
for x in range(x_t + 1)
6868
)

year2018/helpers.py

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ def read_puzzle(
1111
1212
The number of the puzzle is determined by the filename of the caller,
1313
or can be specified explicitly.
14+
15+
Raises:
16+
FileNotFoundError: if cannot find the puzzle file
1417
"""
1518
if number:
1619
year_dir = Path(f'year{year}')

year2018/tests/test_aoc15.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ def st_move_big():
8686
def test_state_fromstring(st_sample):
8787
assert st_sample.height == 5
8888
assert st_sample.width == 7
89-
assert st_sample.map[(0, 0)] == '#'
90-
assert st_sample.map[(1, 1)] == '.'
91-
assert st_sample.map[(1, 2)] == Unit((1, 2), 'G')
92-
assert st_sample.map[(1, 4)] == Unit((1, 4), 'E')
89+
assert st_sample.map[0, 0] == '#'
90+
assert st_sample.map[1, 1] == '.'
91+
assert st_sample.map[1, 2] == Unit((1, 2), 'G')
92+
assert st_sample.map[1, 4] == Unit((1, 4), 'E')
9393
assert st_sample.units == [
9494
Unit((1, 2), 'G'),
9595
Unit((1, 4), 'E'),

year2018/tests/test_aoc20.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
def test_parse_map():
1414
m = parse_map('^ENWWW$')
15-
assert m[(0, -1)] == '#'
16-
assert m[(0, 0)] == 'X'
17-
assert m[(0, 1)] == '|'
18-
assert m[(0, 2)] == '.'
15+
assert m[0, -1] == '#'
16+
assert m[0, 0] == 'X'
17+
assert m[0, 1] == '|'
18+
assert m[0, 2] == '.'
1919

2020

2121
samples = (

year2018/tests/test_aoc22.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ def test_map_init(sample):
1919
assert sample.depth == 510
2020
assert sample.target == (10, 10)
2121
assert len(sample.erosion.keys()) == 121
22-
assert sample.erosion[(0, 0)] == 510
23-
assert sample.erosion[(0, 1)] == 17317
24-
assert sample.erosion[(1, 0)] == 8415
25-
assert sample.erosion[(1, 1)] == 1805
26-
assert sample.erosion[(10, 10)] == 510
27-
assert sample.cave[(0, 0)] == '.'
28-
assert sample.cave[(0, 1)] == '='
29-
assert sample.cave[(1, 0)] == '.'
30-
assert sample.cave[(1, 1)] == '|'
31-
assert sample.cave[(10, 10)] == '.'
22+
assert sample.erosion[0, 0] == 510
23+
assert sample.erosion[0, 1] == 17317
24+
assert sample.erosion[1, 0] == 8415
25+
assert sample.erosion[1, 1] == 1805
26+
assert sample.erosion[10, 10] == 510
27+
assert sample.cave[0, 0] == '.'
28+
assert sample.cave[0, 1] == '='
29+
assert sample.cave[1, 0] == '.'
30+
assert sample.cave[1, 1] == '|'
31+
assert sample.cave[10, 10] == '.'
3232

3333

3434
def test_map_str(sample):

year2019/aoc13.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def read_tiles(program: Intcode) -> TileMap:
2828
tiles = {}
2929
while True:
3030
try:
31-
x, y, tile_id = [next(gen) for _ in range(3)] # noqa: UP027
32-
tiles[(x, y)] = Tile.from_index(tile_id)
31+
x, y, tile_id = [next(gen) for _ in range(3)]
32+
tiles[x, y] = Tile.from_index(tile_id)
3333
except StopIteration:
3434
return tiles
3535

@@ -39,7 +39,7 @@ def print_tiles(tiles: TileMap):
3939
height = max(y for _, y in tiles) + 1 # coords are 0-based
4040
width = max(x for x, _ in tiles) + 1
4141
line = '\n'.join(
42-
''.join(tiles[(x, y)] for x in range(width)) for y in range(height)
42+
''.join(tiles[x, y] for x in range(width)) for y in range(height)
4343
)
4444
print(line)
4545

@@ -73,7 +73,7 @@ def run_game(program: Intcode):
7373
if (x, y) == (-1, 0):
7474
score = tile_id
7575
continue
76-
tiles[(x, y)] = Tile.from_index(tile_id)
76+
tiles[x, y] = Tile.from_index(tile_id)
7777
except StopIteration:
7878
return score
7979

year2019/aoc17.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def render_tiles(tiles: TileMap) -> list[str]:
5555
height = max(y for _, y in tiles) + 1 # coords are 0-based
5656
width = max(x for x, _ in tiles) + 1
5757
return [
58-
''.join(tiles[(x, y)] for x in range(width)) for y in range(height)
58+
''.join(tiles[x, y] for x in range(width)) for y in range(height)
5959
]
6060

6161

year2019/helpers.py

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ def read_puzzle(
1111
1212
The number of the puzzle is determined by the filename of the caller,
1313
or can be specified explicitly.
14+
15+
Raises:
16+
FileNotFoundError: if cannot find the puzzle file
1417
"""
1518
if number:
1619
year_dir = Path(f'year{year}')

year2019/intcode.py

+12-16
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@ def __post_init__(self):
3030
self.program = defaultdict(lambda: 0, enumerate(self.program))
3131

3232
def __iter__(self) -> Generator[int, int, None]:
33-
"""Execute the Intcode program."""
33+
"""Execute the Intcode program.
34+
35+
Raises:
36+
UnknownOpCodeError: if encountered an unknown op code
37+
"""
3438
while True:
3539
op = self.program[self.pointer] % 100
3640
match op:
3741
case 1 | 2: # add, multiply
3842
a, b, c = self.get_parameters(3) # type: ignore
39-
if op == 1:
40-
self.program[c] = a + b
41-
else:
42-
self.program[c] = a * b
43+
self.program[c] = a + b if op == 1 else a * b
4344
self.pointer += 4
4445
case 3: # input
4546
a = self.program[self.pointer + 1]
@@ -51,19 +52,14 @@ def __iter__(self) -> Generator[int, int, None]:
5152
a: int = self.get_parameters(1) # type: ignore
5253
self.pointer += 2
5354
yield a
54-
case 5: # jump-if-true
55-
a, b = self.get_parameters(2) # type: ignore
56-
self.pointer = b if a else self.pointer + 3
57-
case 6: # jump-if-false
55+
case 5 | 6: # jump-if-true, jump-if-false
5856
a, b = self.get_parameters(2) # type: ignore
59-
self.pointer = b if not a else self.pointer + 3
60-
case 7: # less than
61-
a, b, c = self.get_parameters(3) # type: ignore
62-
self.program[c] = 1 if a < b else 0
63-
self.pointer += 4
64-
case 8: # equals
57+
cond = a if op == 5 else not a
58+
self.pointer = b if cond else self.pointer + 3
59+
case 7 | 8: # less than, equals
6560
a, b, c = self.get_parameters(3) # type: ignore
66-
self.program[c] = 1 if a == b else 0
61+
cond = a < b if op == 7 else a == b
62+
self.program[c] = 1 if cond else 0
6763
self.pointer += 4
6864
case 9: # relative base offset
6965
a: int = self.get_parameters(1) # type: ignore

0 commit comments

Comments
 (0)