add day 7 part 2
This commit is contained in:
32
day7/code.py
32
day7/code.py
@@ -11,23 +11,33 @@ def parse_input(lines: list):
|
|||||||
equations.append(eq)
|
equations.append(eq)
|
||||||
return equations
|
return equations
|
||||||
|
|
||||||
def solve1(eqs):
|
def _sum2(l: list[int]) -> list[int]:
|
||||||
return sum([e[0] if may_be_true(e[0], e[1]) else 0 for e in eqs])
|
return [l[0] + l[1], *l[2:]]
|
||||||
|
|
||||||
def may_be_true(val: int, lst: list[int]) -> bool:
|
def _mul2(l: list[int]) -> list[int]:
|
||||||
|
return [l[0] * l[1], *l[2:]]
|
||||||
|
|
||||||
|
def _concat2(l: list[int]) -> list[int]:
|
||||||
|
return [int(str(l[0]) + str(l[1])), *l[2:]]
|
||||||
|
|
||||||
|
def may_be_true(val: int, lst: list[int], funcs) -> bool:
|
||||||
if len(lst) == 1:
|
if len(lst) == 1:
|
||||||
return lst[0] == val
|
return lst[0] == val
|
||||||
|
|
||||||
if may_be_true(val, [lst[0]+lst[1], *lst[2:]]):
|
return any([may_be_true(val, f(lst), funcs) for f in funcs])
|
||||||
return True
|
|
||||||
elif may_be_true(val, [lst[0] * lst[1], *lst[2:]]):
|
def solve1(eqs):
|
||||||
return True
|
funcs = [_sum2, _mul2]
|
||||||
|
return sum([e[0] if may_be_true(e[0], e[1], funcs) else 0 for e in eqs])
|
||||||
|
|
||||||
|
def solve2(eqs):
|
||||||
|
funcs = [_sum2, _mul2, _concat2]
|
||||||
|
return sum([e[0] if may_be_true(e[0], e[1], funcs) else 0 for e in eqs])
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
lines = Input('input_test.txt').lines()
|
#lines = Input('input_test.txt').lines()
|
||||||
#lines = Input('input.txt').lines()
|
lines = Input('input.txt').lines()
|
||||||
|
|
||||||
eq = parse_input(lines)
|
eq = parse_input(lines)
|
||||||
|
|
||||||
@@ -35,4 +45,4 @@ if __name__ == '__main__':
|
|||||||
print('part 1:', solve1(eq))
|
print('part 1:', solve1(eq))
|
||||||
|
|
||||||
#part 2
|
#part 2
|
||||||
print('part 2:', '')
|
print('part 2:', solve2(eq))
|
||||||
|
@@ -47,4 +47,6 @@ Adding up all six test values (the three that could be made before using only +
|
|||||||
|
|
||||||
Using your new knowledge of elephant hiding spots, determine which equations could possibly be true. What is their total calibration result?
|
Using your new knowledge of elephant hiding spots, determine which equations could possibly be true. What is their total calibration result?
|
||||||
|
|
||||||
|
Your puzzle answer was 227921760109726.
|
||||||
|
|
||||||
|
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||||
|
Reference in New Issue
Block a user