Started 2017/23 part 1

This commit is contained in:
FrederikBaerentsen 2024-11-29 20:44:25 +01:00
parent 2ab82894dd
commit 96e5173bd6
6 changed files with 190 additions and 40 deletions

View File

@ -1,3 +1,4 @@
#!/bin/python3
import sys,re
from pprint import pprint

View File

@ -1,38 +0,0 @@
import re
from collections import namedtuple
Particle = namedtuple('Particle', ['pos', 'vel', 'acc'])
def parse_particle(line):
pos_match = re.search('p=<(-?\d+),(-?\d+),(-?\d+)>', line)
position = int(pos_match.group(1)), int(pos_match.group(2)), int(pos_match.group(3))
vel_match = re.search('v=<(-?\d+),(-?\d+),(-?\d+)>', line)
velocity = int(vel_match.group(1)), int(vel_match.group(2)), int(vel_match.group(3))
acc_match = re.search('a=<(-?\d+),(-?\d+),(-?\d+)>', line)
acceleration = int(acc_match.group(1)), int(acc_match.group(2)), int(acc_match.group(3))
return Particle(position, velocity, acceleration)
def move_particle(particle):
new_v = tuple(v + a for v, a in zip(particle.vel, particle.acc))
new_p = tuple(p + v for p, v in zip(particle.pos, new_v))
return Particle(new_p, new_v, particle.acc)
def manhattan(particle):
return sum(abs(k) for k in particle.pos)
if __name__ == '__main__':
particles = [parse_particle(line) for line in open('input')]
orig = particles.copy()
for _ in range(1000):
particles = [move_particle(p) for p in particles]
print(particles.index(min(particles, key=manhattan)))
particles = orig.copy()
for _ in range(1000):
if len(set(p.pos for p in particles)) < len(particles):
positions = [p.pos for p in particles]
particles = [part for part, pos in zip(particles, positions) if positions.count(pos) == 1]
print(len(particles))
particles = [move_particle(p) for p in particles]

32
2017/23/23.md Normal file
View File

@ -0,0 +1,32 @@
## \-\-- Day 23: Coprocessor Conflagration \-\--
You decide to head directly to the CPU and fix the printer from there.
As you get close, you find an *experimental coprocessor* doing so much
work that the local programs are afraid it will [halt and catch
fire](https://en.wikipedia.org/wiki/Halt_and_Catch_Fire). This would
cause serious issues for the rest of the computer, so you head in and
see what you can do.
The code it\'s running seems to be a variant of the kind you saw
recently on that [tablet](18). The general functionality seems *very
similar*, but some of the instructions are different:
- `set X Y` *sets* register `X` to the value of `Y`.
- `sub X Y` *decreases* register `X` by the value of `Y`.
- `mul X Y` sets register `X` to the result of *multiplying* the value
contained in register `X` by the value of `Y`.
- `jnz X Y` *jumps* with an offset of the value of `Y`, but only if
the value of `X` is *not zero*. (An offset of `2` skips the next
instruction, an offset of `-1` jumps to the previous instruction,
and so on.)
The coprocessor is currently set to some kind of *debug mode*, which
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).
Answer:

117
2017/23/solution.py Normal file
View File

@ -0,0 +1,117 @@
#!/bin/python3
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int
input_f = 'input'
part = 1
#########################################
# #
# 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 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 part == 1:
instructions = []
Sets = {}
count = 0
with open(input_f) as file:
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
if i[0] == 'set':
Sets[i[1]] = sets_return(i[2],Sets)
x += 1
elif i[0] == 'sub':
#print(sets_return(i[2],Sets))
Sets[i[1]] -= sets_return(i[2],Sets)
x += 1
elif i[0] == 'mul':
Sets[i[1]] *= sets_return(i[2],Sets)
x += 1
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
print(count)
#########################################
# #
# Part 2 #
# #
#########################################
if part == 2:
exit()

38
2017/23/test.py Normal file
View File

@ -0,0 +1,38 @@
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))

View File

@ -2,9 +2,9 @@
## 2017
|┌┘┌┘┌──────┴o┌─┘│ o┤ ├─┘┌────┘┌*o─┴─┘└┴───┘| 22**
|├───┬┴┴┴┤└──o┌┘└┤ FC├─*o────────┴──┘├┴┴┴┴┬──┘| 21**
|│o┬─┤ ├────┤┌─┤ LP├─┘┌──|(────────┤ ├──*| 20*
|│o┬─┤ ├────┤┌─┤ LP├─┘┌──|(────────┤ ├──*| 20**
|└─┘o┤ ├─┐o─┘└─┤ UT├─┐└───┬┴┴┴┴┴┬┐ *┤1├──┘| 19**
|┌───┤ ├─┴─────┤ XR├─┴────┤ ├┘└┤ 2├──*| 18**
|└┐o─┴┬┬┬┴───────┴┬┬┬┴──────┤ MAGI├*─┤ v├┌─┘| 17**