diff --git a/2016/day10/Cargo.lock b/2016/day10/Cargo.lock new file mode 100644 index 0000000..75a0d52 --- /dev/null +++ b/2016/day10/Cargo.lock @@ -0,0 +1,98 @@ +[root] +name = "day10" +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/day10/Cargo.toml b/2016/day10/Cargo.toml new file mode 100644 index 0000000..d6faf84 --- /dev/null +++ b/2016/day10/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day10" +version = "0.1.0" +authors = ["~krowbar"] + +[dependencies] +regex = "0.1" \ No newline at end of file diff --git a/2016/day10/src/main.rs b/2016/day10/src/main.rs new file mode 100644 index 0000000..4892961 --- /dev/null +++ b/2016/day10/src/main.rs @@ -0,0 +1,60 @@ +extern crate regex; + +use regex::Regex; +use std::io::Read; +use std::fs::File; + + +//I'd like to solve this with a tree or node of sorts but don't know how +enum Pass { + Bot, + Output +} + +struct Bot { + id: i32, //the identifier for this bot + chips: Vec, //the value of the chips a bot can receive + low: i32, //where to pass the low chip + high: i32, //where to pass the high chip +} + +struct Output { + id: i32, //the identifier for this output + chips: Vec, +} + +fn pass_chip(all_bots: Vec, bot_id: i32, value: i32) { + let bot: Bot = all_bots.iter().find(|&b| b.id == bot_id).unwrap(); + bot.chips.push(value); + if bot.chips.len() == 2 { + pass_chip(all_bots, bot.low, bot.chips.iter().min().unwrap()); + pass_chip(all_bots, bot.high, bot.chips.iter().max().unwrap()); + } +} + +fn main() { + let mut file = File::open("./data").expect("could not open file"); + let mut input = String::new(); + file.read_to_string(&mut input).expect("could not read file"); + + let re_bot = Regex::new(r"bot (\d+) gives low to (bot|output) (\d+) and high to (bot|output) (\d+)").unwrap(); + let re_val = Regex::new(r"value (\d+) goes to bot (\d+)").unwrap(); + + let bots: Vec = Vec::new(); + + for line in input.lines() { + if re_bot.is_match(line) { + let cap = re_bot.captures(line).unwrap(); + let bot = Bot { id: cap.at(1).unwrap().parse().unwrap(), chips: vec![], low: None, high: None }; + println!("This dude {} will give to {} and {}", cap.at(1).unwrap(), cap.at(3).unwrap(), cap.at(5).unwrap()); + } + else if re_val.is_match(line) { + let cap = re_val.captures(line).unwrap(); + + } + } + + //for each line of input + // either add a new bot "node" to the tree + // or give a chip to a bot and let it propogate through the tree +} diff --git a/2016/day11/Cargo.toml b/2016/day11/Cargo.toml new file mode 100644 index 0000000..d777565 --- /dev/null +++ b/2016/day11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day11" +version = "0.1.0" +authors = ["~krowbar"] + +[dependencies] diff --git a/2016/day11/src/main.rs b/2016/day11/src/main.rs new file mode 100644 index 0000000..df2764e --- /dev/null +++ b/2016/day11/src/main.rs @@ -0,0 +1,11 @@ +// Setup +// F1 EE PG PM .. .. .. .. .. .. .. .. +// F2 .. .. .. CG .. UG .. RG .. PG .. +// F3 .. .. .. .. CM .. UC .. RC .. PC +// F4 .. .. .. .. .. .. .. .. .. .. .. + +// Goal: Use the elevator to get all generator and microchip pairs to the fourth floor + +fn main() { + println!("Hello, world!"); +} diff --git a/2016/day12/Cargo.lock b/2016/day12/Cargo.lock new file mode 100644 index 0000000..09d2bc6 --- /dev/null +++ b/2016/day12/Cargo.lock @@ -0,0 +1,98 @@ +[root] +name = "day12" +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/day12/Cargo.toml b/2016/day12/Cargo.toml new file mode 100644 index 0000000..8588d69 --- /dev/null +++ b/2016/day12/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day12" +version = "0.1.0" +authors = ["~krowbar"] + +[dependencies] +regex = "0.1" \ No newline at end of file diff --git a/2016/day12/src/main.rs b/2016/day12/src/main.rs new file mode 100644 index 0000000..68ae479 --- /dev/null +++ b/2016/day12/src/main.rs @@ -0,0 +1,114 @@ +extern crate regex; + +use regex::Regex; +use std::io::Read; +use std::fs::File; + +// registers a, b, c, d indexed at 0 +// cpy x y copies x (either an integer or the value of a register) into register y. +// inc x increases the value of register x by one. +// dec x decreases the value of register x by one. +// jnz x y jumps to an instruction y away (positive means forward; negative means backward), but only if x is not zero. + +#[derive(Debug)] +enum Operator { + CpyR { from: u8, dest: u8 }, //from register + CpyV { value: i64, dest: u8}, //from value + Inc { reg: u8}, + Dec { reg: u8}, + JnzR { reg: u8, dist: i32}, //from register + JnzV { flag: i64, dist: i32}, //from value +} + +fn reg_to_idx(name: &str) -> u8 { + match name { + "a" => 0, + "b" => 1, + "c" => 2, + "d" => 3, + _ => 0, + } +} + +fn main() { + let mut ops: Vec = Vec::new(); + + //**** Read in the file input and convert it to a Vec of Operations ****// + let mut file = File::open("./data").expect("could not open file"); + let mut input = String::new(); + file.read_to_string(&mut input).expect("could not read file"); + + let re_cpy = Regex::new(r"^cpy (.*) (.*)$").unwrap(); + let re_inc = Regex::new(r"^inc (.*)$").unwrap(); + let re_dec = Regex::new(r"^dec (.*)$").unwrap(); + let re_jnz = Regex::new(r"^jnz (.*) (.*)$").unwrap(); + + for line in input.lines() { + if re_cpy.is_match(line) { + let cap = re_cpy.captures(line).unwrap(); + let reg2 = reg_to_idx(cap.at(2).unwrap()); + match cap.at(1).unwrap().parse::() { + Ok(v) => ops.push(Operator::CpyV { value: v, dest: reg2 }), + Err(e) => ops.push(Operator::CpyR { from: reg_to_idx(cap.at(1).unwrap()), dest: reg2 }), + }; + } + else if re_inc.is_match(line) { + let cap = re_inc.captures(line).unwrap(); + let value = cap.at(1).unwrap(); + ops.push(Operator::Inc { reg: reg_to_idx(value) }); + } + else if re_dec.is_match(line) { + let cap = re_dec.captures(line).unwrap(); + let value = cap.at(1).unwrap(); + ops.push(Operator::Dec { reg: reg_to_idx(value) }); + } + else if re_jnz.is_match(line) { + let cap = re_jnz.captures(line).unwrap(); + let val2 = cap.at(2).unwrap().parse::().unwrap(); + match cap.at(1).unwrap().parse::() { + Ok(v) => ops.push(Operator::JnzV { flag: v, dist: val2 }), + Err(e) => ops.push(Operator::JnzR { reg: reg_to_idx(cap.at(1).unwrap()), dist: val2 }), + }; + } + } + //println!("{:?}", ops); + println!("Converted input values"); + + //**** loop through the Operations vector ****// + //part1 started registers with all 0s, part 2 sets 'c' to 1 + let mut registers: Vec = vec![0, 0, 1, 0]; //registers 'a', 'b', 'c', and 'd' + let mut idx = 0; + println!("Processing {} operations", ops.len()); + 'ops_loop: while idx < ops.len() { + //print!("Run[{}] {:?}", idx, ops[idx]); + match ops[idx] { + Operator::CpyR{ from, dest } => { + registers[dest as usize] = registers[from as usize]; + }, + Operator::CpyV{ value, dest } => { + registers[dest as usize] = value; + }, + Operator::Inc{ reg } => { + registers[reg as usize] = registers[reg as usize] + 1; + }, + Operator::Dec{ reg } => { + registers[reg as usize] = registers[reg as usize] - 1; + }, + Operator::JnzR{ reg, dist } => { + if registers[reg as usize] != 0 { + idx = (idx as i32 + dist) as usize; + continue 'ops_loop; + } + }, + Operator::JnzV{ flag, dist } => { + if flag != 0 { + idx = (idx as i32 + dist) as usize; + continue 'ops_loop; + } + }, + }; + //println!(" -> {:?}", registers); + idx = idx + 1; + } + println!("Registers {:?}", registers); +} diff --git a/2016/day13/Cargo.lock b/2016/day13/Cargo.lock new file mode 100644 index 0000000..908a5b3 --- /dev/null +++ b/2016/day13/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "day13" +version = "0.1.0" + diff --git a/2016/day13/Cargo.toml b/2016/day13/Cargo.toml new file mode 100644 index 0000000..d27e6a4 --- /dev/null +++ b/2016/day13/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day13" +version = "0.1.0" +authors = ["~krowbar"] + +[dependencies] diff --git a/2016/day13/src/main.rs b/2016/day13/src/main.rs new file mode 100644 index 0000000..360798c --- /dev/null +++ b/2016/day13/src/main.rs @@ -0,0 +1,137 @@ +#[derive(Debug)] +enum Space { + Wall, + Open, +} + +fn get_space(x: u32, y: u32, favorite_number: u32) -> Space { + let total: u32 = x*x + 3*x + 2*x*y + y + y*y + favorite_number; + //println!("{}, {}", total, total.count_ones()); + if total.count_ones() % 2 == 0 { /*even*/ + Space::Open + } + else { /*odd*/ + Space::Wall + } +} + +// fn print_maze(width: u32, height: u32, key: u32) { +// //print header +// print!(" "); +// for col in 0..width { +// print!("{}", col % 10); +// } +// println!(""); +// for row in 0..height { +// print!("{} ", row % 10); +// for col in 0..width { +// match get_space(col, row, key) { +// Space::Wall => print!("#"), +// Space::Open => print!("."), +// } +// } +// println!(""); +// } +// } + +const MAZE_WIDTH: usize = 40; +const MAZE_HEIGHT: usize = 50; +const MAX_STEPS: u32 = 50; +static mut COUNT: u32 = 0; + +fn build_maze(maze: &mut Vec>, key: u32) { + for row in 0..MAZE_HEIGHT { + for col in 0..MAZE_WIDTH { + match get_space(col as u32, row as u32, key) { + Space::Open => maze[row][col] = (true, u32::max_value()), + Space::Wall => maze[row][col] = (false, 0), + } + } + } +} + +fn walk_maze(maze: &mut Vec>, pos: (usize, usize), step: u32) { + if maze[pos.0][pos.1].0 == false /* if position is a wall */ + || step >= maze[pos.0][pos.1].1 /* or position has a better step value */ { + return; // abort now + } + // otherwise + // if this space is within MAX_STEPS and hasn't yet been counted + if maze[pos.0][pos.1].1 == u32::max_value() { //if we never visited this space before + if step <= MAX_STEPS { + unsafe { + COUNT = COUNT + 1; + } + } + } + else if maze[pos.0][pos.1].1 > MAX_STEPS && step <= MAX_STEPS { + unsafe { + COUNT = COUNT + 1; + } + } + + // set the step value + maze[pos.0][pos.1].1 = step; + + //check left + if pos.1 > 0 { + walk_maze(maze, (pos.0, pos.1-1), step+1); + } + + //check right + if pos.1 + 1 < MAZE_WIDTH { + walk_maze(maze, (pos.0, pos.1+1), step+1); + } + + //check up + if pos.0 > 0 { + walk_maze(maze, (pos.0-1, pos.1), step+1); + } + + //check down + if pos.0 + 1 < MAZE_HEIGHT { + walk_maze(maze, (pos.0+1, pos.1), step+1); + } +} + +fn print_maze(maze: &Vec>) { + print!(" "); //first row padding + for col in 0..MAZE_WIDTH { + print!("{:02} ", col % 100); + } + println!(""); //newline + for row in 0..MAZE_HEIGHT { + print!("{:02}: ", row % 100); + for col in 0..MAZE_WIDTH { + if maze[row][col].0 == true { + if maze[row][col].1 == u32::max_value() { + print!(".. "); //unpassable + } + else { + print!("{:02} ", maze[row][col].1 % 100); //step value + } + } + else { + print!("## "); //it's a wall + } + } + println!(""); //print the new line + } +} + +fn main() { + //let mut pos = (1, 1); + //let goal = (31, 39); + + //println!("Answer is {:?}", get_space(1, 2, 3)); + let mut maze: Vec> = vec![vec![(false, 0); MAZE_WIDTH]; MAZE_HEIGHT]; + //print_maze(32, 40, 1358); + build_maze(&mut maze, 1358); + walk_maze(&mut maze, (1, 1), 0); + //print_maze(&maze); + let end = (31, 39); + println!("Part 1: It takes {} steps to get to ({}, {})", maze[end.1][end.0].1, end.0, end.1); + unsafe { + println!("Part 2: There are {} positions within {} steps", COUNT, MAX_STEPS); + } +}