Solutions for 2022, as well as 2015-2018 and 2019 up to day 11
This commit is contained in:
commit
1895197c49
722 changed files with 375457 additions and 0 deletions
8
2015/day23-opening_the_turing_lock/Cargo.toml
Normal file
8
2015/day23-opening_the_turing_lock/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day23-opening_the_turing_lock"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
44
2015/day23-opening_the_turing_lock/challenge.txt
Normal file
44
2015/day23-opening_the_turing_lock/challenge.txt
Normal file
|
@ -0,0 +1,44 @@
|
|||
\--- Day 23: Opening the Turing Lock ---
|
||||
----------
|
||||
|
||||
Little Jane Marie just got her very first computer for Christmas from some unknown benefactor. It comes with instructions and an example program, but the computer itself seems to be malfunctioning. She's curious what the program does, and would like you to help her run it.
|
||||
|
||||
The manual explains that the computer supports two [registers](https://en.wikipedia.org/wiki/Processor_register) and six [instructions](https://en.wikipedia.org/wiki/Instruction_set) (truly, it goes on to remind the reader, a state-of-the-art technology). The registers are named `a` and `b`, can hold any [non-negative integer](https://en.wikipedia.org/wiki/Natural_number), and begin with a value of `0`. The instructions are as follows:
|
||||
|
||||
* `hlf r` sets register `r` to *half* its current value, then continues with the next instruction.
|
||||
* `tpl r` sets register `r` to *triple* its current value, then continues with the next instruction.
|
||||
* `inc r` *increments* register `r`, adding `1` to it, then continues with the next instruction.
|
||||
* `jmp offset` is a *jump*; it continues with the instruction `offset` away *relative to itself*.
|
||||
* `jie r, offset` is like `jmp`, but only jumps if register `r` is *even* ("jump if even").
|
||||
* `jio r, offset` is like `jmp`, but only jumps if register `r` is `1` ("jump if *one*", not odd).
|
||||
|
||||
All three jump instructions work with an *offset* relative to that instruction. The offset is always written with a prefix `+` or `-` to indicate the direction of the jump (forward or backward, respectively). For example, `jmp +1` would simply continue with the next instruction, while `jmp +0` would continuously jump back to itself forever.
|
||||
|
||||
The program exits when it tries to run an instruction beyond the ones defined.
|
||||
|
||||
For example, this program sets `a` to `2`, because the `jio` instruction causes it to skip the `tpl` instruction:
|
||||
|
||||
```
|
||||
inc a
|
||||
jio a, +2
|
||||
tpl a
|
||||
inc a
|
||||
|
||||
```
|
||||
|
||||
What is *the value in register `b`* when the program in your puzzle input is finished executing?
|
||||
|
||||
Your puzzle answer was `255`.
|
||||
|
||||
\--- Part Two ---
|
||||
----------
|
||||
|
||||
The unknown benefactor is *very* thankful for releasi-- er, helping little Jane Marie with her computer. Definitely not to distract you, what is the value in register `b` after the program is finished executing if register `a` starts as `1` instead?
|
||||
|
||||
Your puzzle answer was `334`.
|
||||
|
||||
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](/2015).
|
||||
|
||||
If you still want to see it, you can [get your puzzle input](23/input).
|
116
2015/day23-opening_the_turing_lock/src/lib.rs
Normal file
116
2015/day23-opening_the_turing_lock/src/lib.rs
Normal file
|
@ -0,0 +1,116 @@
|
|||
#[derive(Debug)]
|
||||
enum Instruction {
|
||||
Half(usize),
|
||||
Triple(usize),
|
||||
Increment(usize),
|
||||
Jump(isize),
|
||||
JumpIfEven(usize, isize),
|
||||
JumpIfOne(usize, isize),
|
||||
}
|
||||
|
||||
impl Instruction {
|
||||
fn parse(line: &str) -> Self {
|
||||
let components: Vec<_> = line.split(" ").collect();
|
||||
let register = components[1].bytes().next().unwrap().saturating_sub(b'a') as usize;
|
||||
let offset = if components.len() == 3 {
|
||||
components[2].parse::<isize>().unwrap()
|
||||
} else {
|
||||
components[1].parse::<isize>().unwrap_or(0)
|
||||
};
|
||||
match components[0] {
|
||||
"hlf" => Self::Half(register),
|
||||
"tpl" => Self::Triple(register),
|
||||
"inc" => Self::Increment(register),
|
||||
"jmp" => Self::Jump(offset),
|
||||
"jie" => Self::JumpIfEven(register, offset),
|
||||
"jio" => Self::JumpIfOne(register, offset),
|
||||
_ => panic!("Unexpected token in {line}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Cpu {
|
||||
instructions: Vec<Instruction>,
|
||||
registers: [usize; 2],
|
||||
current_instruction_index: usize,
|
||||
}
|
||||
|
||||
impl Cpu {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
instructions: Vec::new(),
|
||||
registers: [0, 0],
|
||||
current_instruction_index: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn step(&mut self) -> bool {
|
||||
if self.current_instruction_index >= self.instructions.len() {
|
||||
return true;
|
||||
}
|
||||
let next_instruction = &self.instructions[self.current_instruction_index];
|
||||
self.current_instruction_index += 1;
|
||||
match next_instruction {
|
||||
Instruction::Half(r) => self.registers[*r] /= 2,
|
||||
Instruction::Triple(r) => self.registers[*r] *= 3,
|
||||
Instruction::Increment(r) => self.registers[*r] += 1,
|
||||
Instruction::Jump(offset) => self.current_instruction_index = (self.current_instruction_index as isize + *offset - 1) as usize,
|
||||
Instruction::JumpIfEven(r, offset) => {
|
||||
if self.registers[*r] % 2 == 0 {
|
||||
self.current_instruction_index = (self.current_instruction_index as isize + *offset - 1) as usize;
|
||||
}
|
||||
},
|
||||
Instruction::JumpIfOne(r, offset) => {
|
||||
if self.registers[*r] == 1 {
|
||||
self.current_instruction_index = (self.current_instruction_index as isize + *offset - 1) as usize;
|
||||
}
|
||||
},
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(input: &str) -> (usize, usize) {
|
||||
let mut cpu = Cpu::new();
|
||||
let instructions = input.lines().map(Instruction::parse).collect();
|
||||
cpu.instructions = instructions;
|
||||
loop {
|
||||
let finished = cpu.step();
|
||||
if finished {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let first = cpu.registers[1];
|
||||
cpu.registers = [1, 0];
|
||||
cpu.current_instruction_index = 0;
|
||||
loop {
|
||||
let finished = cpu.step();
|
||||
if finished {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let second = cpu.registers[1];
|
||||
(first, second)
|
||||
}
|
||||
|
||||
#[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)[..])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sample() {
|
||||
let sample_input = read_file("tests/sample_input");
|
||||
assert_eq!(run(&sample_input), (2, 2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = read_file("tests/challenge_input");
|
||||
assert_eq!(run(&challenge_input), (255, 334));
|
||||
}
|
||||
}
|
48
2015/day23-opening_the_turing_lock/tests/challenge_input
Normal file
48
2015/day23-opening_the_turing_lock/tests/challenge_input
Normal file
|
@ -0,0 +1,48 @@
|
|||
jio a, +22
|
||||
inc a
|
||||
tpl a
|
||||
tpl a
|
||||
tpl a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
inc a
|
||||
tpl a
|
||||
jmp +19
|
||||
tpl a
|
||||
tpl a
|
||||
tpl a
|
||||
tpl a
|
||||
inc a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
inc a
|
||||
tpl a
|
||||
inc a
|
||||
tpl a
|
||||
tpl a
|
||||
jio a, +8
|
||||
inc b
|
||||
jie a, +4
|
||||
tpl a
|
||||
inc a
|
||||
jmp +2
|
||||
hlf a
|
||||
jmp -7
|
4
2015/day23-opening_the_turing_lock/tests/sample_input
Normal file
4
2015/day23-opening_the_turing_lock/tests/sample_input
Normal file
|
@ -0,0 +1,4 @@
|
|||
inc b
|
||||
jio b, +2
|
||||
tpl b
|
||||
inc b
|
Loading…
Add table
Add a link
Reference in a new issue