This commit is contained in:
DomNomNom 2025-04-17 14:01:20 +12:00
parent 2154840844
commit 4f792e5026
3 changed files with 80 additions and 13 deletions

71
klimpen-functional.py Normal file
View File

@ -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)

Binary file not shown.

View File

@ -329,14 +329,6 @@ def test_breadth_first_search():
) == [twist] ) == [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( def bidirectional_search(
start: Skewb, max_steps: int, end: Skewb = solved_skewb start: Skewb, max_steps: int, end: Skewb = solved_skewb
) -> list[Twist] | None: ) -> list[Twist] | None:
@ -546,7 +538,7 @@ def get_paths_from_heuristic(
assert len(matches) == len(mask) assert len(matches) == len(mask)
return all(match >= m for match, m in zip(matches, 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: if sum(mask) == len(heuristic_permutation) - 3:
# print("going bidirectional now.") # print("going bidirectional now.")
print("end-bi", end=" ", flush=True) print("end-bi", end=" ", flush=True)
@ -555,7 +547,7 @@ def get_paths_from_heuristic(
path = breadth_first_search( path = breadth_first_search(
s, step_finished, bidirectional_fallback_threshold=200000 s, step_finished, bidirectional_fallback_threshold=200000
) )
print(f" {path=}") # print(f" {path=}")
if path is None: if path is None:
raise ValueError("oh no! solver could not find solution") raise ValueError("oh no! solver could not find solution")
out.append(path) 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)) # 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)) # start = Skewb(top=(O0, B0, R1, G1), bot=(B0, R2, G2, O0), mids=(BY, RY, GY, OY, YY))
HURISTIC_PERMUTATION_LENGTH = 4 + 4 + 5 HURISTIC_PERMUTATION_LENGTH = 4 + 4 + 5
@ -589,7 +579,10 @@ def evaluate_permutation(
heuristic_permutation: list[int], seed=4, sample_size: int = 10 heuristic_permutation: list[int], seed=4, sample_size: int = 10
) -> float: ) -> float:
scores = [] scores = []
rng = random.Random(seed)
for i in range(sample_size): 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) paths = get_paths_from_heuristic(start, heuristic_permutation)
score = score_fn([len(p) for p in paths]) score = score_fn([len(p) for p in paths])
print(f"{score=}") print(f"{score=}")
@ -616,10 +609,13 @@ def evaluate_all_1_swaps_except_first(hp: list[int]):
if __name__ == "__main__": if __name__ == "__main__":
hp = top_bot_mids = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] 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] 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(top_down)
evaluate_all_1_swaps_except_first(hp) 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(bidirectional_search(start, max_steps=1000000))
# print(get_paths_from_heuristic(start, hp)) # print(get_paths_from_heuristic(start, hp))