From 4f792e5026c2e99aae47c5ab7020984e32f0121f Mon Sep 17 00:00:00 2001 From: DomNomNom Date: Thu, 17 Apr 2025 14:01:20 +1200 Subject: [PATCH] blah --- klimpen-functional.py | 71 ++++++++++++++++++ ..._solver.evaluate_permutation.shelve.sqlite | Bin 0 -> 28672 bytes skewb_solver.py | 22 +++--- 3 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 klimpen-functional.py create mode 100644 skewb_solver.evaluate_permutation.shelve.sqlite diff --git a/klimpen-functional.py b/klimpen-functional.py new file mode 100644 index 0000000..0b15776 --- /dev/null +++ b/klimpen-functional.py @@ -0,0 +1,71 @@ +from functools import reduce + +def lines_with_sequence(char): + def with_char(length): + sequence = char * length + + def with_length(doc): + return reduce(lambda count, line: count + int(sequence in line), doc.split("\n"), 0) + + return with_length + return with_char + + + +part1 = lines_with_sequence('C') +line_match_counter = part1(4) +doc = """ +hello +world +CCCC CCCC +CCCC +""" +result = line_match_counter(doc) +print(result) + +###### + +def count_matching_lines(doc: str, needle: str) -> int: + """Returns the number of lines where needle is in doc.""" + return sum(1 for line in doc.splitlines() if needle in line) + + +def lines_with_sequence(char): + def with_char(length): + return lambda doc: count_matching_lines(doc, char * length) + return with_char + +part1 = lines_with_sequence('C') +line_match_counter = part1(4) +doc = """ +hello +world +CCCC CCCC +CCCC +""" +result = line_match_counter(doc) +print(result) + + +##### + +from functools import partial + +def count_matching_lines(char: str, length: int, doc: str) -> int: + needle = char * length + return sum(1 for line in doc.splitlines() if needle in line) +# lines_with_sequence +# part1 = lines_with_sequence('C') +# line_match_counter = part1(4) +part1 = partial(count_matching_lines, 'C') +line_match_counter = partial(part1, 4) + +doc = """ +hello +world +CCCC CCCC +CCCC +""" +result = line_match_counter(doc) +print(result) + diff --git a/skewb_solver.evaluate_permutation.shelve.sqlite b/skewb_solver.evaluate_permutation.shelve.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..03786697292840a9485b65da5ac2871d2966c093 GIT binary patch literal 28672 zcmeI4ZHy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9^a&HvEEi*X=QHpMcaj$g2 z_+NqnLvo{#do>3JmLd!o@{0`aO%3+A6?;e$E^5?e8gsg~q1y$ajZ(Oaz`u+D%?V)O zLTrm1j`LT_6S*4X8rATwJsd{_v1);P{@PBFoi34yMTz3Bcz{7U$S)2=vn0zrje<=j z7_?Lr4fmELL<6UaFknb(fqUBxFi31D1`K&Ea4$ar1`O=cLM?EYaln9q903Dftb}`? z+qXjjBW}XISIWRZM^U4I7iG%(S}b57Whe#=Nu~nR8%S0V1BN_P-n-q1#uf&oSP}>B zumEF6gC=-Sn#2`pBX|!QGKE%f7-C4Y5xma|7>Lxw5JRqwV0wdm1ECrmFd}=ung0c$ CvG?f! literal 0 HcmV?d00001 diff --git a/skewb_solver.py b/skewb_solver.py index 6bbb052..d37bdec 100644 --- a/skewb_solver.py +++ b/skewb_solver.py @@ -329,14 +329,6 @@ def test_breadth_first_search(): ) == [twist] -def print_path(path: list[Axis]): - x = start - print(f"S -> {x}") - for twist in path: - x = clockwise_twist(x, twist) - print(f"{twist} -> {x}") - - def bidirectional_search( start: Skewb, max_steps: int, end: Skewb = solved_skewb ) -> list[Twist] | None: @@ -546,7 +538,7 @@ def get_paths_from_heuristic( assert len(matches) == len(mask) return all(match >= m for match, m in zip(matches, mask)) - print(f"{mask=} {s=}", end=" ") + # print(f"{mask=} {s=}", end=" ") if sum(mask) == len(heuristic_permutation) - 3: # print("going bidirectional now.") print("end-bi", end=" ", flush=True) @@ -555,7 +547,7 @@ def get_paths_from_heuristic( path = breadth_first_search( s, step_finished, bidirectional_fallback_threshold=200000 ) - print(f" {path=}") + # print(f" {path=}") if path is None: raise ValueError("oh no! solver could not find solution") out.append(path) @@ -569,8 +561,6 @@ def get_paths_from_heuristic( # close_to_wrongly_solved = Skewb(top=(R0, B0, O0, G0), bot=(B0, O0, G0, R0), mids=(BY, GRB, ORG, RY, YY)) -near_end = Skewb(top=(O0, B0, R0, G2), bot=(B0, R0, G1, O0), mids=(BY, RY, GY, OY, YY)) -start = near_end # start = Skewb(top=(O0, B0, R1, G1), bot=(B0, R2, G2, O0), mids=(BY, RY, GY, OY, YY)) HURISTIC_PERMUTATION_LENGTH = 4 + 4 + 5 @@ -589,7 +579,10 @@ def evaluate_permutation( heuristic_permutation: list[int], seed=4, sample_size: int = 10 ) -> float: scores = [] + rng = random.Random(seed) for i in range(sample_size): + start = random_skewb(rng.randint(0, 2**63)) + print(f"{heuristic_permutation=} {start=}", end=" ") paths = get_paths_from_heuristic(start, heuristic_permutation) score = score_fn([len(p) for p in paths]) print(f"{score=}") @@ -616,10 +609,13 @@ def evaluate_all_1_swaps_except_first(hp: list[int]): if __name__ == "__main__": hp = top_bot_mids = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] + hp = top_down_modified = [0, 1, 2, 3, 8, 9, 10, 11, 12, 4, 5, 6, 7] hp = top_down = [0, 1, 2, 3, 8, 9, 10, 11, 12, 4, 5, 6, 7] # evaluate_all_1_swaps(top_down) evaluate_all_1_swaps_except_first(hp) - start = Skewb((G0, O0, B0, R0), (G0, R1, B2, O2), (BY, OY, RO, GR, YY)) + # start = hard = Skewb((O0, B0, R0, G2), (B0, R0, G1, O0), (BY, RY, GY, OY, YY)) + # start = subtle_invalid = Skewb((O0, B0, R0, G2), (B0, R0, G1, O0), (BY, RY, GY, OY, YY)) + # start = bed = Skewb((G0, O0, B0, R0), (G0, R1, B2, O2), (BY, OY, RO, GR, YY)) # print(bidirectional_search(start, max_steps=1000000)) # print(get_paths_from_heuristic(start, hp))