Solved 2024/03 P1+P2
This commit is contained in:
parent
da43d0c6a6
commit
0d35287000
@ -28,8 +28,6 @@ instruction invoked?*
|
||||
|
||||
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.
|
||||
@ -50,8 +48,14 @@ 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:
|
||||
Your puzzle answer was `907`.
|
||||
|
||||
Although it hasn\'t changed, you can still [get your puzzle
|
||||
Both parts of this puzzle are complete! They provide two gold stars:
|
||||
\*\*
|
||||
|
||||
At this point, you should [return to your Advent calendar](/2017) and
|
||||
try another puzzle.
|
||||
|
||||
If you still want to see it, you can [get your puzzle
|
||||
input](23/input).
|
||||
|
||||
|
22
2017/23/input.2
Normal file
22
2017/23/input.2
Normal file
@ -0,0 +1,22 @@
|
||||
set e 2
|
||||
set g d
|
||||
mul g e
|
||||
sub g b
|
||||
jnz g 2
|
||||
set f 0
|
||||
sub e -1
|
||||
set g e
|
||||
sub g b
|
||||
jnz g -8
|
||||
sub d -1
|
||||
set g d
|
||||
sub g b
|
||||
jnz g -13
|
||||
jnz f 2
|
||||
sub h -1
|
||||
set g b
|
||||
sub g c
|
||||
jnz g 2
|
||||
jnz 1 3
|
||||
sub b -17
|
||||
jnz 1 -23
|
31
2017/23/input.3
Normal file
31
2017/23/input.3
Normal file
@ -0,0 +1,31 @@
|
||||
a = 1
|
||||
b = 79 * 100 + 100000
|
||||
c = 79 * 100 + 100000 + 17000
|
||||
|
||||
9. set f 1
|
||||
10. set d 2
|
||||
11. set e 2
|
||||
|
||||
12.
|
||||
|
||||
|
||||
if g != 0 jump to 12
|
||||
g = d * e - b
|
||||
|
||||
if g != 0
|
||||
f = 0
|
||||
g = (e - 1) - b
|
||||
|
||||
21. sub d -1
|
||||
22. set g d
|
||||
23. sub g b
|
||||
24. jnz g -13 jump to 11
|
||||
25. jnz f 2 jump to 27
|
||||
26. sub h -1
|
||||
27. set g b
|
||||
28. sub g c
|
||||
29. jnz g 2
|
||||
30. jnz 1 3
|
||||
31. sub b -17
|
||||
32. jnz 1 -23 jump to 9
|
||||
|
@ -3,10 +3,10 @@ import sys,re
|
||||
from pprint import pprint
|
||||
sys.path.insert(0, '../../')
|
||||
from fred import list2int
|
||||
|
||||
from math import sqrt
|
||||
input_f = 'input'
|
||||
|
||||
part = 1
|
||||
part = 3
|
||||
#########################################
|
||||
# #
|
||||
# Part 1 #
|
||||
@ -34,7 +34,7 @@ if part == 1:
|
||||
instructions = []
|
||||
|
||||
Sets = {
|
||||
'a': 1,
|
||||
'a': 0,
|
||||
'b': 0,
|
||||
'c': 0,
|
||||
'd': 0,
|
||||
@ -81,4 +81,72 @@ if part == 1:
|
||||
# #
|
||||
#########################################
|
||||
if part == 2:
|
||||
exit()
|
||||
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)
|
81
2024/03/3.md
Normal file
81
2024/03/3.md
Normal file
@ -0,0 +1,81 @@
|
||||
## \-\-- Day 3: Mull It Over \-\--
|
||||
|
||||
\"Our computers are having issues, so I have no idea if we have any
|
||||
Chief Historians [in
|
||||
stock]{title="There's a spot reserved for Chief Historians between the green toboggans and the red toboggans. They've never actually had any Chief Historians in stock, but it's best to be prepared."}!
|
||||
You\'re welcome to check the warehouse, though,\" says the mildly
|
||||
flustered shopkeeper at the [North Pole Toboggan Rental
|
||||
Shop](/2020/day/2). The Historians head out to take a look.
|
||||
|
||||
The shopkeeper turns to you. \"Any chance you can see why our computers
|
||||
are having issues again?\"
|
||||
|
||||
The computer appears to be trying to run a program, but its memory (your
|
||||
puzzle input) is *corrupted*. All of the instructions have been jumbled
|
||||
up!
|
||||
|
||||
It seems like the goal of the program is just to *multiply some
|
||||
numbers*. It does that with instructions like `mul(X,Y)`, where `X` and
|
||||
`Y` are each 1-3 digit numbers. For instance, `mul(44,46)` multiplies
|
||||
`44` by `46` to get a result of `2024`. Similarly, `mul(123,4)` would
|
||||
multiply `123` by `4`.
|
||||
|
||||
However, because the program\'s memory has been corrupted, there are
|
||||
also many invalid characters that should be *ignored*, even if they look
|
||||
like part of a `mul` instruction. Sequences like `mul(4*`, `mul(6,9!`,
|
||||
`?(12,34)`, or `mul ( 2 , 4 )` do *nothing*.
|
||||
|
||||
For example, consider the following section of corrupted memory:
|
||||
|
||||
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
|
||||
|
||||
Only the four highlighted sections are real `mul` instructions. Adding
|
||||
up the result of each instruction produces `161`
|
||||
(`2*4 + 5*5 + 11*8 + 8*5`).
|
||||
|
||||
Scan the corrupted memory for uncorrupted `mul` instructions. *What do
|
||||
you get if you add up all of the results of the multiplications?*
|
||||
|
||||
Your puzzle answer was `174561379`.
|
||||
|
||||
## \-\-- Part Two \-\-- {#part2}
|
||||
|
||||
As you scan through the corrupted memory, you notice that some of the
|
||||
conditional statements are also still intact. If you handle some of the
|
||||
uncorrupted conditional statements in the program, you might be able to
|
||||
get an even more accurate result.
|
||||
|
||||
There are two new instructions you\'ll need to handle:
|
||||
|
||||
- The `do()` instruction *enables* future `mul` instructions.
|
||||
- The `don't()` instruction *disables* future `mul` instructions.
|
||||
|
||||
Only the *most recent* `do()` or `don't()` instruction applies. At the
|
||||
beginning of the program, `mul` instructions are *enabled*.
|
||||
|
||||
For example:
|
||||
|
||||
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
|
||||
|
||||
This corrupted memory is similar to the example from before, but this
|
||||
time the `mul(5,5)` and `mul(11,8)` instructions are *disabled* because
|
||||
there is a `don't()` instruction before them. The other `mul`
|
||||
instructions function normally, including the one at the end that gets
|
||||
re-*enabled* by a `do()` instruction.
|
||||
|
||||
This time, the sum of the results is `48` (`2*4 + 8*5`).
|
||||
|
||||
Handle the new instructions; *what do you get if you add up all of the
|
||||
results of just the enabled multiplications?*
|
||||
|
||||
Your puzzle answer was `106921067`.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars:
|
||||
\*\*
|
||||
|
||||
At this point, you should [return to your Advent calendar](/2024) and
|
||||
try another puzzle.
|
||||
|
||||
If you still want to see it, you can [get your puzzle
|
||||
input](3/input).
|
||||
|
51
2024/03/solution.py
Normal file
51
2024/03/solution.py
Normal file
@ -0,0 +1,51 @@
|
||||
#!/bin/python3
|
||||
import sys,re
|
||||
from pprint import pprint
|
||||
sys.path.insert(0, '../../')
|
||||
from fred import list2int,get_re,nprint,lprint
|
||||
|
||||
input_f = 'input'
|
||||
|
||||
part = 2
|
||||
#########################################
|
||||
# #
|
||||
# Part 1 #
|
||||
# #
|
||||
#########################################
|
||||
result = 0
|
||||
if part == 1:
|
||||
with open(input_f) as file:
|
||||
for line in file:
|
||||
for i in re.findall(r"(mul\((\d+),{1}(\d+)\))",line.rstrip()):
|
||||
result += (int(i[1])*int(i[2]))
|
||||
|
||||
print(result)
|
||||
|
||||
|
||||
|
||||
#########################################
|
||||
# #
|
||||
# Part 2 #
|
||||
# #
|
||||
#########################################
|
||||
result = 0
|
||||
do = True
|
||||
if part == 2:
|
||||
with open(input_f) as file:
|
||||
for line in file:
|
||||
matches = re.findall(r"mul\(\d+,\d+\)|do\(\)|don't\(\)",line.rstrip())
|
||||
for match in matches:
|
||||
if match == 'do()':
|
||||
do = True
|
||||
elif match == "don't()":
|
||||
do = False
|
||||
else:
|
||||
if do:
|
||||
m = get_re(r"mul\((\d+),{1}(\d+)\)",match)
|
||||
result += (int(m.group(1))*int(m.group(2)))
|
||||
print(result)
|
||||
|
||||
# Not really happy with my code. I would prefer to find all mul(x,y) between do() and don't(),
|
||||
# instead of finding all the mul/do/don't and then turning calculation on/off.
|
||||
|
||||
#(do\(\).*?mul\((\d+),{1}(\d+)\))
|
16
2024/03/test.py
Normal file
16
2024/03/test.py
Normal file
@ -0,0 +1,16 @@
|
||||
from re import findall
|
||||
|
||||
total1 = total2 = 0
|
||||
enabled = True
|
||||
data = open('input').read()
|
||||
|
||||
for a, b, do, dont in findall(r"mul\((\d+),(\d+)\)|(do\(\))|(don't\(\))", data):
|
||||
if do or dont:
|
||||
enabled = bool(do)
|
||||
else:
|
||||
x = int(a) * int(b)
|
||||
total1 += x
|
||||
total2 += x * enabled
|
||||
|
||||
print(total1, total2)
|
||||
|
Loading…
Reference in New Issue
Block a user