Skip to content

Commit

Permalink
AoC day 7
Browse files Browse the repository at this point in the history
  • Loading branch information
neilstudd committed Dec 7, 2024
1 parent 89fbc0a commit fb33e0d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
60 changes: 60 additions & 0 deletions 2024/day07/day07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import sys, os, operator, itertools
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
from common import open_file, print_and_verify_answer

def generate_equations(mode):
data_file = open_file( mode + ".txt")
equations = []
for line in data_file:
parts = line.strip().split(":")
equation = [int(parts[0]), list(map(int, parts[1].split()))]
equations.append(equation)
return equations

def generate_expressions(nums, target_number, is_part_two = False):
operators = {
'+': operator.add,
'*': operator.mul
}

if is_part_two:
operators['||'] = concatenate

expressions = set()

# Generate all possible operator combinations
op_combinations = list(itertools.product(operators.keys(), repeat=len(nums)-1))

for ops in op_combinations:
result = nums[0]
for i, op in enumerate(ops):
result = operators[op](result, nums[i+1])
expressions.add(result)
if result == target_number:
return target_number

return 0

def concatenate(x, y):
return int(str(x) + str(y))

def run_part_one(mode, expected = None):
equations = generate_equations(mode)
valid_equation_sums = 0
for equation in equations:
valid_equation_sums += generate_expressions(equation[1], equation[0])
print_and_verify_answer(mode, "one", valid_equation_sums, expected)

def run_part_two(mode, expected = None):
equations = generate_equations(mode)
valid_equation_sums = 0
for equation in equations:
valid_equation_sums += generate_expressions(equation[1], equation[0], True)
print_and_verify_answer(mode, "two", valid_equation_sums, expected)

# ADD EXPECTED OUTPUTS TO TESTS HERE πŸ‘‡
run_part_one("test", 3749)
run_part_one("prod", 20665830408335)
run_part_two("test", 11387)
run_part_two("prod", 354060705047464)
# Now run it and watch the magic happen πŸͺ„
9 changes: 9 additions & 0 deletions 2024/day07/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Started in 2022, but I'll backfill previous years when I need a festive boost.
⬜ = incomplete or not attempted

### Stats
**2024:** 🟧🟧🟧🟧🟧🟧⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜ _(12)_
**2024:** 🟧🟧🟧🟧🟧🟧🟧⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜ _(14)_
**2023:** 🟨🟨🟨🟨🟧🟨🟨🟧⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜ _(16)_
**2022:** πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ¨πŸŸ§πŸŸ¨πŸŸ¨β¬›β¬œβ¬œβ¬›β¬œβ¬›πŸŸ¨β¬œπŸŸ¨β¬œβ¬› _(36)_
**2021:** 🟨🟨🟨🟨🟨🟨🟨🟨⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜ _(16)_
Expand Down

0 comments on commit fb33e0d

Please sign in to comment.