152 lines
3.2 KiB
Python
152 lines
3.2 KiB
Python
#!/bin/python3
|
|
import sys,re
|
|
from pprint import pprint
|
|
sys.path.insert(0, '../../')
|
|
from fred import list2int
|
|
from math import sqrt
|
|
input_f = 'input'
|
|
|
|
part = 3
|
|
#########################################
|
|
# #
|
|
# 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):
|
|
if x.isalpha():
|
|
return Sets[x]
|
|
return int(x)
|
|
|
|
if part == 1:
|
|
|
|
instructions = []
|
|
|
|
Sets = {
|
|
'a': 0,
|
|
'b': 0,
|
|
'c': 0,
|
|
'd': 0,
|
|
'e': 0,
|
|
'f': 0,
|
|
'g': 0,
|
|
'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(count)
|
|
|
|
#########################################
|
|
# #
|
|
# Part 2 #
|
|
# #
|
|
#########################################
|
|
if part == 2:
|
|
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) |