Added 2017/23 part 1
This commit is contained in:
parent
96e5173bd6
commit
63bf8effed
@ -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`
|
||||
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:
|
||||
|
||||
Although it hasn\'t changed, you can still [get your puzzle
|
||||
input](23/input).
|
||||
|
||||
|
@ -25,21 +25,24 @@ def parse_input(input_str):
|
||||
return None
|
||||
|
||||
def sets_return(x,Sets):
|
||||
if x in Sets:
|
||||
value = Sets[x]
|
||||
if isinstance(value, str) and not x.lstrip('-').isdigit():
|
||||
return sets_return(value)
|
||||
return value
|
||||
elif x.isdigit() or x.lstrip('-').isdigit():
|
||||
return int(x)
|
||||
else:
|
||||
return None
|
||||
if x.isalpha():
|
||||
return Sets[x]
|
||||
return int(x)
|
||||
|
||||
if part == 1:
|
||||
|
||||
instructions = []
|
||||
|
||||
Sets = {}
|
||||
Sets = {
|
||||
'a': 1,
|
||||
'b': 0,
|
||||
'c': 0,
|
||||
'd': 0,
|
||||
'e': 0,
|
||||
'f': 0,
|
||||
'g': 0,
|
||||
'h': 0
|
||||
}
|
||||
|
||||
count = 0
|
||||
|
||||
@ -47,64 +50,28 @@ if part == 1:
|
||||
for line in file:
|
||||
instructions.append(list(parse_input(line.rstrip())))
|
||||
|
||||
x = 0
|
||||
print(len(instructions))
|
||||
while True:
|
||||
i = instructions[x]
|
||||
print(i)
|
||||
if isinstance(i[1], str):
|
||||
if i[1] not in Sets:
|
||||
Sets[i[1]] = None
|
||||
intr = 0
|
||||
while 0 <= intr < len(instructions):
|
||||
|
||||
i = instructions[intr]
|
||||
|
||||
x = i[1]
|
||||
y = i[2]
|
||||
|
||||
if i[0] == 'set':
|
||||
Sets[i[1]] = sets_return(i[2],Sets)
|
||||
x += 1
|
||||
|
||||
Sets[x] = sets_return(y,Sets)
|
||||
|
||||
elif i[0] == 'sub':
|
||||
#print(sets_return(i[2],Sets))
|
||||
Sets[i[1]] -= sets_return(i[2],Sets)
|
||||
x += 1
|
||||
|
||||
Sets[x] -= sets_return(y,Sets)
|
||||
|
||||
elif i[0] == 'mul':
|
||||
Sets[i[1]] *= sets_return(i[2],Sets)
|
||||
x += 1
|
||||
Sets[x] *= sets_return(y,Sets)
|
||||
count += 1
|
||||
|
||||
elif i[0] == 'jnz':
|
||||
#tmp = sets_return(i[1],Sets)
|
||||
#print(type(tmp),tmp)
|
||||
if sets_return(i[1],Sets) != 0:
|
||||
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
|
||||
|
||||
if sets_return(x,Sets) != 0:
|
||||
intr += sets_return(y,Sets)-1
|
||||
intr += 1
|
||||
|
||||
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))
|
Loading…
Reference in New Issue
Block a user