13
This commit is contained in:
59
13/13-1.py
Normal file
59
13/13-1.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
file = "./input.txt"
|
||||||
|
# file = "./ex.txt"
|
||||||
|
from time import time
|
||||||
|
import re
|
||||||
|
start_time = time()
|
||||||
|
|
||||||
|
def check_machine(m:dict) -> int:
|
||||||
|
min_token = 900
|
||||||
|
solution_found = False
|
||||||
|
for a in range(101):
|
||||||
|
for b in range(101):
|
||||||
|
if ((m['ba_x'] * a) + (m['bb_x'] * b) == m['p_x'] and
|
||||||
|
(m['ba_y'] * a) + (m['bb_y'] * b) == m['p_y'] and
|
||||||
|
(a*3)+b < min_token):
|
||||||
|
solution_found = True
|
||||||
|
min_token = (a*3)+b
|
||||||
|
if solution_found:
|
||||||
|
return min_token
|
||||||
|
else:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
input_dict = {}
|
||||||
|
m_count = 0
|
||||||
|
input_file = open(file, "r")
|
||||||
|
for line in input_file:
|
||||||
|
line = line.strip()
|
||||||
|
if line == "":
|
||||||
|
continue
|
||||||
|
if 'Button A' in line:
|
||||||
|
pat_b_a = r'A\: X\+(\d+), Y\+(\d+)'
|
||||||
|
match = re.search(pat_b_a, line)
|
||||||
|
input_dict[f'M_{m_count}'] = {'ba_x' :int(match.group(1))}
|
||||||
|
input_dict[f'M_{m_count}']['ba_y'] = int(match.group(2))
|
||||||
|
elif 'Button B' in line:
|
||||||
|
pat_b_b = r'B\: X\+(\d+), Y\+(\d+)'
|
||||||
|
match = re.search(pat_b_b, line)
|
||||||
|
input_dict[f'M_{m_count}']['bb_x'] = int(match.group(1))
|
||||||
|
input_dict[f'M_{m_count}']['bb_y'] = int(match.group(2))
|
||||||
|
elif 'Prize' in line:
|
||||||
|
pat_p = r'Prize\: X\=(\d+), Y\=(\d+)'
|
||||||
|
match = re.search(pat_p, line)
|
||||||
|
input_dict[f'M_{m_count}']['p_x'] = int(match.group(1))
|
||||||
|
input_dict[f'M_{m_count}']['p_y'] = int(match.group(2))
|
||||||
|
m_count += 1
|
||||||
|
input_file.close()
|
||||||
|
|
||||||
|
solution = 0
|
||||||
|
for key in input_dict.keys():
|
||||||
|
solution += check_machine(input_dict[key])
|
||||||
|
|
||||||
|
|
||||||
|
print(input_dict)
|
||||||
|
print(f'Solution: {solution}')
|
||||||
|
print(f'Runtime: {time()-start_time:.2f} s')
|
||||||
55
13/13-2.py
Normal file
55
13/13-2.py
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# like in: https://www.reddit.com/r/adventofcode/comments/1hd7irq/2024_day_13_an_explanation_of_the_mathematics/
|
||||||
|
|
||||||
|
file = "./input.txt"
|
||||||
|
#file = "./ex.txt"
|
||||||
|
from time import time
|
||||||
|
import re
|
||||||
|
start_time = time()
|
||||||
|
|
||||||
|
def check_machine(m:dict) -> int:
|
||||||
|
a = (m['p_x']*m["bb_y"] - m["p_y"]*m["bb_x"]) / (m["ba_x"]*m["bb_y"] - m["ba_y"]*m["bb_x"])
|
||||||
|
b = (m["ba_x"]*m["p_y"] - m["ba_y"]*m["p_x"]) / (m["ba_x"]*m["bb_y"] - m["ba_y"]*m["bb_x"])
|
||||||
|
if (a*10)%10==0 and (b*10)%10==0:
|
||||||
|
return int((3*a)+b)
|
||||||
|
else:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
input_dict = {}
|
||||||
|
m_count = 0
|
||||||
|
input_file = open(file, "r")
|
||||||
|
for line in input_file:
|
||||||
|
line = line.strip()
|
||||||
|
if line == "":
|
||||||
|
continue
|
||||||
|
if 'Button A' in line:
|
||||||
|
pat_b_a = r'A\: X\+(\d+), Y\+(\d+)'
|
||||||
|
match = re.search(pat_b_a, line)
|
||||||
|
input_dict[f'M_{m_count}'] = {'ba_x' :int(match.group(1))}
|
||||||
|
input_dict[f'M_{m_count}']['ba_y'] = int(match.group(2))
|
||||||
|
elif 'Button B' in line:
|
||||||
|
pat_b_b = r'B\: X\+(\d+), Y\+(\d+)'
|
||||||
|
match = re.search(pat_b_b, line)
|
||||||
|
input_dict[f'M_{m_count}']['bb_x'] = int(match.group(1))
|
||||||
|
input_dict[f'M_{m_count}']['bb_y'] = int(match.group(2))
|
||||||
|
elif 'Prize' in line:
|
||||||
|
pat_p = r'Prize\: X\=(\d+), Y\=(\d+)'
|
||||||
|
match = re.search(pat_p, line)
|
||||||
|
input_dict[f'M_{m_count}']['p_x'] = int(match.group(1)) +10000000000000
|
||||||
|
input_dict[f'M_{m_count}']['p_y'] = int(match.group(2)) +10000000000000
|
||||||
|
m_count += 1
|
||||||
|
input_file.close()
|
||||||
|
|
||||||
|
solution = 0
|
||||||
|
for key in input_dict.keys():
|
||||||
|
tok = check_machine(input_dict[key])
|
||||||
|
print(key, tok)
|
||||||
|
solution +=tok
|
||||||
|
|
||||||
|
#print(input_dict)
|
||||||
|
print(f'Solution: {solution}')
|
||||||
|
print(f'Runtime: {time()-start_time:.2f} s')
|
||||||
15
13/ex.txt
Normal file
15
13/ex.txt
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
Button A: X+94, Y+34
|
||||||
|
Button B: X+22, Y+67
|
||||||
|
Prize: X=8400, Y=5400
|
||||||
|
|
||||||
|
Button A: X+26, Y+66
|
||||||
|
Button B: X+67, Y+21
|
||||||
|
Prize: X=12748, Y=12176
|
||||||
|
|
||||||
|
Button A: X+17, Y+86
|
||||||
|
Button B: X+84, Y+37
|
||||||
|
Prize: X=7870, Y=6450
|
||||||
|
|
||||||
|
Button A: X+69, Y+23
|
||||||
|
Button B: X+27, Y+71
|
||||||
|
Prize: X=18641, Y=10279
|
||||||
1279
13/input.txt
Normal file
1279
13/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user