Added 2017/23 part 1
This commit is contained in:
+26
-1
@@ -26,7 +26,32 @@ allows for testing, but prevents it from doing any meaningful work.
|
|||||||
If you run the program (your puzzle input), *how many times is the `mul`
|
If you run the program (your puzzle input), *how many times is the `mul`
|
||||||
instruction invoked?*
|
instruction invoked?*
|
||||||
|
|
||||||
To begin, [get your puzzle input](23/input).
|
Your puzzle answer was `5929`.
|
||||||
|
|
||||||
|
The first half of this puzzle is complete! It provides one gold star: \*
|
||||||
|
|
||||||
|
## \-\-- Part Two \-\-- {#part2}
|
||||||
|
|
||||||
|
Now, it\'s time to fix the problem.
|
||||||
|
|
||||||
|
The *debug mode switch* is wired directly to register `a`. You [flip the
|
||||||
|
switch]{title="From 'magic' to 'more magic'."}, which makes *register
|
||||||
|
`a` now start at `1`* when the program is executed.
|
||||||
|
|
||||||
|
Immediately, the coprocessor begins to overheat. Whoever wrote this
|
||||||
|
program obviously didn\'t choose a very efficient implementation.
|
||||||
|
You\'ll need to *optimize the program* if it has any hope of completing
|
||||||
|
before Santa needs that printer working.
|
||||||
|
|
||||||
|
The coprocessor\'s ultimate goal is to determine the final value left in
|
||||||
|
register `h` once the program completes. Technically, if it had that\...
|
||||||
|
it wouldn\'t even need to run the program.
|
||||||
|
|
||||||
|
After setting register `a` to `1`, if the program were to run to
|
||||||
|
completion, *what value would be left in register `h`?*
|
||||||
|
|
||||||
Answer:
|
Answer:
|
||||||
|
|
||||||
|
Although it hasn\'t changed, you can still [get your puzzle
|
||||||
|
input](23/input).
|
||||||
|
|
||||||
|
|||||||
+28
-61
@@ -25,21 +25,24 @@ def parse_input(input_str):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def sets_return(x,Sets):
|
def sets_return(x,Sets):
|
||||||
if x in Sets:
|
if x.isalpha():
|
||||||
value = Sets[x]
|
return Sets[x]
|
||||||
if isinstance(value, str) and not x.lstrip('-').isdigit():
|
return int(x)
|
||||||
return sets_return(value)
|
|
||||||
return value
|
|
||||||
elif x.isdigit() or x.lstrip('-').isdigit():
|
|
||||||
return int(x)
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
if part == 1:
|
if part == 1:
|
||||||
|
|
||||||
instructions = []
|
instructions = []
|
||||||
|
|
||||||
Sets = {}
|
Sets = {
|
||||||
|
'a': 1,
|
||||||
|
'b': 0,
|
||||||
|
'c': 0,
|
||||||
|
'd': 0,
|
||||||
|
'e': 0,
|
||||||
|
'f': 0,
|
||||||
|
'g': 0,
|
||||||
|
'h': 0
|
||||||
|
}
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
|
|
||||||
@@ -47,64 +50,28 @@ if part == 1:
|
|||||||
for line in file:
|
for line in file:
|
||||||
instructions.append(list(parse_input(line.rstrip())))
|
instructions.append(list(parse_input(line.rstrip())))
|
||||||
|
|
||||||
x = 0
|
intr = 0
|
||||||
print(len(instructions))
|
while 0 <= intr < len(instructions):
|
||||||
while True:
|
|
||||||
i = instructions[x]
|
i = instructions[intr]
|
||||||
print(i)
|
|
||||||
if isinstance(i[1], str):
|
x = i[1]
|
||||||
if i[1] not in Sets:
|
y = i[2]
|
||||||
Sets[i[1]] = None
|
|
||||||
|
|
||||||
if i[0] == 'set':
|
if i[0] == 'set':
|
||||||
Sets[i[1]] = sets_return(i[2],Sets)
|
Sets[x] = sets_return(y,Sets)
|
||||||
x += 1
|
|
||||||
|
|
||||||
elif i[0] == 'sub':
|
elif i[0] == 'sub':
|
||||||
#print(sets_return(i[2],Sets))
|
Sets[x] -= sets_return(y,Sets)
|
||||||
Sets[i[1]] -= sets_return(i[2],Sets)
|
|
||||||
x += 1
|
|
||||||
|
|
||||||
elif i[0] == 'mul':
|
elif i[0] == 'mul':
|
||||||
Sets[i[1]] *= sets_return(i[2],Sets)
|
Sets[x] *= sets_return(y,Sets)
|
||||||
x += 1
|
|
||||||
count += 1
|
count += 1
|
||||||
|
|
||||||
elif i[0] == 'jnz':
|
elif i[0] == 'jnz':
|
||||||
#tmp = sets_return(i[1],Sets)
|
if sets_return(x,Sets) != 0:
|
||||||
#print(type(tmp),tmp)
|
intr += sets_return(y,Sets)-1
|
||||||
if sets_return(i[1],Sets) != 0:
|
intr += 1
|
||||||
x += sets_return(i[2],Sets)
|
|
||||||
|
|
||||||
if x > len(instructions) or x < 0:
|
|
||||||
exit()
|
|
||||||
print(x,i)
|
|
||||||
print(Sets)
|
|
||||||
input()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#else:
|
|
||||||
# x += 1
|
|
||||||
|
|
||||||
# print(x)
|
|
||||||
# input()
|
|
||||||
|
|
||||||
# elif i[0] == 'mod':
|
|
||||||
# Sets[i[1]] %= sets_return(i[2],Sets)
|
|
||||||
# x += 1
|
|
||||||
|
|
||||||
# elif i[0] == 'snd':
|
|
||||||
# last_sound = sets_return(i[1],Sets)
|
|
||||||
# x += 1
|
|
||||||
|
|
||||||
# elif i[0] == 'rcv':
|
|
||||||
# if sets_return(i[1]) != 0:
|
|
||||||
# Sets[i[1]] = sets_return(i[1],Sets)
|
|
||||||
# #print(last_sound)
|
|
||||||
# exit()
|
|
||||||
# x += 1
|
|
||||||
|
|
||||||
|
|
||||||
print(count)
|
print(count)
|
||||||
|
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
from collections import defaultdict
|
|
||||||
|
|
||||||
|
|
||||||
with open('input') as f:
|
|
||||||
instructions = f.readlines()
|
|
||||||
|
|
||||||
|
|
||||||
def solve(part):
|
|
||||||
registers = defaultdict(int)
|
|
||||||
registers['a'] = part - 1
|
|
||||||
interpret = lambda val: registers[val] if val.isalpha() else int(val)
|
|
||||||
i = 0
|
|
||||||
while i < 11:
|
|
||||||
op, reg, val = instructions[i].split()
|
|
||||||
if op == 'set':
|
|
||||||
registers[reg] = interpret(val)
|
|
||||||
elif op == 'sub':
|
|
||||||
registers[reg] -= interpret(val)
|
|
||||||
elif op == 'mul':
|
|
||||||
registers[reg] *= interpret(val)
|
|
||||||
elif op == 'jnz':
|
|
||||||
if interpret(reg) != 0:
|
|
||||||
i += interpret(val)
|
|
||||||
continue
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
if part == 1:
|
|
||||||
return (registers['b'] - registers['e']) * (registers['b'] - registers['d'])
|
|
||||||
else:
|
|
||||||
nonprimes = 0
|
|
||||||
for b in range(registers['b'], registers['c']+1, 17):
|
|
||||||
if any(b % d == 0 for d in range(2, int(b**0.5))):
|
|
||||||
nonprimes += 1
|
|
||||||
return nonprimes
|
|
||||||
|
|
||||||
|
|
||||||
print(solve(part=1))
|
|
||||||
print(solve(part=2))
|
|
||||||
Reference in New Issue
Block a user