From 486144352b303bb2d21a4d8a4116cf872a558d8d Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Fri, 13 Dec 2024 21:45:55 +0100 Subject: [PATCH] Solved 2024/13 P2 --- 2024/13/13.md | 12 ++++++++---- 2024/13/solution.py | 48 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/2024/13/13.md b/2024/13/13.md index 18ffa52..9c5b48e 100644 --- a/2024/13/13.md +++ b/2024/13/13.md @@ -83,8 +83,6 @@ tokens you would have to spend to win all possible prizes?* Your puzzle answer was `30973`. -The first half of this puzzle is complete! It provides one gold star: \* - ## \-\-- Part Two \-\-- {#part2} As you go to win the first prize, you discover that the claw is nowhere @@ -119,8 +117,14 @@ Using the corrected prize coordinates, figure out how to win as many prizes as possible. *What is the fewest tokens you would have to spend to win all possible prizes?* -Answer: +Your puzzle answer was `95688837203288`. -Although it hasn\'t changed, you can still [get your puzzle +Both parts of this puzzle are complete! They provide two gold stars: +\*\* + +At this point, you should [return to your Advent calendar](/2024) and +try another puzzle. + +If you still want to see it, you can [get your puzzle input](13/input). diff --git a/2024/13/solution.py b/2024/13/solution.py index 46a1eec..ddfa762 100644 --- a/2024/13/solution.py +++ b/2024/13/solution.py @@ -14,7 +14,6 @@ input_f = 'input' # # ######################################### def part1(): - instructions = [] with open(input_f) as file: @@ -23,7 +22,6 @@ def part1(): if match: instructions.append(list2int([match.group(1),match.group(2)])) - #print(instructions) a_cost = 3 b_cost = 1 @@ -34,7 +32,6 @@ def part1(): result = 0 for idx, inst in enumerate(instructions): - #print(inst) if idx%3 == 0: a = (inst[0],inst[1]) @@ -46,15 +43,13 @@ def part1(): eq2 = Eq(a[1]*x + b[1]*y,r[1]) s = solve((eq1,eq2),(x,y)) - #print(s) if s: result += s[x]*a_cost result += s[y]*b_cost - #input() return result start_time = time.time() -print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms') +#rint('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms') ######################################### @@ -62,8 +57,43 @@ print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms') # Part 2 # # # ######################################### -def part1(): - return +def part2(): + instructions = [] + + with open(input_f) as file: + for line in file: + match = get_re(r".*X[\+|=](\d+), Y[\+|=](\d+)",line.rstrip()) + if match: + instructions.append(list2int([match.group(1),match.group(2)])) + + a_cost = 3 + b_cost = 1 + + a = () + b = () + r = () + x,y = symbols('x y',integer=True) + result = 0 + + for idx, inst in enumerate(instructions): + + if idx%3 == 0: + a = (inst[0],inst[1]) + elif idx%3 == 1: + b = (inst[0],inst[1]) + elif idx%3 == 2: + r = (inst[0]+10000000000000,inst[1]+10000000000000) + eq1 = Eq(a[0]*x + b[0]*y,r[0]) + eq2 = Eq(a[1]*x + b[1]*y,r[1]) + + s = solve((eq1,eq2),(x,y)) + if s: + result += s[x]*a_cost + result += s[y]*b_cost + #input() + return result start_time = time.time() -print('Part 2:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms') \ No newline at end of file + +start_time = time.time() +print('Part 2:',part2(), '\t\t', round((time.time() - start_time)*1000), 'ms') \ No newline at end of file