My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add solution for 2024/07

+36
+36
2024/day07.py
··· 1 + import fileinput 2 + import itertools 3 + 4 + from utils import parse_nums 5 + 6 + 7 + def solve(target, operands, part_2=False): 8 + ops = ['+', '*'] 9 + if part_2: 10 + ops.append('||') 11 + 12 + for operators in itertools.product(ops, repeat=(len(operands))): 13 + ans = operands[0] 14 + for operand, operator in zip(operands[1:], operators): 15 + if operator == '+': 16 + ans += operand 17 + elif operator == '*': 18 + ans *= operand 19 + else: 20 + ans = int('{}{}'.format(ans, operand)) 21 + 22 + if int(ans) == target: 23 + return target 24 + 25 + return 0 26 + 27 + part_1 = 0 28 + part_2 = 0 29 + 30 + for line in fileinput.input(): 31 + target, *operands = parse_nums(line) 32 + part_1 += solve(target, operands) 33 + part_2 += solve(target, operands, part_2=True) 34 + 35 + print("Part 1:", part_1) 36 + print("Part 2:", part_2)