2024-11-29 20:44:25 +01:00
|
|
|
#!/bin/python3
|
|
|
|
import sys,re
|
|
|
|
from pprint import pprint
|
|
|
|
sys.path.insert(0, '../../')
|
|
|
|
from fred import list2int
|
2024-12-03 17:23:25 +01:00
|
|
|
from math import sqrt
|
2024-11-29 20:44:25 +01:00
|
|
|
input_f = 'input'
|
|
|
|
|
2024-12-03 17:23:25 +01:00
|
|
|
part = 3
|
2024-11-29 20:44:25 +01:00
|
|
|
#########################################
|
|
|
|
# #
|
|
|
|
# Part 1 #
|
|
|
|
# #
|
|
|
|
#########################################
|
|
|
|
def parse_input(input_str):
|
|
|
|
pattern = r"^([a-zA-Z]{3})\s{1}(\w)?\s?(-?\w+)$"
|
|
|
|
|
|
|
|
match = re.match(pattern, input_str)
|
|
|
|
if match:
|
|
|
|
if match.group(2):
|
|
|
|
return match.group(1),match.group(2),match.group(3)
|
|
|
|
else:
|
|
|
|
return match.group(1),match.group(3)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def sets_return(x,Sets):
|
2024-11-29 22:09:10 +01:00
|
|
|
if x.isalpha():
|
|
|
|
return Sets[x]
|
|
|
|
return int(x)
|
2024-11-29 20:44:25 +01:00
|
|
|
|
|
|
|
if part == 1:
|
|
|
|
|
|
|
|
instructions = []
|
|
|
|
|
2024-11-29 22:09:10 +01:00
|
|
|
Sets = {
|
2024-12-03 17:23:25 +01:00
|
|
|
'a': 0,
|
2024-11-29 22:09:10 +01:00
|
|
|
'b': 0,
|
|
|
|
'c': 0,
|
|
|
|
'd': 0,
|
|
|
|
'e': 0,
|
|
|
|
'f': 0,
|
|
|
|
'g': 0,
|
|
|
|
'h': 0
|
|
|
|
}
|
2024-11-29 20:44:25 +01:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
with open(input_f) as file:
|
|
|
|
for line in file:
|
|
|
|
instructions.append(list(parse_input(line.rstrip())))
|
|
|
|
|
2024-11-29 22:09:10 +01:00
|
|
|
intr = 0
|
|
|
|
while 0 <= intr < len(instructions):
|
2024-11-29 20:44:25 +01:00
|
|
|
|
2024-11-29 22:09:10 +01:00
|
|
|
i = instructions[intr]
|
2024-11-29 20:44:25 +01:00
|
|
|
|
2024-11-29 22:09:10 +01:00
|
|
|
x = i[1]
|
|
|
|
y = i[2]
|
2024-11-29 20:44:25 +01:00
|
|
|
|
2024-11-29 22:09:10 +01:00
|
|
|
if i[0] == 'set':
|
|
|
|
Sets[x] = sets_return(y,Sets)
|
|
|
|
|
|
|
|
elif i[0] == 'sub':
|
|
|
|
Sets[x] -= sets_return(y,Sets)
|
|
|
|
|
2024-11-29 20:44:25 +01:00
|
|
|
elif i[0] == 'mul':
|
2024-11-29 22:09:10 +01:00
|
|
|
Sets[x] *= sets_return(y,Sets)
|
2024-11-29 20:44:25 +01:00
|
|
|
count += 1
|
|
|
|
|
|
|
|
elif i[0] == 'jnz':
|
2024-11-29 22:09:10 +01:00
|
|
|
if sets_return(x,Sets) != 0:
|
|
|
|
intr += sets_return(y,Sets)-1
|
|
|
|
intr += 1
|
2024-11-29 20:44:25 +01:00
|
|
|
|
|
|
|
print(count)
|
|
|
|
|
|
|
|
#########################################
|
|
|
|
# #
|
|
|
|
# Part 2 #
|
|
|
|
# #
|
|
|
|
#########################################
|
|
|
|
if part == 2:
|
2024-12-03 17:23:25 +01:00
|
|
|
instructions = []
|
|
|
|
|
|
|
|
Sets = {
|
|
|
|
'a': 1,
|
|
|
|
'b': 107900,
|
|
|
|
'c': 124900,
|
|
|
|
'd': 3,
|
|
|
|
'e': 107900,
|
|
|
|
'f': 1,
|
|
|
|
'g': -107897,
|
|
|
|
'h': 0
|
|
|
|
}
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
with open(input_f) as file:
|
|
|
|
for line in file:
|
|
|
|
instructions.append(list(parse_input(line.rstrip())))
|
|
|
|
|
|
|
|
intr = 0
|
|
|
|
while 0 <= intr < len(instructions):
|
|
|
|
|
|
|
|
i = instructions[intr]
|
|
|
|
|
|
|
|
x = i[1]
|
|
|
|
y = i[2]
|
|
|
|
|
|
|
|
if i[0] == 'set':
|
|
|
|
Sets[x] = sets_return(y,Sets)
|
|
|
|
|
|
|
|
elif i[0] == 'sub':
|
|
|
|
Sets[x] -= sets_return(y,Sets)
|
|
|
|
|
|
|
|
elif i[0] == 'mul':
|
|
|
|
Sets[x] *= sets_return(y,Sets)
|
|
|
|
count += 1
|
|
|
|
|
|
|
|
elif i[0] == 'jnz':
|
|
|
|
if sets_return(x,Sets) != 0:
|
|
|
|
intr += sets_return(y,Sets)-1
|
|
|
|
intr += 1
|
|
|
|
print(i)
|
|
|
|
print(Sets)
|
|
|
|
input()
|
|
|
|
print(count)
|
|
|
|
|
|
|
|
|
|
|
|
if part == 3:
|
|
|
|
b = 107900
|
|
|
|
d = 2
|
|
|
|
e = 2
|
|
|
|
h = 0
|
|
|
|
for x in range(107900,124900,17):
|
|
|
|
for y in range(2,x):
|
|
|
|
if x%y == 0:
|
|
|
|
print(x,y,h)
|
|
|
|
h+=1
|
|
|
|
print(h)
|
|
|
|
|
|
|
|
# def is_composite(n):
|
|
|
|
# if n < 2:
|
|
|
|
# return False
|
|
|
|
# for i in range(2, int(sqrt(n)) + 1):
|
|
|
|
# if n % i == 0:
|
|
|
|
# return True
|
|
|
|
# return False
|
|
|
|
|
|
|
|
# h = sum(1 for b in range(107900, 124901, 17) if is_composite(b))
|
|
|
|
# print(h)
|