Solutions for 2022, as well as 2015-2018 and 2019 up to day 11

This commit is contained in:
Chris Alge 2023-03-12 15:20:02 +01:00
commit 1895197c49
722 changed files with 375457 additions and 0 deletions

View file

@ -0,0 +1,9 @@
[package]
name = "day23-safe_cracking"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
asm_interpreter = { path = "../common/asm_interpreter" }

View file

@ -0,0 +1,67 @@
\--- Day 23: Safe Cracking ---
----------
This is one of the top floors of the nicest tower in EBHQ. The Easter Bunny's private office is here, complete with a safe hidden behind a painting, and who *wouldn't* hide a star in a safe behind a painting?
The safe has a digital screen and keypad for code entry. A sticky note attached to the safe has a password hint on it: "eggs". The painting is of a large rabbit coloring some eggs. You see `7`.
When you go to type the code, though, nothing appears on the display; instead, the keypad comes apart in your hands, apparently having been smashed. Behind it is some kind of socket - one that matches a connector in your [prototype computer](11)! You pull apart the smashed keypad and extract the logic circuit, plug it into your computer, and plug your computer into the safe.
Now, you just need to figure out what output the keypad would have sent to the safe. You extract the [assembunny code](12) from the logic chip (your puzzle input).
The code looks like it uses *almost* the same architecture and instruction set that the [monorail computer](12) used! You should be able to *use the same assembunny interpreter* for this as you did there, but with one new instruction:
`tgl x` *toggles* the instruction `x` away (pointing at instructions like `jnz` does: positive means forward; negative means backward):
* For *one-argument* instructions, `inc` becomes `dec`, and all other one-argument instructions become `inc`.
* For *two-argument* instructions, `jnz` becomes `cpy`, and all other two-instructions become `jnz`.
* The arguments of a toggled instruction are *not affected*.
* If an attempt is made to toggle an instruction outside the program, *nothing happens*.
* If toggling produces an *invalid instruction* (like `cpy 1 2`) and an attempt is later made to execute that instruction, *skip it instead*.
* If `tgl` toggles *itself* (for example, if `a` is `0`, `tgl a` would target itself and become `inc a`), the resulting instruction is not executed until the next time it is reached.
For example, given this program:
```
cpy 2 a
tgl a
tgl a
tgl a
cpy 1 a
dec a
dec a
```
* `cpy 2 a` initializes register `a` to `2`.
* The first `tgl a` toggles an instruction `a` (`2`) away from it, which changes the third `tgl a` into `inc a`.
* The second `tgl a` also modifies an instruction `2` away from it, which changes the `cpy 1 a` into `jnz 1 a`.
* The fourth line, which is now `inc a`, increments `a` to `3`.
* Finally, the fifth line, which is now `jnz 1 a`, jumps `a` (`3`) instructions ahead, skipping the `dec a` instructions.
In this example, the final value in register `a` is `3`.
The rest of the electronics seem to place the keypad entry (the number of eggs, `7`) in register `a`, run the code, and then send the value left in register `a` to the safe.
*What value* should be sent to the safe?
Your puzzle answer was `12654`.
\--- Part Two ---
----------
The safe doesn't open, but it *does* make several angry noises to express its frustration.
You're quite sure your logic is working correctly, so the only other thing is... you check the painting again. As it turns out, colored eggs are still eggs. Now you count `12`.
As you run the program with this new input, the prototype computer begins to *overheat*. You wonder what's taking so long, and whether the lack of any instruction more powerful than "add one" has anything to do with it. Don't bunnies usually *multiply*?
Anyway, *what value* should actually be sent to the safe?
Your puzzle answer was `479009214`.
Both parts of this puzzle are complete! They provide two gold stars: \*\*
At this point, all that is left is for you to [admire your Advent calendar](/2016).
If you still want to see it, you can [get your puzzle input](23/input).

View file

@ -0,0 +1,33 @@
use asm_interpreter::assembly_interpreter::Cpu;
pub fn run(input: &str) -> (isize, isize) {
let mut cpu = Cpu::new(input);
let mut cpu_2 = Cpu::new(input);
cpu.set(0, 7);
cpu_2.set(0, 12);
cpu.run();
cpu_2.run();
(cpu.get(0), cpu_2.get(0))
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::read_to_string;
fn read_file(name: &str) -> String {
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
}
#[test]
fn test_sample() {
let sample_input = read_file("tests/sample_input");
assert_eq!(run(&sample_input), (3, 3));
}
#[test]
fn test_challenge() {
let challenge_input = read_file("tests/challenge_input");
assert_eq!(run(&challenge_input), (12654, 479009214));
}
}

View file

@ -0,0 +1,26 @@
cpy a b
dec b
cpy a d
cpy 0 a
cpy b c
inc a
dec c
jnz c -2
dec d
jnz d -5
dec b
cpy b c
cpy c d
dec d
inc c
jnz d -2
tgl c
cpy -16 c
jnz 1 c
cpy 81 c
jnz 94 d
inc a
inc d
jnz d -2
inc c
jnz c -5

View file

@ -0,0 +1,7 @@
cpy 2 a
tgl a
tgl a
tgl a
cpy 1 a
dec a
dec a