From 901f6822ee8fbd05dffe4488a0a1e16d3afb71a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Doblas=20Jim=C3=A9nez?= Date: Tue, 5 Apr 2022 11:47:08 +0200 Subject: [PATCH] Solution to problem 8 in Python --- src/Year_2017/P8.py | 93 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 82 insertions(+), 11 deletions(-) diff --git a/src/Year_2017/P8.py b/src/Year_2017/P8.py index 4ddf353..ad6ee54 100644 --- a/src/Year_2017/P8.py +++ b/src/Year_2017/P8.py @@ -1,21 +1,92 @@ +# --- Day 8: I Heard You Like Registers --- + +# You receive a signal directly from the CPU. Because of your recent assistance +# with jump instructions, 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? + from collections import defaultdict +from typing import Dict with open("files/P8.txt") as f: instructions = [line for line in f.read().strip().split("\n")] -# print(instructions) -registry: dict[str, int] = defaultdict(int) - - -def inc(s: str) -> None: +def inc(s: str, dic: Dict[str, int]) -> None: reg, inst, num, iff, reg_cond, op, num_cond = s.split() - if eval("registry[reg_cond] " + op + num_cond): - registry[reg] += int(num) + if eval("dic[reg_cond] " + op + num_cond): + dic[reg] += int(num) -for instruction in instructions: - inc(instruction) - break +def dec(s: str, dic: Dict[str, int]) -> None: + reg, inst, num, iff, reg_cond, op, num_cond = s.split() + if eval("dic[reg_cond] " + op + num_cond): + dic[reg] -= int(num) -print(registry) + +def part_1() -> None: + registry: Dict[str, int] = defaultdict(int) + for instruction in instructions: + if "inc" in instruction: + inc(instruction, registry) + elif "dec" in instruction: + dec(instruction, registry) + + val = max(registry.values()) + + print(f"The largest value in the registry is {val}") + + +# --- Part Two --- + +# 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). + + +def part_2() -> None: + registry: Dict[str, int] = defaultdict(int) + max_val = 0 + for instruction in instructions: + if "inc" in instruction: + inc(instruction, registry) + elif "dec" in instruction: + dec(instruction, registry) + + max_val = max(max_val, max(registry.values())) + + print(f"The largest value ever held by the registry is {max_val}") + + +if __name__ == "__main__": + part_1() + part_2()