-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
47 lines (33 loc) · 1.06 KB
/
utils.py
File metadata and controls
47 lines (33 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
class Matrix:
def __init__(self, values):
self.values = values
widths = set(len(line) for line in values)
if len(widths) != 1:
raise ValueError(f"inconsistent line sizes: {widths}")
self.height = len(values)
self.width = next(iter(widths))
def __str__(self):
return "\n".join("".join(str(c) for c in line) for line in self.values)
def set_value_at(self, x, y, value):
self.values[y][x] = value
def value_at(self, x, y):
return self.values[y][x]
def neighbours(self, x, y):
if x > 0:
yield Point(x - 1, y)
if x < self.width - 1:
yield Point(x + 1, y)
if y > 0:
yield Point(x, y - 1)
if y < self.height - 1:
yield Point(x, y + 1)
def test_input():
return open("input.txt")
def input():
return open("input-2.txt")
def read_matrix(f):
return Matrix([[int(c) for c in line.strip()] for line in f.readlines()])