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.int_seq import triangular
from tools.tools import list_combinations_of_sum
from typing import Any
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)
def _get_placement_options(open_groups: list[tuple[int, str]], groups: list[int], not_found: list[tuple[int, int, int]]) -> list:
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:
print("look in", group_str, "for", [groups[x[2]] for x in not_found])
assigned = []
@ -16,7 +29,7 @@ def get_arrangements(springs: str, groups: list[int]):
if groups[idx_need] > len(group_str):
continue
elif (not assigned or sum(assigned) + len(assigned) + groups[idx_need] <= len(group_str)) and (
min_x <= start <= max_x
min_x <= start <= max_x
):
assigned.append(groups[idx_need])
not_found = not_found[1:]
@ -26,6 +39,14 @@ def get_arrangements(springs: str, groups: list[int]):
if 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
for group_str, values in to_determine:
if len(values) == 1 and len(group_str) == values[0]:
@ -51,7 +72,7 @@ def get_arrangements(springs: str, groups: list[int]):
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 = []
start = None
c_size = 0
@ -123,21 +144,30 @@ def get_open_groups(springs: str) -> list[tuple[int, str]]:
group_str = ""
for i, c in enumerate(springs):
if c == ".":
if "?" in group_str:
if start is not None:
open_groups.append((start, group_str))
start = None
group_str = ""
start = None
group_str = ""
else:
if start is None:
start = i
group_str += c
if "?" in group_str:
if start is not None:
open_groups.append((start, group_str))
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):
inputs = [
[
@ -159,10 +189,13 @@ class Day(AOCDay):
print("RESULT", springs, "=>", f)
ans += f
print("Final result:", ans, "=>", self._current_test_solution)
exit(0)
return ans
def part2(self) -> Any:
return ""
if not self.is_test():
return ""
ans = 0
for i, line in enumerate(self.getInput()):
springs, groups = line.split()

View File

@ -19,7 +19,7 @@ def walk(grid: Grid, start: tuple[Coordinate, tuple[int, int]]) -> int:
visited.add(cur)
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))
elif tile == '|':
q.append((pos + (0, 1), (0, 1)))