Skip to content

Commit

Permalink
completed day 7 of year 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
fschatbot committed Dec 31, 2024
1 parent 0ae5adf commit 2124365
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions 2024/7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from typing import List
from itertools import product

split_data = True
completed = True
raw_data = None # Not To be touched

def part1(data:List[str]):
result = 0

for line in data:
output, numbers = line.split(': ')
numbers = numbers.split(' ')

for operators in product(['+', '*'], repeat=len(numbers) - 1):
equation = int(numbers[0])
for op, num in zip(operators, numbers[1:]):
if op == '+':
equation += int(num)
else:
equation *= int(num)

if equation == int(output):
result += int(output)
break

return result

def part2(data:List[str]):
result = 0

for line in data:
output, numbers = line.split(': ')
numbers = numbers.split(' ')

for operators in product(['+', '*', '||'], repeat=len(numbers) - 1):
equation = int(numbers[0])
for op, num in zip(operators, numbers[1:]):
if op == '+':
equation += int(num)
elif op == '*':
equation *= int(num)
else:
equation = int(str(equation)+num)

if equation == int(output):
result += int(output)
break

return result

0 comments on commit 2124365

Please sign in to comment.