import sys from math import log10 import os sys.path.insert(1, os.path.abspath('../../')) from python_tools.aoc_utils import * def look_for_solution_p1(ans, input): if (len(input) == 1): return (ans == input[0]) else: ans1 = look_for_solution_p1(ans-input[-1], input[:-1]) ans2 = look_for_solution_p1(ans/input[-1], input[:-1]) return ans1 or ans2 def digits(n): return int(log10(n)) + 1 def look_for_solution_p2(ans, input): if (len(input) == 1): return (ans == input[0]) else: ans1, ans2, ans3 = False, False, False if (ans-input[-1]) >= 0: ans1 = look_for_solution_p2(ans-input[-1], input[:-1]) if int(ans/input[-1]) == ans/input[-1]: ans2 = look_for_solution_p2(ans/input[-1], input[:-1]) #first check ans is a int if int(ans) == ans: if input[-1] == int(str(int(ans))[-digits(input[-1]):]): new_ans = str(int(ans))[:-digits(input[-1])] if new_ans == '': ans = look_for_solution_p2(0, input[:-1]) # print("SD:LFKJSDF", input, ans) # return True else: ans3 = look_for_solution_p2(int(new_ans), input[:-1]) return ans1 or ans2 or ans3 def part_one(g): p1 = 0 for i in g: if look_for_solution_p1(i, g[i]): p1+=i print("Part One: ", p1) def part_two(g): p2 = 0 for i in g: if look_for_solution_p2(i, g[i]): p2+=i print("Part Two: ", p2) G = {} def main(): contents = file2list("input.txt") for i in contents: x = i.split(":") G[int(x[0])] = list(map(int, x[1].split())) # print(G) part_one(G) part_two(G) if __name__ == "__main__": main()