From 3e43a1c38f522c18b29b59c3ccde27019fbe1f38 Mon Sep 17 00:00:00 2001 From: Wesley Moore Date: Sat, 23 Dec 2017 19:45:08 +1100 Subject: [PATCH] Add day 18 solution --- 2017/day/18/Cargo.lock | 4 + 2017/day/18/Cargo.toml | 6 ++ 2017/day/18/input | 41 +++++++++ 2017/day/18/problem.txt | 42 +++++++++ 2017/day/18/src/main.rs | 183 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 276 insertions(+) create mode 100644 2017/day/18/Cargo.lock create mode 100644 2017/day/18/Cargo.toml create mode 100644 2017/day/18/input create mode 100644 2017/day/18/problem.txt create mode 100644 2017/day/18/src/main.rs diff --git a/2017/day/18/Cargo.lock b/2017/day/18/Cargo.lock new file mode 100644 index 0000000..a4065a9 --- /dev/null +++ b/2017/day/18/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "day18" +version = "0.1.0" + diff --git a/2017/day/18/Cargo.toml b/2017/day/18/Cargo.toml new file mode 100644 index 0000000..fcaa91f --- /dev/null +++ b/2017/day/18/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day18" +version = "0.1.0" +authors = ["Wesley Moore "] + +[dependencies] diff --git a/2017/day/18/input b/2017/day/18/input new file mode 100644 index 0000000..dd5f7cc --- /dev/null +++ b/2017/day/18/input @@ -0,0 +1,41 @@ +set i 31 +set a 1 +mul p 17 +jgz p p +mul a 2 +add i -1 +jgz i -2 +add a -1 +set i 127 +set p 618 +mul p 8505 +mod p a +mul p 129749 +add p 12345 +mod p a +set b p +mod b 10000 +snd b +add i -1 +jgz i -9 +jgz a 3 +rcv b +jgz b -1 +set f 0 +set i 126 +rcv a +rcv b +set p a +mul p -1 +add p b +jgz p 4 +snd a +set a b +jgz 1 3 +snd b +set f 1 +add i -1 +jgz i -11 +snd a +jgz f -16 +jgz a -19 diff --git a/2017/day/18/problem.txt b/2017/day/18/problem.txt new file mode 100644 index 0000000..4b6eebe --- /dev/null +++ b/2017/day/18/problem.txt @@ -0,0 +1,42 @@ +--- Day 18: Duet --- + +You discover a tablet containing some strange assembly code labeled simply "Duet". Rather than bother the sound card with it, you decide to run the code yourself. Unfortunately, you don't see any documentation, so you're left to figure out what the instructions mean on your own. + +It seems like the assembly is meant to operate on a set of registers that are each named with a single letter and that can each hold a single integer. You suppose each register should start with a value of 0. + +There aren't that many instructions, so it shouldn't be hard to figure out what they do. Here's what you determine: + + snd X plays a sound with a frequency equal to the value of X. + set X Y sets register X to the value of Y. + add X Y increases 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. + mod X Y sets register X to the remainder of dividing the value contained in register X by the value of Y (that is, it sets X to the result of X modulo Y). + rcv X recovers the frequency of the last sound played, but only when the value of X is not zero. (If it is zero, the command does nothing.) + jgz X Y jumps with an offset of the value of Y, but only if the value of X is greater than zero. (An offset of 2 skips the next instruction, an offset of -1 jumps to the previous instruction, and so on.) + +Many of the instructions can take either a register (a single letter) or a number. The value of a register is the integer it contains; the value of a number is that number. + +After each jump instruction, the program continues with the instruction to which the jump jumped. After any other instruction, the program continues with the next instruction. Continuing (or jumping) off either end of the program terminates it. + +For example: + +set a 1 +add a 2 +mul a a +mod a 5 +snd a +set a 0 +rcv a +jgz a -1 +set a 1 +jgz a -2 + + The first four instructions set a to 1, add 2 to it, square it, and then set it to itself modulo 5, resulting in a value of 4. + Then, a sound with frequency 4 (the value of a) is played. + After that, a is set to 0, causing the subsequent rcv and jgz instructions to both be skipped (rcv because a is 0, and jgz because a is not greater than 0). + Finally, a is set to 1, causing the next jgz instruction to activate, jumping back two instructions to another jump, which jumps again to the rcv, which ultimately triggers the recover operation. + +At the time the recover operation is executed, the frequency of the last sound played is 4. + +What is the value of the recovered frequency (the value of the most recently played sound) the first time a rcv instruction is executed with a non-zero value? + diff --git a/2017/day/18/src/main.rs b/2017/day/18/src/main.rs new file mode 100644 index 0000000..08c6c04 --- /dev/null +++ b/2017/day/18/src/main.rs @@ -0,0 +1,183 @@ +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::str::FromStr; +use std::collections::HashMap; + +type Register = char; + +#[derive(Debug)] +enum Operand { + Register(Register), + Immediate(isize), +} + +impl Operand { + fn new(register_or_value: &str) -> Self { + if let Ok(value) = isize::from_str(register_or_value) { + Operand::Immediate(value) + } + else { + Operand::Register(register_or_value.chars().next().unwrap()) + } + } + + fn value(&self, machine: &Machine) -> isize { + match *self { + // TODO: Sdd a get to Machine + Operand::Register(ref reg) => machine.registers.get(reg).map(|val| *val).unwrap_or(0), + Operand::Immediate(val) => val, + } + } +} + +#[derive(Debug)] +enum Instruction { + Snd(Operand), + Set(Operand, Operand), + Add(Operand, Operand), + Mul(Operand, Operand), + Mod(Operand, Operand), + Rcv(Operand), + Jgz(Operand, Operand), +} + +impl Instruction { + fn new(line: &str) -> Self { + use Instruction::*; + + let asm = line.split_whitespace().collect::>(); + let instruction = asm[0]; + + match instruction { + "snd" => Snd(Operand::new(asm[1])), + "set" => Set(Operand::new(asm[1]), Operand::new(asm[2])), + "add" => Add(Operand::new(asm[1]), Operand::new(asm[2])), + "mul" => Mul(Operand::new(asm[1]), Operand::new(asm[2])), + "mod" => Mod(Operand::new(asm[1]), Operand::new(asm[2])), + "rcv" => Rcv(Operand::new(asm[1])), + "jgz" => Jgz(Operand::new(asm[1]), Operand::new(asm[2])), + _ => panic!("bad instruction") + } + } +} + +struct Program(Vec); + +impl Program { + fn load(path: &str) -> Program { + let file = File::open(path).expect("unable to open input file"); + let reader = BufReader::new(file); + + let program = reader.lines() + .map(|line| Instruction::new(line.as_ref().unwrap())) + .collect::>(); + Program(program) + } +} + +// TODO: Machine executes programs and instructions +#[derive(Debug)] +struct Machine { + registers: HashMap, + pc: isize, + last_played: isize, + last_received: isize, +} + +impl Machine { + pub fn new() -> Machine { + Machine { + registers: HashMap::new(), + pc: 0, + last_played: 0, + last_received: 0, + } + } + + pub fn run(&mut self, program: &Program) { + loop { + if self.pc < 0 || self.pc >= program.0.len() as isize { + break; + } + + let instruction = &program.0[self.pc as usize]; + self.execute(instruction); + + if self.last_received != 0 { + break; + } + } + } + + fn get(&self, register: char) -> isize { + self.registers.get(®ister).map(|val| *val).unwrap_or(0) + } + + fn set(&mut self, register: char, value: isize) { + self.registers.insert(register, value); + } + + fn execute(&mut self, instruction: &Instruction) { + use Instruction::*; + + // TODO; Increment pc + + match *instruction { + Snd(ref op) => { + self.last_played = op.value(self); + } + Set(Operand::Register(reg), ref val) => { + let value = val.value(self); + self.set(reg, value); + } + Add(Operand::Register(reg), ref val) => { + let reg_value = self.get(reg); + let value = val.value(self); + self.set(reg, reg_value + value); + } + Mul(Operand::Register(reg), ref val) => { + let reg_value = self.get(reg); + let value = val.value(self); + self.set(reg, reg_value * value); + } + Mod(Operand::Register(reg), ref val) => { + let reg_value = self.get(reg); + let value = val.value(self); + self.set(reg, reg_value % value); + } + Rcv(ref op) => { + let value = op.value(self); + if value != 0 { + self.last_received = self.last_played; + } + } + Jgz(_, _) => { /* handled below */ } + _ => panic!("bad argument") + }; + + // Update PC + match *instruction { + Jgz(ref x, ref offset) => { + let x_value = x.value(self); + let offset_value = offset.value(self); + + if x_value != 0 { + self.pc += offset_value; + } + else { + self.pc += 1; + } + } + _ => self.pc += 1 + }; + } +} + +fn main() { + let program = Program::load("input"); + let mut machine = Machine::new(); + + machine.run(&program); + + println!("{:?}", machine); +}