diff options
Diffstat (limited to '2016/aoc25')
-rw-r--r-- | 2016/aoc25/Cargo.lock | 98 | ||||
-rw-r--r-- | 2016/aoc25/Cargo.toml | 7 | ||||
-rw-r--r-- | 2016/aoc25/input.txt | 30 | ||||
-rw-r--r-- | 2016/aoc25/src/main.rs | 284 |
4 files changed, 419 insertions, 0 deletions
diff --git a/2016/aoc25/Cargo.lock b/2016/aoc25/Cargo.lock new file mode 100644 index 0000000..c4553c9 --- /dev/null +++ b/2016/aoc25/Cargo.lock @@ -0,0 +1,98 @@ +[root] +name = "aoc25" +version = "0.1.0" +dependencies = [ + "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 = "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 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/aoc25/Cargo.toml b/2016/aoc25/Cargo.toml new file mode 100644 index 0000000..aa84af4 --- /dev/null +++ b/2016/aoc25/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "aoc25" +version = "0.1.0" +authors = ["Justin Worthe <justin.worthe@gmail.com>"] + +[dependencies] +regex = "0.1"
\ No newline at end of file diff --git a/2016/aoc25/input.txt b/2016/aoc25/input.txt new file mode 100644 index 0000000..a9734a4 --- /dev/null +++ b/2016/aoc25/input.txt @@ -0,0 +1,30 @@ +cpy a d +cpy 7 c +cpy 362 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/aoc25/src/main.rs b/2016/aoc25/src/main.rs new file mode 100644 index 0000000..7db2ce5 --- /dev/null +++ b/2016/aoc25/src/main.rs @@ -0,0 +1,284 @@ +extern crate regex; +use regex::Regex; + +use std::io::BufReader; +use std::io::prelude::*; +use std::fs::File; + +enum Instruction { + Inc(usize), + Dec(usize), + + TglLit(i32), + TglReg(usize), + + OutLit(i32), + OutReg(usize), + + CpyLitReg(i32, usize), + CpyRegReg(usize, usize), + CpyLitLit(i32, i32), + CpyRegLit(usize, i32), + + JnzLitLit(i32, i32), + JnzRegLit(usize, i32), + JnzLitReg(i32, usize), + JnzRegReg(usize, usize), + + Noop +} + +impl Instruction { + fn parse(line: &str) -> Instruction { + match Regex::new(r"inc (a|b|c|d)").unwrap().captures(line) { + Some(cap) => { + let reg = to_register_index(cap.at(1).unwrap()); + return Instruction::Inc(reg); + }, + _ => {} + } + + match Regex::new(r"dec (a|b|c|d)").unwrap().captures(line) { + Some(cap) => { + let reg = to_register_index(cap.at(1).unwrap()); + return Instruction::Dec(reg); + }, + _ => {} + } + + + match Regex::new(r"tgl (a|b|c|d)").unwrap().captures(line) { + Some(cap) => { + let reg = to_register_index(cap.at(1).unwrap()); + return Instruction::TglReg(reg); + }, + _ => {} + } + + match Regex::new(r"tgl ([-\d]+)").unwrap().captures(line) { + Some(cap) => { + let lit = cap.at(1).unwrap().parse().unwrap(); + return Instruction::TglLit(lit); + }, + _ => {} + } + + + match Regex::new(r"out (a|b|c|d)").unwrap().captures(line) { + Some(cap) => { + let reg = to_register_index(cap.at(1).unwrap()); + return Instruction::OutReg(reg); + }, + _ => {} + } + + match Regex::new(r"out ([-\d]+)").unwrap().captures(line) { + Some(cap) => { + let lit = cap.at(1).unwrap().parse().unwrap(); + return Instruction::OutLit(lit); + }, + _ => {} + } + + + match Regex::new(r"cpy ([-\d]+) (a|b|c|d)").unwrap().captures(line) { + Some(cap) => { + let src = cap.at(1).unwrap().parse().unwrap(); + let dest = to_register_index(cap.at(2).unwrap()); + return Instruction::CpyLitReg(src, dest); + }, + _ => {} + } + + match Regex::new(r"cpy (a|b|c|d) (a|b|c|d)").unwrap().captures(line) { + Some(cap) => { + let src = to_register_index(cap.at(1).unwrap()); + let dest = to_register_index(cap.at(2).unwrap()); + return Instruction::CpyRegReg(src, dest); + }, + _ => {} + } + + match Regex::new(r"cpy ([-\d]+) ([-\d]+)").unwrap().captures(line) { + Some(cap) => { + let src = cap.at(1).unwrap().parse().unwrap(); + let dest = cap.at(2).unwrap().parse().unwrap(); + return Instruction::CpyLitLit(src, dest); + }, + _ => {} + } + + match Regex::new(r"cpy (a|b|c|d) ([-\d]+)").unwrap().captures(line) { + Some(cap) => { + let src = to_register_index(cap.at(1).unwrap()); + let dest = cap.at(2).unwrap().parse().unwrap(); + return Instruction::CpyRegReg(src, dest); + }, + _ => {} + } + + + match Regex::new(r"jnz ([-\d]+) (a|b|c|d)").unwrap().captures(line) { + Some(cap) => { + let src = cap.at(1).unwrap().parse().unwrap(); + let dest = to_register_index(cap.at(2).unwrap()); + return Instruction::JnzLitReg(src, dest); + }, + _ => {} + } + + match Regex::new(r"jnz (a|b|c|d) (a|b|c|d)").unwrap().captures(line) { + Some(cap) => { + let src = to_register_index(cap.at(1).unwrap()); + let dest = to_register_index(cap.at(2).unwrap()); + return Instruction::JnzRegReg(src, dest); + }, + _ => {} + } + + match Regex::new(r"jnz ([-\d]+) ([-\d]+)").unwrap().captures(line) { + Some(cap) => { + let src = cap.at(1).unwrap().parse().unwrap(); + let dest = cap.at(2).unwrap().parse().unwrap(); + return Instruction::JnzLitLit(src, dest); + }, + _ => {} + } + + match Regex::new(r"jnz (a|b|c|d) ([-\d]+)").unwrap().captures(line) { + Some(cap) => { + let src = to_register_index(cap.at(1).unwrap()); + let dest = cap.at(2).unwrap().parse().unwrap(); + return Instruction::JnzRegLit(src, dest); + }, + _ => {} + } + + panic!("Invalid instruction line"); + } + + fn toggle(&self) -> Self { + match *self { + Instruction::Inc(a) => Instruction::Dec(a), + Instruction::Dec(a) => Instruction::Inc(a), + + Instruction::TglLit(_) => Instruction::Noop, + Instruction::TglReg(a) => Instruction::Inc(a), + + Instruction::OutLit(_) => Instruction::Noop, + Instruction::OutReg(a) => Instruction::Inc(a), + + Instruction::CpyLitReg(a, b) => Instruction::JnzLitReg(a, b), + Instruction::CpyRegReg(a, b) => Instruction::JnzRegReg(a, b), + Instruction::CpyLitLit(a, b) => Instruction::JnzLitLit(a, b), + Instruction::CpyRegLit(a, b) => Instruction::JnzRegLit(a, b), + + Instruction::JnzLitLit(a, b) => Instruction::CpyLitLit(a, b), + Instruction::JnzRegLit(a, b) => Instruction::CpyRegLit(a, b), + Instruction::JnzLitReg(a, b) => Instruction::CpyLitReg(a, b), + Instruction::JnzRegReg(a, b) => Instruction::CpyRegReg(a, b), + Instruction::Noop => Instruction::Noop + } + } +} + +fn main() { + let expected_output = vec!(0, 1, 0, 1, 0, 1, 0, 1, 0, 1); + let min_input = (0..).find(|&i| run_program(i) == expected_output).unwrap(); + println!("{}", min_input); +} + + +fn run_program(input: i32) -> Vec<i32> { + let mut transmission = Vec::new(); + let mut program = read_file(); + + let mut registers: [i32; 4] = [input, 0, 0, 0]; + let mut pc: usize = 0; + + while pc < program.len() && transmission.len() < 10 { + let mut pc_next: usize = pc+1; + + match program[pc] { + Instruction::Inc(dest) => { + registers[dest] += 1; + }, + Instruction::Dec(dest) => { + registers[dest] -= 1; + }, + + Instruction::TglReg(offset) => { + let line_to_toggle = (pc as i32 + registers[offset]) as usize; + if line_to_toggle > 0 && line_to_toggle < program.len() { + program[line_to_toggle] = program[line_to_toggle].toggle(); + } + }, + Instruction::TglLit(offset) => { + let line_to_toggle = (pc as i32 + offset) as usize; + if line_to_toggle > 0 && line_to_toggle < program.len() { + program[line_to_toggle] = program[line_to_toggle].toggle(); + } + }, + + Instruction::OutReg(output) => { + transmission.push(registers[output]); + }, + Instruction::OutLit(output) => { + transmission.push(output); + }, + + Instruction::CpyLitReg(src, dest) => { + registers[dest] = src; + }, + Instruction::CpyRegReg(src, dest) => { + registers[dest] = registers[src]; + }, + Instruction::CpyLitLit(_, _) | Instruction::CpyRegLit(_, _) => {}, + + Instruction::JnzRegLit(condition, offset) => { + if registers[condition] != 0 { + pc_next = (pc as i32 + offset) as usize; + } + }, + Instruction::JnzLitLit(condition, offset) => { + if condition != 0 { + pc_next = (pc as i32 + offset) as usize; + } + }, + Instruction::JnzRegReg(condition, offset) => { + if registers[condition] != 0 { + pc_next = (pc as i32 + registers[offset]) as usize; + } + }, + Instruction::JnzLitReg(condition, offset) => { + if condition != 0 { + pc_next = (pc as i32 + registers[offset]) as usize; + } + }, + Instruction::Noop => {} + } + + pc = pc_next; + } + + transmission +} + +fn to_register_index(name: &str) -> usize { + match name { + "a" => 0, + "b" => 1, + "c" => 2, + "d" => 3, + _ => panic!("Invalid register provided") + } +} + +fn read_file() -> Vec<Instruction> { + let file = BufReader::new(File::open("input.txt").unwrap()); + file.lines() + .map(|line| line.unwrap()) + .filter(|line| line.len() > 0) + .map(|line| Instruction::parse(line.trim())) + .collect() +} |