Guest User

Untitled

a guest
Dec 10th, 2025
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. # Input: lines like "[<part1>] (<part2>) ... {<part3>}"
  2. #        part1 = string of . #
  3. #        part2, part3 = comma-separated list of numbers
  4. # Output: each part2 button toggles elements of that line's part1 (0-indexed)
  5. #         find fewest # presses to get each line from all "." to part1
  6. #         ignore part3 for this half
  7.  
  8. import itertools
  9.  
  10. def string_without_ends(s):
  11.   return s[1:len(s)-1]
  12.  
  13. def list_to_string(l):
  14.   return "/".join(l)
  15.  
  16. def string_to_list(s):
  17.   return s.split("/")
  18.  
  19. def is_all_off(s):
  20.   return not ("#" in s)
  21.  
  22. def is_valid(part1_list_to_string, part2_list_to_string):
  23.   part1_list = string_to_list(part1_list_to_string)
  24.   for part2 in string_to_list(part2_list_to_string):
  25.     for part2_element in part2.split(","):
  26.       part2_int = int(part2_element)
  27.       part1_list[part2_int] = "#" if (part1_list[part2_int] == ".") else "."
  28.   return is_all_off(part1_list)
  29.  
  30. total = 0
  31.  
  32. file = open("10_input.txt", "r")
  33. for line in file:
  34.   line = line.replace("\n", "")
  35.   chunks = line.split(" ")
  36.   part1 = string_without_ends(chunks.pop(0))
  37.   part1_list = []
  38.   for character in part1:
  39.     part1_list.append(character)
  40.   part3 = string_without_ends(chunks.pop())
  41.   part2_all = chunks
  42.   for index in range(0, len(part2_all)):
  43.     part2_all[index] = string_without_ends(part2_all[index])
  44.   m = -1
  45.   for r in range(1, len(part2_all) + 1):
  46.     for c in itertools.combinations(part2_all, r):
  47.       if is_valid(list_to_string(part1_list), list_to_string(c)):
  48.         m = r
  49.         break
  50.     if m > 0:
  51.       break
  52.   total += m
  53. print (total)
Advertisement
Add Comment
Please, Sign In to add comment