Skip to content

Commit

Permalink
2024 Day 7 Part 2 Completed
Browse files Browse the repository at this point in the history
  • Loading branch information
pcorliss committed Dec 7, 2024
1 parent 61a0428 commit 415fe80
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
12 changes: 10 additions & 2 deletions 2024/07/daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def parse(input_text):
return out

def add_symbols(goal, nums, symbols):
for symbols in itertools.product(['*', '+'], repeat=len(nums) - 1):
for symbols in itertools.product(symbols, repeat=len(nums) - 1):
equation = [nums[0]]
sum = nums[0]
for i in range(1, len(nums)):
Expand All @@ -22,6 +22,8 @@ def add_symbols(goal, nums, symbols):
sum *= nums[i]
elif symbols[i - 1] == '+':
sum += nums[i]
elif symbols[i - 1] == '||':
sum = int(f"{sum}{nums[i]}")
else:
raise f"Unknown symbol {equation}"

Expand All @@ -40,7 +42,13 @@ def part1(input_text):
return sum

def part2(input_text):
return 0
data = parse(input_text)
sum = 0
for goal, nums in data.items():
equation = add_symbols(goal, nums, ['*', '+', '||'])
if equation:
sum += goal
return sum

if __name__ == "__main__":
with open(__file__.rsplit('/', 1)[0] + "/input.txt", 'r') as file:
Expand Down
30 changes: 25 additions & 5 deletions 2024/07/daily_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def test_add_symbols_one_pos(sample_data):

assert symbolized_data == [10, '*', 19]

def test_add_symbols_two_pos(sample_data):
symbolized_data = add_symbols(3267, [81,40,27], ['+', '*'])
# def test_add_symbols_two_pos(sample_data):
# symbolized_data = add_symbols(3267, [81,40,27], ['+', '*'])

# Symbols can be swapped so test may be unstable
assert symbolized_data == [81, '*', 40, '+', 27]
# # Symbols can be swapped so test may be unstable
# assert symbolized_data == [81, '*', 40, '+', 27]

def test_add_symbols_left_to_right(sample_data):
symbolized_data = add_symbols(292, [11,6,16,20], ['+', '*'])
Expand All @@ -43,5 +43,25 @@ def test_add_symbols_unsolvable(sample_data):

assert symbolized_data is None

# 156: 15 6 can be made true through a single concatenation: 15 || 6 = 156.
# 7290: 6 8 6 15 can be made true using 6 * 8 || 6 * 15.
# 192: 17 8 14 can be made true using 17 || 8 + 14.

def test_add_symbols_concatenation(sample_data):
symbolized_data = add_symbols(156, [15,6], ['+', '*', '||'])

assert symbolized_data == [15, '||', 6]

symbolized_data = add_symbols(7290, [6,8,6,15], ['+', '*', '||'])

assert symbolized_data == [6, '*', 8, '||', 6, '*', 15]

symbolized_data = add_symbols(192, [17,8,14], ['+', '*', '||'])

assert symbolized_data == [17, '||', 8, '+', 14]

def test_part1(sample_data):
assert part1(sample_data) == 3749
assert part1(sample_data) == 3749

def test_part2(sample_data):
assert part2(sample_data) == 11387

0 comments on commit 415fe80

Please sign in to comment.