import random, sys, argparse, textwrap, pprint from datetime import datetime class AnimalSet: tot_animals = 6 def __init__(self): self.animal_index = 0 self.base_animals = [ "monkey", "dog", "cat", "tortoise", "lizard", "bird", "rabbit", "fox", "goat", "chicken", "wolf", "duck", "snake", "hamster", "raccoon", "parrot", "ferret", "hedgehog", "otter", "squirrel", ] self.animals = {} self.animal_names = [] self.perms = [] self.current_perm_num = None for i in range(0,AnimalSet.tot_animals): self.animals[ self.base_animals[ i ] ] = False self.animal_names.append( self.base_animals[ i ] ) def __iter__(self): return self def __next__(self): next_animal = None for i in range( self.animal_index, AnimalSet.tot_animals ): animal_to_consider = self.animal_names[ i ] if self.animals[ animal_to_consider ]: self.animal_index = i + 1 next_animal = animal_to_consider break if next_animal: return next_animal raise StopIteration def reset_index(self): self.animal_index = 0 def add(self, animal): self.animals[ animal ] = True return self def add_set( self, animal_set ): for k,v in animal_set.animals.items(): if v: self.add( k ) return self def remove( self, animal ): self.animals[ animal ] = False return self def remove_set( self, animal_set ): for k,v in animal_set.animals.items(): if v: self.remove( k ) return self def add_all_animals(self): for i in range(0,AnimalSet.tot_animals): self.add( self.base_animals[ i ] ) return self def has_animals(self): # are there any animals present? has_animals = False for k,v in self.animals.items(): if v: has_animals = True break return has_animals def has_animal(self, animal): # is a particular animal present? return self.animals[ animal ] def num_animals(self): num_animals = 0 for k,v in self.animals.items(): if v: num_animals += 1 return num_animals def pick_subset(self, num): subset = AnimalSet() populated = [] for k,v in self.animals.items(): if v: populated.append( k ) sample = random.sample( populated, num ) for animal in sample: subset.add( animal ) self.remove( animal ) return subset def inverse(self): inverse = AnimalSet().add_all_animals() for k,v in self.animals.items(): if v: inverse.remove( k ) return inverse def dump(self): if self.has_animals(): for k,v in self.animals.items(): if v: print(" " + k) print("") else: print(" (no animals)\n") def as_inline_string(self, delim = "or", prepos = "the"): inline = "" animal_count = 0 num_animals = self.num_animals() if num_animals == 0: return "(no animals)" for k,v in self.animals.items(): if v: animal_count += 1 if inline: if animal_count == num_animals: inline += " " + delim + " " else: inline += ", " inline += prepos + " " + k return inline def dump_inline( self, delim = "or", prepos = "the" ): inline = self.as_inline_string( delim, prepos ) print(inline, end="") def clone( self ): cloned_set = AnimalSet() cloned_set.add_set( self ) return cloned_set def current_animal_names( self ): current_animal_names = [] for k,v in self.animals.items(): if v: current_animal_names.append( k ) return current_animal_names def perm( self, num, include_zero = False ): current_animal_names = self.current_animal_names() if self.current_perm_num == None or self.current_perm_num != num: self.zero_perm( num ) else: if len(self.perms) == 0: return None self.inc_perm() perm_set = AnimalSet() final_perm = True for perm_i in range(0,len( self.perms )): if self.perms[ perm_i ] < len( current_animal_names ): animal = current_animal_names[ self.perms[ perm_i ] ] perm_set.add( animal ) final_perm = False else: self.perms.pop(perm_i) break if final_perm: if include_zero: return AnimalSet() else: return None return perm_set def inc_perm( self ): done = False for perm_i in range(0,len( self.perms ) - 1): animal_i = self.perms[ perm_i ] + 1 if animal_i < self.perms[ perm_i + 1 ]: self.perms[ perm_i ] = animal_i done = True break else: self.perms[ perm_i ] = perm_i if not done: self.perms[-1] += 1 def clear_perm( self ): self.current_perm_num = None def zero_perm( self, num ): self.current_perm_num = num self.perms = [] for i in range(0,num): self.perms.append(-1) already = [] current_animal_names = self.current_animal_names() for perm_i in range(0,len( self.perms )): for animal_i, animal_name in enumerate( current_animal_names ): if animal_name not in already: self.perms[ perm_i ] = animal_i already.append( animal_name ) break def is_identical( self, test_set ): if self.num_animals() != test_set.num_animals(): return False is_identical = True ts = test_set.clone() ts.reset_index() for t in ts: if not self.has_animal( t ): is_identical = False break return is_identical class Step: def __init__(self): # The default step has all animals on the left bank, with none transported self.left_bank_before = AnimalSet().add_all_animals() self.right_bank_before = AnimalSet() self.is_outgoing = True self.transit_animals = AnimalSet() self.unsupervised = AnimalSet().add_all_animals() self.left_bank_after = AnimalSet().add_all_animals() self.right_bank_after = AnimalSet() self.already_travelled = AnimalSet() def dump_step(self): out_or_in = "Incoming" if self.is_outgoing: out_or_in = "Outgoing" print( "Transported (" + out_or_in + "): ", end="" ) self.transit_animals.dump_inline("and") print("") print( "Left Bank (at start): ", end="") self.left_bank_before.dump_inline("and") print("") print( "Right Bank (at start): ", end="") self.right_bank_before.dump_inline("and") print("") print( "Already travelled: ", end="") self.already_travelled.dump_inline("and") print("") print( "Left Bank (at end): ", end="") self.left_bank_after.dump_inline("and") print("") print( "Right Bank (at end): ", end="") self.right_bank_after.dump_inline("and") print("") def is_identical( self, test_step ): is_identical = True if not ( self.left_bank_before.is_identical( test_step.left_bank_before ) and \ self.right_bank_before.is_identical( test_step.right_bank_before ) and \ self.is_outgoing == test_step.is_outgoing and \ self.transit_animals.is_identical( test_step.transit_animals ) and \ self.unsupervised.is_identical( test_step.unsupervised ) and \ self.left_bank_after.is_identical( test_step.left_bank_after ) and \ self.right_bank_after.is_identical( test_step.right_bank_after ) and \ self.already_travelled.is_indentical( test_step.already_travelled ) ): identical = False return is_identical class StepManager: def __init__(self): self.max_boat_animals = 2 self.constraint_factor = 0.5 self.steps = [] self.left_bank_animals = AnimalSet() self.left_bank_animals.add_all_animals() self.right_bank_animals = AnimalSet() def add_step( self, step ): self.steps.append( step ) def prepend_step( self, step ): self.steps.insert(0, step) def dump_steps( self ): for i,step in enumerate(self.steps): print(f"Step {i + 1}:\n") step.dump_step() print(f"------------------------") print("\n") def choose_step( self, is_outgoing, num_transit_animals, already_travelled ): step = Step() step.already_travelled = already_travelled step.left_bank_before = self.left_bank_animals.clone() step.right_bank_before = self.right_bank_animals.clone() step.is_outgoing = is_outgoing transit_from = self.right_bank_animals transit_to = self.left_bank_animals if is_outgoing: transit_from = self.left_bank_animals transit_to = self.right_bank_animals step.transit_animals = transit_from.pick_subset( num_transit_animals ) step.already_travelled.add_set( step.transit_animals ) step.unsupervised = transit_from.clone() transit_to.add_set( step.transit_animals ) step.left_bank_after = self.left_bank_animals.clone() step.right_bank_after = self.right_bank_animals.clone() self.add_step( step ) return step def choose_steps( self ): all_transported = False already_travelled = AnimalSet() while True: outgoing_boat_animals = self.max_boat_animals if self.left_bank_animals.num_animals() < self.max_boat_animals: outgoing_boat_animals = self.left_bank_animals.num_animals() step = self.choose_step( True, outgoing_boat_animals, already_travelled ) already_travelled = step.already_travelled.clone() if not self.left_bank_animals.has_animals(): break step = self.choose_step( False, random.randint(0,self.max_boat_animals - 1), already_travelled ) already_travelled = step.already_travelled.clone() def reduce_strictness( self, animal_set ): num_to_set = animal_set.num_animals() if animal_set.num_animals() > 1: num_to_set = int( animal_set.num_animals() * self.constraint_factor ) if num_to_set == 0: num_to_set = 1 less_strict = animal_set.pick_subset( num_to_set ) return less_strict def always_transported_with( self, animal ): always_transported_with = AnimalSet().add_all_animals().remove( animal ) for step in self.steps: if not step.transit_animals.has_animal( animal ): continue step.transit_animals.reset_index() all_animals_cp = AnimalSet().add_all_animals() for animal_cp in all_animals_cp: if not step.transit_animals.has_animal( animal_cp ): always_transported_with.remove( animal_cp ) return self.reduce_strictness( always_transported_with ) def never_transported_with( self, animal ): transported_together = AnimalSet() for step in self.steps: if not step.transit_animals.has_animal( animal ): continue step.transit_animals.reset_index() for transit_animal in step.transit_animals: if transit_animal == animal: continue transported_together.add( transit_animal ) never_transported_with = transported_together.inverse() return self.reduce_strictness( never_transported_with ) def always_unsupervised_with( self, animal ): always_unsupervised_with = AnimalSet().add_all_animals().remove( animal ) for step in self.steps: if not step.unsupervised.has_animal( animal ): continue all_animals_cp = AnimalSet().add_all_animals() for animal_cp in all_animals_cp: if not step.unsupervised.has_animal( animal_cp ): always_unsupervised_with.remove( animal_cp ) return self.reduce_strictness( always_unsupervised_with ) def never_unsupervised_with( self, animal ): never_unsupervised_with = AnimalSet().add_all_animals().remove( animal ) for step in self.steps: if not step.unsupervised.has_animal( animal ): continue step.unsupervised.reset_index() for unsupervised_animal in step.unsupervised: if unsupervised_animal == animal: continue never_unsupervised_with.remove( unsupervised_animal ) return self.reduce_strictness( never_unsupervised_with ) def never_replaces( self, animal ): never_replaces = AnimalSet().add_all_animals().remove( animal ) for i in range(0,len(self.steps)-1): next_step = self.steps[i+1] if not next_step.transit_animals.has_animal( animal ): continue this_step = self.steps[i] never_replaces.remove_set( this_step.transit_animals ) return self.reduce_strictness( never_replaces ) def always_replaces( self, animal ): if self.steps[0].transit_animals.has_animal( animal ): # we can't always be replacing any animal return AnimalSet() # if we travel on the first step always_replaces = AnimalSet().add_all_animals().remove( animal ) for i in range(0,len(self.steps)-1): next_step = self.steps[i+1] if not next_step.transit_animals.has_animal( animal ): continue this_step = self.steps[i] did_not_replace = this_step.transit_animals.inverse() always_replaces.remove_set( did_not_replace ) return self.reduce_strictness( always_replaces ) def always_travels_after( self, animal ): always_travels_after = AnimalSet() for step in self.steps: if step.transit_animals.has_animal( animal ): break always_travels_after.add_set( step.transit_animals ) return self.reduce_strictness( always_travels_after ) def is_identical( self, test_sm ): if len(self.steps) != len(test_sm.steps): return False is_identical = True for i,test_step in enumerate(test_sm.steps): if not self.steps[i].is_identical( test_step ): is_identical = False break return is_identical class RuleManager: def __init__(self, sm): self.solution_sm = sm self.rules = [] self.unsupervised_with = { "never" : {}, "always" : {} } self.transported_with = { "never" : {}, "always" : {} } self.replaces = {"never" : {}, "always" : {} } self.always_travels_after = {} self.allow_zero_on_return = True def set_unsupervised_with( self, type, animal, animal_set ): self.unsupervised_with[ type ][ animal ] = animal_set def set_transported_with( self, type, animal, animal_set ): self.transported_with[ type ][ animal ] = animal_set def set_replaces( self, type, animal, animal_set): self.replaces[ type ][ animal ] = animal_set def set_always_travels_after( self, animal, animal_set ): self.always_travels_after[ animal ] = animal_set def get_unsupervised_with( self, type, animal ): unsupervised_with = self.unsupervised_with[ type ][ animal ] return unsupervised_with def get_transported_with( self, type, animal ): transported_with = self.transported_with[ type ][ animal ] return transported_with def get_replaces( self, type, animal ): replaces = self.replaces[ type ][ animal ] return replaces def get_always_travels_after( self, animal ): return self.always_travels_after[ animal ] def set_rules( self ): all_animals = AnimalSet().add_all_animals() seen_animals = AnimalSet() for animal in all_animals: seen_animals.add( animal ) always_transported_with = self.solution_sm.always_transported_with( animal ) never_transported_with = self.solution_sm.never_transported_with( animal ) never_transported_with.remove_set( seen_animals ) always_unsupervised_with = self.solution_sm.always_unsupervised_with( animal ) never_unsupervised_with = self.solution_sm.never_unsupervised_with( animal ) never_replaces = self.solution_sm.never_replaces( animal ) always_replaces = self.solution_sm.always_replaces( animal ) always_travels_after = self.solution_sm.always_travels_after( animal ) self.set_unsupervised_with( "never", animal, never_unsupervised_with ) self.set_unsupervised_with( "always", animal, always_unsupervised_with ) self.set_transported_with( "never", animal, never_transported_with ) self.set_transported_with( "always", animal, always_transported_with ) self.set_replaces( "never", animal, never_replaces ) self.set_replaces( "always", animal, always_replaces ) self.set_always_travels_after( animal, always_travels_after ) def gen_rule_descriptions( self ): all_animals = AnimalSet().add_all_animals() for animal in all_animals: never_unsupervised_with = self.get_unsupervised_with( "never", animal ) if never_unsupervised_with.has_animals(): self.rules.append( f"The {animal} can never be left unsupervised with " + never_unsupervised_with.as_inline_string() ) always_unsupervised_with = self.get_unsupervised_with("always", animal ) if always_unsupervised_with.has_animals(): self.rules.append( f"The {animal} can only be left unsupervised if accompanied by " + always_unsupervised_with.as_inline_string("and") ) never_transported_with = self.get_transported_with( "never", animal ) if never_transported_with.has_animals(): self.rules.append(f"The {animal} can never be transported with " + never_transported_with.as_inline_string() ) always_transported_with = self.get_transported_with( "always", animal ) if always_transported_with.has_animals(): self.rules.append(f"The {animal} can only be transported if accompanied with " + always_transported_with.as_inline_string("and") ) never_replaces = self.get_replaces( "never", animal ) if never_replaces.has_animals(): self.rules.append(f"The {animal} cannot get on the boat if " + never_replaces.as_inline_string("or") + " just got off it") always_replaces = self.get_replaces( "always", animal ) if always_replaces.has_animals(): self.rules.append(f"The {animal} can only get on the boat if " + always_replaces.as_inline_string("and") + " just got off it") always_travels_after = self.get_always_travels_after( animal ) if always_travels_after.has_animals(): self.rules.append(f"The {animal}'s first trip must always be after the first trip of " + always_travels_after.as_inline_string("and")) def dump_rules( self ): for i,rule in enumerate(self.rules): print(f"{i+1}: {rule}") def get_allowed_steps( self, previous_step, max_boat_animals ): if previous_step and previous_step.left_bank_after.num_animals() == 0: # we're done if the left return [] # bank is empty left_bank_animals = AnimalSet().add_all_animals() right_bank_animals = AnimalSet() is_outgoing = True if previous_step: left_bank_animals = previous_step.left_bank_after right_bank_animals = previous_step.right_bank_after is_outgoing = not previous_step.is_outgoing from_bank_animals = right_bank_animals if is_outgoing: from_bank_animals = left_bank_animals steps = [] from_bank_animals.clear_perm() allow_zero = False if self.allow_zero_on_return: allow_zero = not is_outgoing if from_bank_animals.num_animals() < max_boat_animals: max_boat_animals = from_bank_animals.num_animals() while transit_animals := from_bank_animals.perm( max_boat_animals, allow_zero ): if previous_step and transit_animals.is_identical( previous_step.transit_animals ): continue unsupervised_animals = from_bank_animals.clone().remove_set( transit_animals ) allowed = self.can_be_unsupervised( unsupervised_animals ) if allowed: allowed = self.can_travel_together( transit_animals ) if allowed and previous_step: allowed = self.can_replace( previous_step, transit_animals ) if allowed: allowed = self.free_to_travel_now( previous_step, transit_animals ) if allowed: step = Step() step.left_bank_before = left_bank_animals.clone() step.right_bank_before = right_bank_animals.clone() step.is_outgoing = is_outgoing step.transit_animals = transit_animals step.already_travelled = AnimalSet().add_set( step.transit_animals ) if previous_step: step.already_travelled = previous_step.already_travelled.clone().add_set( step.transit_animals ) if is_outgoing: step.left_bank_after = left_bank_animals.clone().remove_set(transit_animals) step.right_bank_after = right_bank_animals.clone().add_set(transit_animals) else: step.left_bank_after = left_bank_animals.clone().add_set( transit_animals ) step.right_bank_after = right_bank_animals.clone().remove_set( transit_animals ) steps.append( step ) return steps def can_travel_together( self, animals ): ok = self.check_never_ok( "transported", animals ) if ok: ok = self.check_always_ok( "transported", animals ) return ok def can_replace( self, previous_step, transit_animals ): allowed = self.check_never_replace( previous_step, transit_animals ) if allowed: allowed = self.check_always_replace( previous_step, transit_animals ) return allowed def check_never_replace( self, previous_step, transit_animals ): if not previous_step: return True can_replace = True transit_animals.reset_index() for transit_animal in transit_animals: never_replaces = self.get_replaces( "never", transit_animal ) previous_step.transit_animals.reset_index() for previous_animal in previous_step.transit_animals: if never_replaces.has_animal( previous_animal ): can_replace = False break if not can_replace: break return can_replace def check_always_replace( self, previous_step, transit_animals ): can_replace = True transit_animals.reset_index() for transit_animal in transit_animals: always_replaces = self.get_replaces( "always", transit_animal ) if not always_replaces.has_animals(): continue if not previous_step: can_replace = False break previous_step.transit_animals.reset_index() for previous_animal in previous_step.transit_animals: if not always_replaces.has_animal( previous_animal ): can_replace = False break if not can_replace: break return can_replace def free_to_travel_now( self, previous_step, transit_animals ): already_travelled = AnimalSet() if previous_step: already_travelled = previous_step.already_travelled free_to_travel = True transit_animals.reset_index() for transit_animal in transit_animals: if already_travelled.has_animal( transit_animal ): continue always_travels_after = self.get_always_travels_after( transit_animal ) always_travels_after.reset_index() for always_animal in always_travels_after: if not already_travelled.has_animal( always_animal ): free_to_travel = False break if not free_to_travel: break return free_to_travel def can_be_unsupervised( self, animals ): ok = self.check_never_ok( "unsupervised", animals ) if ok: ok = self.check_always_ok( "unsupervised", animals ) return ok def check_never_ok( self, type, animals ): never_ok = True animals.reset_index() for animal in animals: method = "get_" + type + "_with" never_set = getattr(self,method)("never", animal) never_set.reset_index() for never_animal in never_set: if animals.has_animal( never_animal ): never_ok = False break if not never_ok: break return never_ok def check_always_ok( self, type, animals ): always_ok = True animals.reset_index() for animal in animals: method = "get_" + type + "_with" always_set = getattr(self, method)("always", animal ) always_set.reset_index() for always_animal in always_set: if not animals.has_animal( always_animal ): always_ok = False break if not always_ok: break return always_ok def find_paths( self ): print(f"Finding paths") sms = self.find_remaining_steps( None ) return sms def find_remaining_steps( self, previous_step): # Not efficient - needs a better algorithm max_animals = self.solution_sm.max_boat_animals if previous_step and previous_step.is_outgoing: max_animals = previous_step.transit_animals.num_animals() - 1 allowed_steps = self.get_allowed_steps( previous_step, max_animals ) all_sms = [] for allowed_step in allowed_steps: if allowed_step.left_bank_after.num_animals() == 0: sm = StepManager() sm.add_step( allowed_step ) all_sms.append( sm ) else: sms = self.find_remaining_steps( allowed_step) if len(sms) == 0: sm = StepManager() sm.add_step( allowed_step ) all_sms.append( sm ) else: for sm in sms: sm.prepend_step( allowed_step ) all_sms.append( sm ) return all_sms def is_simple_unique( self ): previous_step = None is_unique = True max_i = 0 for i,step in enumerate( self.solution_sm.steps ): allowed_steps = self.get_allowed_steps( previous_step, self.solution_sm.max_boat_animals ) if len( allowed_steps ) != 1: is_unique = False break previous_step = allowed_steps[0] return is_unique def is_varied_incoming( self ): seen = [] target = 2 if self.solution_sm.max_boat_animals > 2: target = int( 1 + self.solution_sm.max_boat_animals / 2 ) is_varied_incoming = False for step in self.solution_sm.steps: na = step.transit_animals.num_animals() if not na in seen: seen.append( na ) if len(seen) >= target: is_varied_incoming = True break return is_varied_incoming class PuzzleManager: def __init__(self): self.max_boat_animals = 2 self.constraint_factor = 0.5 self.max_retries = 5000 self.sm = None self.rm = None self.paths_sms = [] self.solution_sms = [] self.min_solution_sms = [] def init_puzzle(self, verbose = False): self.sm = StepManager() self.sm.constraint_factor = self.constraint_factor self.choose_solution( verbose ) self.rm = RuleManager( self.sm ) self.gen_rules( verbose ) def choose_solution(self, verbose = False): self.sm.max_boat_animals = self.max_boat_animals self.sm.choose_steps() if verbose: print(f"=> Decided 1 solution to start with") def gen_rules(self, verbose = False): self.rm.set_rules() self.rm.gen_rule_descriptions() if verbose: print(f"=> Set rules based on the originally chosen solution") def find_simple_unique_puzzle(self): unique_found = False for i in range(0,self.max_retries): self.sm = StepManager() self.sm.constraint_factor = 1 self.choose_solution() self.rm = RuleManager( self.sm ) self.gen_rules() if self.rm.is_simple_unique(): print("=> Found a simple unique puzzle!") unique_found = True break if not unique_found: print(f"=> Failed to find a unique puzzle in {self.max_simple_unique_tries} attempts") def find_varied_incoming_puzzle(self): varied_incoming_found = False for i in range(0, self.max_retries): self.init_puzzle( True ) if self.rm.is_varied_incoming(): varied_incoming_found = True break if not varied_incoming_found: print(f"=> Failed to find a unique puzzle in {self.max_simple_unique_tries} attempts") def clear_report( self ): self.report = "" def add_to_report( self, text ): self.report += "=> " + text + "\n" def dump_report( self ): print(self.report) def find_min_solutions(self): self.clear_report() self.add_to_report("Finding available paths based on the rules") self.paths_sms = self.rm.find_paths() self.add_to_report(f"Found {len(self.paths_sms)} paths!") num_solutions = 0 min_step_solution_sm = None min_steps = 0 self.solution_sms = [] for i,paths_sm in enumerate(self.paths_sms): # find minimum number of steps made amongst solutions if not paths_sm.steps[-1].left_bank_after.has_animals(): num_solutions +=1 self.solution_sms.append( paths_sm ) if not min_steps or len(paths_sm.steps) < min_steps: min_steps = len(paths_sm.steps) self.add_to_report(f"Of which {num_solutions} were solutions") # gather all min step solutions num_min_step_solutions = 1 self.min_solution_sms = [] solution_content_str = "" if min_steps == 0 or len(self.sm.steps) < min_steps: self.min_solution_sms = [ self.sm ] # there's only one if the original is the shortest self.add_to_report("This was the only solution found with the minimum number of steps.") min_steps = len(self.sm.steps) else: num_min_step_solutions = 0 if len(self.sm.steps) == min_steps: # if original is the same length as the others found... is_identical = False for solution_sm in self.solution_sms: # .. check if its the same... if solution_sm.is_identical( self.sm ): is_identical = True break if is_identical: self.add_to_report("The original was the same as one of those found.") else: self.min_solution_sms.append( sm ) # ... and add it to the set if not self.add_to_report("The original solution was different to those found, and has been added to the list.") num_solutions += 1 for solution_sm in self.solution_sms: if len(solution_sm.steps) == min_steps: self.min_solution_sms.append( solution_sm ) num_min_step_solutions += 1 self.add_to_report(f"{num_solutions} solutions were found among checked paths") self.add_to_report(f"In total {num_min_step_solutions} solutions were solved in a minimum number of steps ({min_steps} steps)") self.add_to_report(f"The original chosen solution has {len(self.sm.steps)} steps") def dump_puzzle(self): print("\n== RIVER CROSSING PUZZLE ==\n") animals = AnimalSet().add_all_animals().as_inline_string("and", "a") puzzle_text = textwrap.fill(f"""You are a human on the side of river with {animals}. You are responsible for looking after these animals. You need to get the animals to the other side of the river using a boat which is moored at the side of the river. However, you need to be aboard the boat to steer it, and the boat can only carry {self.max_boat_animals} animals in addition to yourself. The animals won't run away if left alone, but they might attack or eat each other in certain circumstances, which cannot be allowed. The rules are as follows:""", width = 100) print(puzzle_text) print("\n========= RULES ===========\n") self.rm.dump_rules() def dump_min_solutions( self ): print("\n\n=== MIN STEP SOLUTIONS ===\n") for i,min_sm in enumerate(self.min_solution_sms): print(f"--- Solution {i+1} ---\n") min_sm.dump_steps() def dump_original_solution( self ): print("\n\n=== ORIGINAL SOLUTION ====\n") self.sm.dump_steps() if __name__ == "__main__": parser = argparse.ArgumentParser( prog = "river-crossing-tg", description="River Crossing Puzzle Generator - TG Oct 2026 - Written entirely by a human!" ) parser.add_argument("-a", "--max-animals", type=int, default=6, help="The number of animals to transport") parser.add_argument("-b", "--boat-capacity", type=int, default=2, help="The maximum number of animals that can be transported in the boat at one time") parser.add_argument("-B", "--buster", action="store_true", default=False, help="Create a ChatGPT \"busting\" puzzle (one ChatGPT is likely to struggle with). Note this assumes a set of parameters, and will override them if they are set independently") parser.add_argument("-c", "--constraint-factor", type=float, default=0.5, help="The strictness of the rules to apply. 1 = strict as possible (can lead to a forced puzzle), 0 = least strict (puzzle may have many solutions)") parser.add_argument("-m", "--min-solutions", action="store_true", default=False, help="Also check for shorter solutions (than the original assumed solution) which obey the same rule set. Note this is a search-type operation which may take a very long time (and consume system resources) for higher complexity puzzles, and probably should not be used in this case.") parser.add_argument("-u", "--simple-unique", action="store_true", default=False, help="Create a \"simple unique\" puzzle - where there is always only one possible next move until all animals are transported. This overrides the \"constraint factor\" setting it to 1. Potentially useful for testing. Otherwise, not very challenging.") parser.add_argument("-r", "--max-retries", type=int, default=5000, help="Applies when search-type operations are involved. This is the hard upper limit to the number of possible retries, above which the job is aborted") parser.add_argument("-s", "--seed", type=int, default=None, help="The random seed to use. Stating an explicit seed is helpful for repeat generations of the same puzzle. If not specified, the seed is taken from the clock") args = parser.parse_args() if args.seed: random.seed(args.seed) else: random.seed(random.seed(datetime.now().timestamp())) pm = PuzzleManager() # Set params from command line pm.max_boat_animals = args.boat_capacity AnimalSet.tot_animals = args.max_animals pm.max_retries = args.max_retries pm.constraint_factor = args.constraint_factor find_min_solutions = args.min_solutions if args.buster: # Override with "busting" parameters. What should these be though? pm.max_boat_animals = 3 AnimalSet.tot_animals = 12 pm.constraint_factor = 0.8 find_min_solutions = False pm.find_varied_incoming_puzzle() elif args.simple_unique: pm.find_simple_unique_puzzle() else: pm.init_puzzle(True) pm.dump_puzzle() pm.dump_original_solution() if find_min_solutions: pm.find_min_solutions() pm.dump_report() pm.dump_min_solutions()