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 0000000..0378669 Binary files /dev/null and b/skewb_solver.evaluate_permutation.shelve.sqlite differ 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))