Day 16 - correct magically working brainfart

This commit is contained in:
Stefan Harmuth 2023-12-16 13:36:52 +01:00
parent e56617ad6b
commit 8eb98d2bd6
2 changed files with 45 additions and 12 deletions

View File

@ -1,13 +1,26 @@
from tools.aoc import AOCDay from tools.aoc import AOCDay
from tools.int_seq import triangular from tools.int_seq import triangular
from tools.tools import list_combinations_of_sum
from typing import Any from typing import Any
def get_arrangements(springs: str, groups: list[int]): def _get_placement_options(open_groups: list[tuple[int, str]], groups: list[int], not_found: list[tuple[int, int, int]]) -> list:
springs, not_found = clean_springs(springs, groups)
print("not_found", not_found)
open_groups = get_open_groups(springs)
to_determine = [] to_determine = []
min_group_len = min(groups[x[2]] for x in not_found)
for x, group_str in open_groups.copy():
if len(group_str) < min_group_len:
open_groups.remove((x, group_str))
assert not not_found or open_groups, f"No open_groups left to fill with {not_found}"
# open_groups = list(sorted(open_groups))
# open_groups should always be sorted by default
for possible_combination in list_combinations_of_sum(len(not_found), len(open_groups)):
# try to put possible_combination[x] in open_groups[x]
nf_idx = 0
for i, to_place in enumerate(possible_combination):
# try to place to_place not_founds in open_groups[i]
pass
for start, group_str in open_groups: for start, group_str in open_groups:
print("look in", group_str, "for", [groups[x[2]] for x in not_found]) print("look in", group_str, "for", [groups[x[2]] for x in not_found])
assigned = [] assigned = []
@ -26,6 +39,14 @@ def get_arrangements(springs: str, groups: list[int]):
if assigned: if assigned:
to_determine.append((group_str, assigned)) to_determine.append((group_str, assigned))
return to_determine
def _get_arrangements(springs: str, groups: list[int]):
springs, not_found = clean_springs(springs, groups)
print("not_found", not_found)
open_groups = get_open_groups(springs)
to_determine = get_placement_options(open_groups, groups, not_found)
arrangements = 1 arrangements = 1
for group_str, values in to_determine: for group_str, values in to_determine:
if len(values) == 1 and len(group_str) == values[0]: if len(values) == 1 and len(group_str) == values[0]:
@ -51,7 +72,7 @@ def get_arrangements(springs: str, groups: list[int]):
return arrangements return arrangements
def clean_springs(springs: str, groups: list[int]) -> (str, list[int]): def _clean_springs(springs: str, groups: list[int]) -> (str, list[tuple[int, int, int]]):
hashes = [] hashes = []
start = None start = None
c_size = 0 c_size = 0
@ -123,7 +144,7 @@ def get_open_groups(springs: str) -> list[tuple[int, str]]:
group_str = "" group_str = ""
for i, c in enumerate(springs): for i, c in enumerate(springs):
if c == ".": if c == ".":
if "?" in group_str: if start is not None:
open_groups.append((start, group_str)) open_groups.append((start, group_str))
start = None start = None
group_str = "" group_str = ""
@ -132,12 +153,21 @@ def get_open_groups(springs: str) -> list[tuple[int, str]]:
start = i start = i
group_str += c group_str += c
if "?" in group_str: if start is not None:
open_groups.append((start, group_str)) open_groups.append((start, group_str))
return open_groups return open_groups
def get_arrangements(springs: str, groups: list[int]) -> int:
if sum(groups) + len(groups) - 1 == len(springs):
return 1
open_groups = get_open_groups(springs)
print(f"{groups} => {springs} => {open_groups}")
return 0
class Day(AOCDay): class Day(AOCDay):
inputs = [ inputs = [
[ [
@ -159,9 +189,12 @@ class Day(AOCDay):
print("RESULT", springs, "=>", f) print("RESULT", springs, "=>", f)
ans += f ans += f
print("Final result:", ans, "=>", self._current_test_solution)
exit(0)
return ans return ans
def part2(self) -> Any: def part2(self) -> Any:
if not self.is_test():
return "" return ""
ans = 0 ans = 0
for i, line in enumerate(self.getInput()): for i, line in enumerate(self.getInput()):

View File

@ -19,7 +19,7 @@ def walk(grid: Grid, start: tuple[Coordinate, tuple[int, int]]) -> int:
visited.add(cur) visited.add(cur)
tile = grid.get(pos) tile = grid.get(pos)
if not tile or (tile == '-' and dir in [(0, 1), (0, -1)]) or (tile == '|' and dir in [(1, 0), (-1, 0)]): if not tile or (tile == '-' and direction[1] == 0) or (tile == '|' and direction[0] == 0):
q.append((pos + direction, direction)) q.append((pos + direction, direction))
elif tile == '|': elif tile == '|':
q.append((pos + (0, 1), (0, 1))) q.append((pos + (0, 1), (0, 1)))