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')
|
||||
Reference in New Issue
Block a user