mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 10:19:55 +00:00
Add day 18 solution
This commit is contained in:
parent
6f0eed583e
commit
3e43a1c38f
5 changed files with 276 additions and 0 deletions
4
2017/day/18/Cargo.lock
generated
Normal file
4
2017/day/18/Cargo.lock
generated
Normal file
|
@ -0,0 +1,4 @@
|
|||
[[package]]
|
||||
name = "day18"
|
||||
version = "0.1.0"
|
||||
|
6
2017/day/18/Cargo.toml
Normal file
6
2017/day/18/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "day18"
|
||||
version = "0.1.0"
|
||||
authors = ["Wesley Moore <wes@wezm.net>"]
|
||||
|
||||
[dependencies]
|
41
2017/day/18/input
Normal file
41
2017/day/18/input
Normal file
|
@ -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
|
42
2017/day/18/problem.txt
Normal file
42
2017/day/18/problem.txt
Normal file
|
@ -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?
|
||||
|
183
2017/day/18/src/main.rs
Normal file
183
2017/day/18/src/main.rs
Normal file
|
@ -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::<Vec<&str>>();
|
||||
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<Instruction>);
|
||||
|
||||
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::<Vec<Instruction>>();
|
||||
Program(program)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Machine executes programs and instructions
|
||||
#[derive(Debug)]
|
||||
struct Machine {
|
||||
registers: HashMap<char, isize>,
|
||||
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);
|
||||
}
|
Loading…
Reference in a new issue