Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Input: lines like "[<part1>] (<part2>) ... {<part3>}"
- # part1 = string of . #
- # part2, part3 = comma-separated list of numbers
- # Output: each part2 button toggles elements of that line's part1 (0-indexed)
- # find fewest # presses to get each line from all "." to part1
- # ignore part3 for this half
- import itertools
- def string_without_ends(s):
- return s[1:len(s)-1]
- def list_to_string(l):
- return "/".join(l)
- def string_to_list(s):
- return s.split("/")
- def is_all_off(s):
- return not ("#" in s)
- def is_valid(part1_list_to_string, part2_list_to_string):
- part1_list = string_to_list(part1_list_to_string)
- for part2 in string_to_list(part2_list_to_string):
- for part2_element in part2.split(","):
- part2_int = int(part2_element)
- part1_list[part2_int] = "#" if (part1_list[part2_int] == ".") else "."
- return is_all_off(part1_list)
- total = 0
- file = open("10_input.txt", "r")
- for line in file:
- line = line.replace("\n", "")
- chunks = line.split(" ")
- part1 = string_without_ends(chunks.pop(0))
- part1_list = []
- for character in part1:
- part1_list.append(character)
- part3 = string_without_ends(chunks.pop())
- part2_all = chunks
- for index in range(0, len(part2_all)):
- part2_all[index] = string_without_ends(part2_all[index])
- m = -1
- for r in range(1, len(part2_all) + 1):
- for c in itertools.combinations(part2_all, r):
- if is_valid(list_to_string(part1_list), list_to_string(c)):
- m = r
- break
- if m > 0:
- break
- total += m
- print (total)
Advertisement
Add Comment
Please, Sign In to add comment