Solve 2018 day 16 and add function to download input
This commit is contained in:
58
lib.py
58
lib.py
@@ -11,20 +11,25 @@ INF = float("inf")
|
||||
fst = lambda l: l[0]
|
||||
snd = lambda l: l[1]
|
||||
|
||||
|
||||
def nth(n):
|
||||
return lambda l: l[n]
|
||||
|
||||
|
||||
def maps(f, xs):
|
||||
if isinstance(xs, list):
|
||||
return [maps(f, x) for x in xs]
|
||||
return f(xs)
|
||||
|
||||
|
||||
def mape(f, xs):
|
||||
return list(map(f, xs))
|
||||
|
||||
|
||||
def add2(a: tuple[int, int], b: tuple[int, int]) -> tuple[int, int]:
|
||||
return (a[0] + b[0], a[1] + b[1])
|
||||
|
||||
|
||||
class Grid2D:
|
||||
N = (-1, 0)
|
||||
E = (0, 1)
|
||||
@@ -55,8 +60,7 @@ class Grid2D:
|
||||
c = Grid2D("d\nd")
|
||||
c.n_rows = self.n_rows
|
||||
c.n_cols = self.n_cols
|
||||
c.grid = [[val for _ in range(c.n_cols)]
|
||||
for _ in range(self.n_rows)]
|
||||
c.grid = [[val for _ in range(c.n_cols)] for _ in range(self.n_rows)]
|
||||
return c
|
||||
|
||||
def rows(self) -> list[list[str]]:
|
||||
@@ -64,8 +68,7 @@ class Grid2D:
|
||||
|
||||
def cols(self) -> list[list[str]]:
|
||||
rows = self.rows()
|
||||
return [[row[col_i] for row in rows]
|
||||
for col_i in range(self.n_cols)]
|
||||
return [[row[col_i] for row in rows] for col_i in range(self.n_cols)]
|
||||
|
||||
def find(self, chars: str) -> list[tuple[int, int]]:
|
||||
return [c for c in self.all_coords() if self[c] in chars]
|
||||
@@ -74,9 +77,11 @@ class Grid2D:
|
||||
return [c for c in self.all_coords() if self[c] not in chars]
|
||||
|
||||
def all_coords(self) -> list[tuple[int, int]]:
|
||||
return [(row_i, col_i)
|
||||
for row_i in range(self.n_rows)
|
||||
for col_i in range(self.n_cols)]
|
||||
return [
|
||||
(row_i, col_i)
|
||||
for row_i in range(self.n_rows)
|
||||
for col_i in range(self.n_cols)
|
||||
]
|
||||
|
||||
def row_coords(self, row_i) -> list[tuple[int, int]]:
|
||||
assert row_i < self.n_rows, f"{row_i=} must be smaller than {self.n_rows=}"
|
||||
@@ -94,10 +99,14 @@ class Grid2D:
|
||||
return self.contains(pos)
|
||||
|
||||
def neighbors_ort(self, pos: tuple[int, int]) -> list[tuple[int, int]]:
|
||||
return [add2(pos, off) for off in self.dirs_ort() if self.contains(add2(pos, off))]
|
||||
return [
|
||||
add2(pos, off) for off in self.dirs_ort() if self.contains(add2(pos, off))
|
||||
]
|
||||
|
||||
def neighbors_vert(self, pos: tuple[int, int]) -> list[tuple[int, int]]:
|
||||
return [add2(pos, off) for off in self.dirs_vert() if self.contains(add2(pos, off))]
|
||||
return [
|
||||
add2(pos, off) for off in self.dirs_vert() if self.contains(add2(pos, off))
|
||||
]
|
||||
|
||||
def neighbors_adj(self, pos: tuple[int, int]) -> list[tuple[int, int]]:
|
||||
return self.neighbors_ort(pos) + self.neighbors_vert(pos)
|
||||
@@ -119,6 +128,7 @@ class Grid2D:
|
||||
for r in self.rows():
|
||||
print(" ".join(map(str, r)))
|
||||
|
||||
|
||||
class Input:
|
||||
def __init__(self, text: str):
|
||||
if os.path.isfile(text):
|
||||
@@ -141,6 +151,7 @@ class Input:
|
||||
def grid2(self) -> Grid2D:
|
||||
return Grid2D(self.text)
|
||||
|
||||
|
||||
def prime_factors(n):
|
||||
"""
|
||||
Returns a list of prime factors for n.
|
||||
@@ -163,6 +174,7 @@ def prime_factors(n):
|
||||
factors.append(rest)
|
||||
return factors
|
||||
|
||||
|
||||
def lcm(numbers: list[int]) -> int:
|
||||
fs = []
|
||||
for n in numbers:
|
||||
@@ -173,6 +185,7 @@ def lcm(numbers: list[int]) -> int:
|
||||
s *= f
|
||||
return s
|
||||
|
||||
|
||||
def str_to_int(line: str) -> int:
|
||||
line = line.replace(" ", "")
|
||||
r = re.compile(r"(-?\d+)")
|
||||
@@ -180,16 +193,20 @@ def str_to_int(line: str) -> int:
|
||||
(x,) = m
|
||||
return int(x)
|
||||
|
||||
|
||||
def str_to_ints(line: str) -> list[int]:
|
||||
r = re.compile(r"-?\d+")
|
||||
return list(map(int, r.findall(line)))
|
||||
|
||||
|
||||
def str_to_lines_no_empty(text: str) -> list[str]:
|
||||
return list(filter(lambda l: l.strip() != "", text.splitlines()))
|
||||
|
||||
|
||||
def str_to_lines(text: str) -> list[str]:
|
||||
return list(text.splitlines())
|
||||
|
||||
|
||||
def count_trailing_repeats(lst):
|
||||
count = 0
|
||||
for elem in reversed(lst):
|
||||
@@ -199,6 +216,7 @@ def count_trailing_repeats(lst):
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
class A_Star(object):
|
||||
def __init__(self, starts, is_goal, h, d, neighbors):
|
||||
"""
|
||||
@@ -221,12 +239,12 @@ class A_Star(object):
|
||||
|
||||
for neighbor in neighbors(current):
|
||||
tentative_g_score = g_score[current] + d(current, neighbor)
|
||||
if neighbor not in g_score or \
|
||||
tentative_g_score < g_score[neighbor]:
|
||||
if neighbor not in g_score or tentative_g_score < g_score[neighbor]:
|
||||
g_score[neighbor] = tentative_g_score
|
||||
f_score = g_score[neighbor] + h(neighbor)
|
||||
heapq.heappush(open_set, (f_score, neighbor))
|
||||
|
||||
|
||||
def shoelace_area(corners):
|
||||
n = len(corners)
|
||||
area = 0
|
||||
@@ -236,7 +254,25 @@ def shoelace_area(corners):
|
||||
area += (x1 * y2) - (x2 * y1)
|
||||
return abs(area) / 2.0
|
||||
|
||||
|
||||
def extract_year_and_date(scriptname) -> tuple[str, str]:
|
||||
r = re.compile(r"aoc(\d\d\d\d)/d(\d+).py")
|
||||
[(year, day)] = r.findall(scriptname)
|
||||
return (year, day)
|
||||
|
||||
|
||||
def get_data(filename):
|
||||
path, file = os.path.split(filename)
|
||||
year = path[-4:]
|
||||
day = file.replace("d", "").replace(".py", "")
|
||||
txt_file = f"d{day}.txt"
|
||||
|
||||
if os.path.isfile(txt_file):
|
||||
with open(txt_file) as f:
|
||||
return f.read()
|
||||
else:
|
||||
import subprocess
|
||||
subprocess.call(["../get.py", year, day])
|
||||
assert os.path.isfile(txt_file), "Could not download AoC file"
|
||||
with open(txt_file) as f:
|
||||
return f.read()
|
||||
|
||||
Reference in New Issue
Block a user