Reformat code
This commit is contained in:
parent
3d46f259ca
commit
2096f0eeaa
6 changed files with 49 additions and 32 deletions
|
@ -56,11 +56,13 @@ def two(inp: str) -> int:
|
|||
for line in parsed:
|
||||
first_digit = find_first(line, search_values)
|
||||
second_digit = find_first("".join(reversed(line)), reversed_search_values)
|
||||
assert first_digit is not None and second_digit is not None, f"must have at least one digit per line: {line}"
|
||||
assert (
|
||||
first_digit is not None and second_digit is not None
|
||||
), f"must have at least one digit per line: {line}"
|
||||
|
||||
# second digit will be the reversed form
|
||||
second_digit = "".join(reversed(second_digit))
|
||||
|
||||
|
||||
first_digit = TRANSFORMATIONS.get(first_digit, first_digit)
|
||||
second_digit = TRANSFORMATIONS.get(second_digit, second_digit)
|
||||
|
||||
|
|
|
@ -23,11 +23,13 @@ class Card:
|
|||
|
||||
def calculate_score(self) -> int:
|
||||
n = len(self.get_winning_numbers())
|
||||
score = 0 if n == 0 else 2**(n-1)
|
||||
score = 0 if n == 0 else 2 ** (n - 1)
|
||||
return score
|
||||
|
||||
def get_prize_copies(self) -> set[int]:
|
||||
return set(map(lambda x: self.id + x + 1, range(len(self.get_winning_numbers()))))
|
||||
return set(
|
||||
map(lambda x: self.id + x + 1, range(len(self.get_winning_numbers())))
|
||||
)
|
||||
|
||||
|
||||
def make_card_counter(all_cards: list[Card]) -> Callable[[Card], int]:
|
||||
|
@ -35,11 +37,12 @@ def make_card_counter(all_cards: list[Card]) -> Callable[[Card], int]:
|
|||
def fn(c: Card) -> int:
|
||||
acc = 1
|
||||
won = c.get_prize_copies()
|
||||
|
||||
|
||||
for won_card in won:
|
||||
acc += fn(all_cards[won_card-1])
|
||||
acc += fn(all_cards[won_card - 1])
|
||||
|
||||
return acc
|
||||
|
||||
return fn
|
||||
|
||||
|
||||
|
@ -48,7 +51,7 @@ def parse(instr: str) -> list[Card]:
|
|||
|
||||
for line in instr.splitlines():
|
||||
card_decl, numbers_sect = line.split(": ")
|
||||
|
||||
|
||||
card = Card(int(card_decl.lstrip("Card ")))
|
||||
|
||||
winning, present = numbers_sect.split("|")
|
||||
|
@ -56,7 +59,7 @@ def parse(instr: str) -> list[Card]:
|
|||
card.present = set([int(x) for x in present.split(" ") if x != ""])
|
||||
|
||||
res.append(card)
|
||||
|
||||
|
||||
return res
|
||||
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ class Transition:
|
|||
self.dest_start = dest_start
|
||||
self.src_start = src_start
|
||||
self.n = n
|
||||
|
||||
|
||||
def get_delta(self) -> int:
|
||||
return self.dest_start - self.src_start
|
||||
|
||||
|
@ -79,14 +79,20 @@ def resolve(x: int, level: str, state_transitions, transition_functions) -> int:
|
|||
return x
|
||||
|
||||
|
||||
def apply_level(transition_functions: dict[str, list[Transition]], level: str, val: int) -> int:
|
||||
def apply_level(
|
||||
transition_functions: dict[str, list[Transition]], level: str, val: int
|
||||
) -> int:
|
||||
for transition in transition_functions[level]:
|
||||
if transition.is_applicable_to(val):
|
||||
return transition.get_next_value(val)
|
||||
return val
|
||||
|
||||
|
||||
def apply_transitions(seeds: list[int], state_transitions: dict[str, str], transition_functions: dict[str, list[Transition]]) -> list[int]:
|
||||
def apply_transitions(
|
||||
seeds: list[int],
|
||||
state_transitions: dict[str, str],
|
||||
transition_functions: dict[str, list[Transition]],
|
||||
) -> list[int]:
|
||||
res = []
|
||||
|
||||
for item_id in seeds:
|
||||
|
@ -94,7 +100,7 @@ def apply_transitions(seeds: list[int], state_transitions: dict[str, str], trans
|
|||
while item_type != "location":
|
||||
item_id = apply_level(transition_functions, item_type, item_id)
|
||||
item_type = state_transitions[item_type]
|
||||
|
||||
|
||||
res.append(item_id)
|
||||
|
||||
return res
|
||||
|
@ -108,9 +114,11 @@ def two(instr: str):
|
|||
seeds, state_transitions, transition_functions = parse(instr)
|
||||
assert len(seeds) % 2 == 0
|
||||
|
||||
inverted_transitions = {(k := tuple(reversed(x)))[0]: k[1] for x in state_transitions.items()}
|
||||
inverted_transitions = {
|
||||
(k := tuple(reversed(x)))[0]: k[1] for x in state_transitions.items()
|
||||
}
|
||||
|
||||
end_stop = max(seeds[i-1]+seeds[i] for i in range(1, len(seeds), 2))
|
||||
end_stop = max(seeds[i - 1] + seeds[i] for i in range(1, len(seeds), 2))
|
||||
|
||||
for i in range(0, end_stop):
|
||||
c = i
|
||||
|
@ -124,7 +132,7 @@ def two(instr: str):
|
|||
|
||||
for x in range(0, len(seeds), 2):
|
||||
start = seeds[x]
|
||||
end = seeds[x+1] + start - 1
|
||||
end = seeds[x + 1] + start - 1
|
||||
|
||||
if start <= c <= end:
|
||||
return i
|
||||
|
|
|
@ -13,7 +13,7 @@ def get_term(start: list[int], reduction_fn: Callable[[int, list[int]], int]) ->
|
|||
cs = seqs[-1]
|
||||
x = []
|
||||
for i in range(len(cs) - 1):
|
||||
x.append(cs[i+1] - cs[i])
|
||||
x.append(cs[i + 1] - cs[i])
|
||||
seqs.append(x)
|
||||
return reduce(reduction_fn, seqs[-2::-1], 0)
|
||||
|
||||
|
@ -23,7 +23,7 @@ def run(instr: str, reduction_fn: Callable[[int, list[int]], int]):
|
|||
acc = 0
|
||||
for sq in sequences:
|
||||
acc += get_term(sq, reduction_fn)
|
||||
return acc
|
||||
return acc
|
||||
|
||||
|
||||
def one(instr: str):
|
||||
|
@ -47,4 +47,4 @@ if __name__ == "__main__":
|
|||
if sys.argv[1] == "1":
|
||||
print(one(inp))
|
||||
else:
|
||||
print(two(inp))
|
||||
print(two(inp))
|
||||
|
|
|
@ -12,10 +12,10 @@ def parse(instr: str) -> tuple[dict[Coordinate, str], Coordinate]:
|
|||
for y, line in enumerate(instr.splitlines()):
|
||||
for x, char in enumerate(line):
|
||||
coord = (x, y)
|
||||
|
||||
|
||||
if char == "S":
|
||||
s_loc = coord
|
||||
|
||||
|
||||
res[coord] = char
|
||||
|
||||
assert s_loc is not None
|
||||
|
@ -53,9 +53,7 @@ acceptable_moves["7"] = {
|
|||
(c := (-1, 0)): acceptable_moves["-"][c],
|
||||
}
|
||||
|
||||
acceptable_moves["S"] = {
|
||||
**acceptable_moves["|"], **acceptable_moves["-"]
|
||||
}
|
||||
acceptable_moves["S"] = {**acceptable_moves["|"], **acceptable_moves["-"]}
|
||||
|
||||
|
||||
def apply_coord_delta(a: Coordinate, b: Coordinate) -> Coordinate:
|
||||
|
@ -64,7 +62,9 @@ def apply_coord_delta(a: Coordinate, b: Coordinate) -> Coordinate:
|
|||
return aa + ba, ab + bb
|
||||
|
||||
|
||||
def check_coord(grid: dict[Coordinate, str], coord: Coordinate, vals: list[str]) -> bool:
|
||||
def check_coord(
|
||||
grid: dict[Coordinate, str], coord: Coordinate, vals: list[str]
|
||||
) -> bool:
|
||||
v = grid.get(coord)
|
||||
if v is None:
|
||||
return False
|
||||
|
@ -75,14 +75,16 @@ def check_coord(grid: dict[Coordinate, str], coord: Coordinate, vals: list[str])
|
|||
return False
|
||||
|
||||
|
||||
def get_loop_boundary(grid: dict[Coordinate, str], start: Coordinate) -> dict[Coordinate, int]:
|
||||
def get_loop_boundary(
|
||||
grid: dict[Coordinate, str], start: Coordinate
|
||||
) -> dict[Coordinate, int]:
|
||||
visited = {start: 0}
|
||||
frontier = [start]
|
||||
|
||||
while frontier:
|
||||
coord = frontier.pop(0)
|
||||
char = grid[coord]
|
||||
|
||||
|
||||
for c in acceptable_moves.get(char, {}):
|
||||
c_must_be = acceptable_moves[char][c]
|
||||
c = apply_coord_delta(coord, c)
|
||||
|
@ -103,7 +105,9 @@ def one(instr: str):
|
|||
def area(p):
|
||||
# https://stackoverflow.com/a/451482
|
||||
# HMM. I BARELY UNDERSTAND THIS.
|
||||
return 0.5 * abs(sum(x0*y1 - x1*y0 for ((x0, y0), (x1, y1)) in zip(p, p[1:] + [p[0]])))
|
||||
return 0.5 * abs(
|
||||
sum(x0 * y1 - x1 * y0 for ((x0, y0), (x1, y1)) in zip(p, p[1:] + [p[0]]))
|
||||
)
|
||||
|
||||
|
||||
def two(instr: str):
|
||||
|
@ -149,4 +153,4 @@ if __name__ == "__main__":
|
|||
if sys.argv[1] == "1":
|
||||
print(one(inp))
|
||||
else:
|
||||
print(two(inp))
|
||||
print(two(inp))
|
||||
|
|
|
@ -16,7 +16,7 @@ def parse(instr: str) -> list[Rule]:
|
|||
|
||||
def unfold(rule: Rule) -> Rule:
|
||||
obs, lens = rule
|
||||
return ((obs + "?")*5)[:-1], lens * 5
|
||||
return ((obs + "?") * 5)[:-1], lens * 5
|
||||
|
||||
|
||||
@cache
|
||||
|
@ -46,12 +46,11 @@ def solve(observations: str, lengths: list[int]) -> int:
|
|||
if "." in observations[:target_len]:
|
||||
return 0
|
||||
|
||||
|
||||
if target_len + 1 <= len(observations):
|
||||
if observations[target_len] == "#":
|
||||
return 0
|
||||
if observations[target_len] == "?":
|
||||
return solve("." + observations[target_len+1:], lengths[1:])
|
||||
return solve("." + observations[target_len + 1 :], lengths[1:])
|
||||
|
||||
return solve(observations[target_len:], lengths[1:])
|
||||
|
||||
|
@ -63,6 +62,7 @@ def run(rules: Iterable[Rule]) -> int:
|
|||
def one(instr: str):
|
||||
return run(parse(instr))
|
||||
|
||||
|
||||
def two(instr: str):
|
||||
return run(map(unfold, parse(instr)))
|
||||
|
||||
|
@ -80,4 +80,4 @@ if __name__ == "__main__":
|
|||
if sys.argv[1] == "1":
|
||||
print(one(inp))
|
||||
else:
|
||||
print(two(inp))
|
||||
print(two(inp))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue