Added 2017/08

This commit is contained in:
FrederikBaerentsen 2024-11-22 15:12:35 +01:00
parent 70aa64c63a
commit c524009ad3
2 changed files with 193 additions and 0 deletions

57
2017/08/8.md Normal file
View File

@ -0,0 +1,57 @@
## \-\-- Day 8: I Heard You Like Registers \-\--
You receive a signal
directly from the CPU. Because of your recent assistance with [jump
instructions](5), it would like you to compute the result of a series of
unusual register instructions.
Each instruction consists of several parts: the register to modify,
whether to increase or decrease that register\'s value, the amount by
which to increase or decrease it, and a condition. If the condition
fails, skip the instruction without modifying the register. The
registers all start at `0`. The instructions look like this:
b inc 5 if a > 1
a inc 1 if b < 5
c dec -10 if a >= 1
c inc -20 if c == 10
These instructions would be processed as follows:
- Because `a` starts at `0`, it is not greater than `1`, and so `b` is
not modified.
- `a` is increased by `1` (to `1`) because `b` is less than `5` (it is
`0`).
- `c` is decreased by `-10` (to `10`) because `a` is now greater than
or equal to `1` (it is `1`).
- `c` is increased by `-20` (to `-10`) because `c` is equal to `10`.
After this process, the largest value in any register is `1`.
You might also encounter `<=` (less than or equal to) or `!=` (not equal
to). However, the CPU doesn\'t have the bandwidth to tell you what all
the registers are named, and leaves that to you to determine.
*What is the largest value in any register* after completing the
instructions in your puzzle input?
Your puzzle answer was `4066`.
## \-\-- Part Two \-\-- {#part2}
To be safe, the CPU also needs to know *the highest value held in any
register during this process* so that it can decide how much memory to
allocate to these operations. For example, in the above instructions,
the highest value ever held was `10` (in register `c` after the third
instruction was evaluated).
Your puzzle answer was `4829`.
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](8/input).

136
2017/08/solution.py Normal file
View File

@ -0,0 +1,136 @@
#!/bin/python3
import sys,re
from pprint import pprint
input_f = 'input'
part = 2
#########################################
# #
# Part 1 #
# #
#########################################
if part == 1:
grid = []
values = {}
with open(input_f) as file:
for line in file:
grid.append(line.rstrip())
#pprint(grid)
pattern = r"(\w+)\s(inc|dec)\s(-?\d+)\sif\s(\w+)\s([<>=!]+)\s(-?\d+)"
# Process each instruction
for instruction in grid:
match = re.match(pattern, instruction)
if match:
input_var = match.group(1)
change = match.group(2)
value = int(match.group(3))
con1 = match.group(4)
con2 = match.group(5)
con3 = int(match.group(6))
#print(f"input = {input_var}, change = {change}, value = {value}, con1 = {con1}, con2 = {con2}, con3 = {con3}")
else:
print(instruction)
print('NO MATCH')
exit()
#print(f"input = {input_var}, change = {change}, value = {value}, con1 = {con1}, con2 = {con2}, con3 = {con3}")
if input_var not in values:
values[input_var] = 0
if con1 not in values:
values[con1] = 0
condition = f"{values[con1]} {con2} {con3}"
#print('calculation =======',condition)
if eval(condition):
if change == 'inc':
values[input_var] += value
if change == 'dec':
values[input_var] -= value
#pprint(values)
max_v = 0
for key, i in values.items():
if max_v < i:
max_v = i
print(max_v)
#########################################
# #
# Part 2 #
# #
#########################################
def find_max(x):
#print(x)
max_v = 0
for key, i in values.items():
if max_v < i:
max_v = i
return max_v
if part == 2:
grid = []
values = {}
total_max = 0
with open(input_f) as file:
for line in file:
grid.append(line.rstrip())
#pprint(grid)
pattern = r"(\w+)\s(inc|dec)\s(-?\d+)\sif\s(\w+)\s([<>=!]+)\s(-?\d+)"
# Process each instruction
for instruction in grid:
match = re.match(pattern, instruction)
if match:
input_var = match.group(1)
change = match.group(2)
value = int(match.group(3))
con1 = match.group(4)
con2 = match.group(5)
con3 = int(match.group(6))
#print(f"input = {input_var}, change = {change}, value = {value}, con1 = {con1}, con2 = {con2}, con3 = {con3}")
else:
print(instruction)
print('NO MATCH')
exit()
#print(f"input = {input_var}, change = {change}, value = {value}, con1 = {con1}, con2 = {con2}, con3 = {con3}")
if input_var not in values:
values[input_var] = 0
if con1 not in values:
values[con1] = 0
condition = f"{values[con1]} {con2} {con3}"
#print('calculation =======',condition)
if eval(condition):
if change == 'inc':
values[input_var] += value
if change == 'dec':
values[input_var] -= value
temp_max = find_max(values)
if total_max < temp_max:
total_max = temp_max
#print('New Max Found',total_max)
print(total_max)
#pprint(values)