From 9ce16b772fd350feb5f3562e89a0558200e4033b Mon Sep 17 00:00:00 2001 From: logaritmisk Date: Mon, 26 Dec 2016 01:57:25 +0100 Subject: [PATCH] 2016/25 --- 2016/25/Cargo.lock | 105 ++++++++++++++++ 2016/25/Cargo.toml | 8 ++ 2016/25/input.txt | 30 +++++ 2016/25/src/main.rs | 300 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 443 insertions(+) create mode 100644 2016/25/Cargo.lock create mode 100644 2016/25/Cargo.toml create mode 100644 2016/25/input.txt create mode 100644 2016/25/src/main.rs diff --git a/2016/25/Cargo.lock b/2016/25/Cargo.lock new file mode 100644 index 0000000..e9e0353 --- /dev/null +++ b/2016/25/Cargo.lock @@ -0,0 +1,105 @@ +[root] +name = "25" +version = "0.1.0" +dependencies = [ + "lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "aho-corasick" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "lazy_static" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "memchr" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "0.1.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "thread-id" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "utf8-ranges" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66" +"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +"checksum lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "cf186d1a8aa5f5bee5fd662bc9c1b949e0259e1bcc379d1f006847b0080c7417" +"checksum libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a51822fc847e7a8101514d1d44e354ba2ffa7d4c194dcab48870740e327cac70" +"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20" +"checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f" +"checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957" +"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" +"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5" +"checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f" +"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" +"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/2016/25/Cargo.toml b/2016/25/Cargo.toml new file mode 100644 index 0000000..9ced5c0 --- /dev/null +++ b/2016/25/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "25" +version = "0.1.0" +authors = ["logaritmisk "] + +[dependencies] +lazy_static = "0.1" +regex = "0.1" diff --git a/2016/25/input.txt b/2016/25/input.txt new file mode 100644 index 0000000..3907038 --- /dev/null +++ b/2016/25/input.txt @@ -0,0 +1,30 @@ +cpy a d +cpy 11 c +cpy 231 b +inc d +dec b +jnz b -2 +dec c +jnz c -5 +cpy d a +jnz 0 0 +cpy a b +cpy 0 a +cpy 2 c +jnz b 2 +jnz 1 6 +dec b +dec c +jnz c -4 +inc a +jnz 1 -7 +cpy 2 b +jnz c 2 +jnz 1 4 +dec b +dec c +jnz 1 -4 +jnz 0 0 +out b +jnz a -19 +jnz 1 -21 diff --git a/2016/25/src/main.rs b/2016/25/src/main.rs new file mode 100644 index 0000000..f305760 --- /dev/null +++ b/2016/25/src/main.rs @@ -0,0 +1,300 @@ +#[macro_use] extern crate lazy_static; +extern crate regex; + +use std::collections::HashMap; +use std::str::FromStr; +use std::io::{self, BufRead}; +use std::i64; + +use regex::Regex; + + +type Register = String; +type Value = i64; + + +#[derive(Debug, PartialEq, Clone)] +pub enum Input { + Value(Value), + Register(Register) +} + +impl FromStr for Input { + type Err = (); + + fn from_str(s: &str) -> Result { + if let Ok(value) = s.parse::() { + return Ok(Input::Value(value)); + } + + if let Ok(register) = s.parse::() { + return Ok(Input::Register(register)); + } + + Err(()) + } +} + + +#[derive(Debug, PartialEq, Clone)] +pub enum Operation { + Cpy(Input, Register), + Inc(Register), + Dec(Register), + Jnz(Input, Input), + Tgl(Register), + Out(Input) +} + +impl FromStr for Operation { + type Err = (); + + fn from_str(s: &str) -> Result { + lazy_static! { + static ref CPY: Regex = Regex::new(r"^cpy ([a-z]|-?\d+) ([a-z])$").unwrap(); + static ref INC: Regex = Regex::new(r"^inc ([a-z])$").unwrap(); + static ref DEC: Regex = Regex::new(r"^dec ([a-z])$").unwrap(); + static ref JNZ: Regex = Regex::new(r"^jnz ([a-z]|-?\d+) ([a-z]|-?\d+)$").unwrap(); + static ref TGL: Regex = Regex::new(r"^tgl ([a-z]+)$").unwrap(); + static ref OUT: Regex = Regex::new(r"^out ([a-z]|-?\d+)$").unwrap(); + } + + if let Some(caps) = CPY.captures(s) { + let input = caps.at(1).unwrap().parse::().unwrap(); + let register = caps.at(2).unwrap().parse::().unwrap(); + + return Ok(Operation::Cpy(input, register)); + } + + if let Some(caps) = INC.captures(s) { + let register = caps.at(1).unwrap().parse::().unwrap(); + + return Ok(Operation::Inc(register)); + } + + if let Some(caps) = DEC.captures(s) { + let register = caps.at(1).unwrap().parse::().unwrap(); + + return Ok(Operation::Dec(register)); + } + + if let Some(caps) = JNZ.captures(s) { + let input = caps.at(1).unwrap().parse::().unwrap(); + let delta = caps.at(2).unwrap().parse::().unwrap(); + + return Ok(Operation::Jnz(input, delta)); + } + + if let Some(caps) = TGL.captures(s) { + let register = caps.at(1).unwrap().parse::().unwrap(); + + return Ok(Operation::Tgl(register)); + } + + if let Some(caps) = OUT.captures(s) { + let value = caps.at(1).unwrap().parse::().unwrap(); + + return Ok(Operation::Out(value)); + } + + Err(()) + } +} + + +#[derive(Debug)] +pub struct CPU { + pc: usize, + reg: HashMap, + output: Vec, + rom: Vec +} + +impl CPU { + pub fn new() -> Self { + CPU { + pc: 0, + reg: HashMap::new(), + output: Vec::new(), + rom: Vec::new() + } + } + + pub fn load(&mut self, input: Vec) { + self.rom = input; + } + + pub fn clear(&mut self) { + self.pc = 0; + self.reg.clear(); + self.output.clear(); + } + + pub fn execute(&mut self) { + loop { + if let Err(()) = self.step() { + break; + } + } + } + + pub fn step(&mut self) -> Result<(), ()> { + if self.pc >= self.rom.len() { + return Err(()); + } + + let mut swap = None; + + match self.rom[self.pc] { + Operation::Cpy(ref value, ref register) => { + let input = match value { + &Input::Value(value) => { + value + } + &Input::Register(ref register) => { + *self.reg.entry(register.clone()).or_insert(0) + } + }; + + *self.reg.entry(register.clone()).or_insert(0) = input; + + self.pc += 1; + } + Operation::Inc(ref register) => { + *self.reg.entry(register.clone()).or_insert(0) += 1; + + self.pc += 1; + } + Operation::Dec(ref register) => { + *self.reg.entry(register.clone()).or_insert(0) -= 1; + + self.pc += 1; + } + Operation::Jnz(ref value, ref delta) => { + let input = match value { + &Input::Value(value) => { + value + } + &Input::Register(ref register) => { + *self.reg.entry(register.clone()).or_insert(0) + } + }; + + let delta = match delta { + &Input::Value(value) => { + value + } + &Input::Register(ref register) => { + *self.reg.entry(register.clone()).or_insert(0) + } + }; + + if input != 0 { + self.pc = ((self.pc as i64) + delta) as usize; + } else { + self.pc += 1; + } + } + Operation::Tgl(ref register) => { + let index = self.pc as i64 + *self.reg.entry(register.clone()).or_insert(0); + + if index >= 0 && index < self.rom.len() as i64 { + swap = match &self.rom[index as usize] { + &Operation::Inc(ref register) => { + Some((index as usize, Operation::Dec(register.clone()))) + } + &Operation::Dec(ref register) => { + Some((index as usize, Operation::Inc(register.clone()))) + } + &Operation::Tgl(ref register) => { + Some((index as usize, Operation::Inc(register.clone()))) + } + &Operation::Jnz(ref value, ref delta) => { + if let &Input::Register(ref register) = delta { + Some((index as usize, Operation::Cpy(value.clone(), register.clone()))) + } else { + None + } + } + &Operation::Cpy(ref value, ref register) => { + Some((index as usize, Operation::Jnz(value.clone(), Input::Register(register.clone())))) + } + &Operation::Out(_) => { + None + } + }; + } + + self.pc += 1; + } + Operation::Out(ref value) => { + let value = match value { + &Input::Value(value) => { + value + } + &Input::Register(ref register) => { + *self.reg.entry(register.clone()).or_insert(0) + } + }; + + self.output.push(value); + + self.pc += 1; + } + } + + if let Some((pc, swap)) = swap { + self.rom[pc] = swap; + } + + return Ok(()); + } +} + + +fn main() { + let stdin = io::stdin(); + + let operations = stdin.lock().lines() + .filter_map(|x| x.ok()) + .map(|line| line.parse::().unwrap()) + .collect::>(); + + // Part 1. + let mut cpu = CPU::new(); + + cpu.load(operations.clone()); + + 'main: for i in 0..i64::MAX { + cpu.clear(); + cpu.reg.entry("a".to_owned()).or_insert(i); + + while cpu.output.len() < 100_000 { + let _ = cpu.step(); + + if cpu.output.len() > 0 { + let last = cpu.output.last().unwrap(); + + if *last != (cpu.output.len() - 1) as i64 % 2 { + continue 'main; + } + } + } + + println!("a={:#?}", i); + println!("{:#?}", cpu.reg); + + break; + } +} + + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_jnz() { + assert_eq!(Ok(Operation::Jnz(Input::Register("c".to_owned()), -2)), "jnz c -2".parse::()); + } +}