From 5cdf361ed295325f16121887640b61c53a734398 Mon Sep 17 00:00:00 2001 From: Russell Champoux Date: Tue, 20 Dec 2016 17:41:14 -0500 Subject: [PATCH] Day 17 solution --- 2016/day10/src/main.rs | 12 +++---- 2016/day17/Cargo.lock | 81 ++++++++++++++++++++++++++++++++++++++++++ 2016/day17/Cargo.toml | 7 ++++ 2016/day17/src/main.rs | 80 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 174 insertions(+), 6 deletions(-) create mode 100644 2016/day17/Cargo.lock create mode 100644 2016/day17/Cargo.toml create mode 100644 2016/day17/src/main.rs diff --git a/2016/day10/src/main.rs b/2016/day10/src/main.rs index 4892961..2294058 100644 --- a/2016/day10/src/main.rs +++ b/2016/day10/src/main.rs @@ -14,8 +14,8 @@ enum Pass { 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 + low: Option, //where to pass the low chip + high: Option, //where to pass the high chip } struct Output { @@ -23,12 +23,12 @@ struct 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(); +fn pass_chip(all_bots: &Vec, bot_id: i32, value: i32) { + let mut 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()); + pass_chip(all_bots, bot.low.unwrap(), *bot.chips.iter().clone().min().unwrap()); + pass_chip(all_bots, bot.high.unwrap(), *bot.chips.iter().clone().max().unwrap()); } } diff --git a/2016/day17/Cargo.lock b/2016/day17/Cargo.lock new file mode 100644 index 0000000..52c5546 --- /dev/null +++ b/2016/day17/Cargo.lock @@ -0,0 +1,81 @@ +[root] +name = "day17" +version = "0.1.0" +dependencies = [ + "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "gcc" +version = "0.3.40" +source = "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 = "rand" +version = "0.3.15" +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 = "rust-crypto" +version = "0.2.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.40 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.22" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "time" +version = "0.1.35" +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)", + "winapi 0.2.8 (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 gcc 0.3.40 (registry+https://github.com/rust-lang/crates.io-index)" = "872db9e59486ef2b14f8e8c10e9ef02de2bccef6363d7f34835dedb386b3d950" +"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 rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d" +"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a" +"checksum rustc-serialize 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "237546c689f20bb44980270c73c3b9edd0891c1be49cc1274406134a66d3957b" +"checksum time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "3c7ec6d62a20df54e07ab3b78b9a3932972f4b7981de295563686849eb3989af" +"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/day17/Cargo.toml b/2016/day17/Cargo.toml new file mode 100644 index 0000000..6885090 --- /dev/null +++ b/2016/day17/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day17" +version = "0.1.0" +authors = ["~krowbar"] + +[dependencies] +rust-crypto = "^0.2" \ No newline at end of file diff --git a/2016/day17/src/main.rs b/2016/day17/src/main.rs new file mode 100644 index 0000000..668c7fd --- /dev/null +++ b/2016/day17/src/main.rs @@ -0,0 +1,80 @@ +extern crate crypto; + +use crypto::md5::Md5; +use crypto::digest::Digest; + +// If you are at the goal +// return +// else +// For each possible direction you can go +// step in that direction +// loop + +type Path = Vec; + +struct Pos { + row: u32, + col: u32 +} + +const WIDTH: u32 = 4; +const HEIGHT: u32 = 4; + +static KEYS: [char; 5] = ['b', 'c', 'd', 'e', 'f']; + +fn recursive_step(passcode: &str, pos: Pos, path: Path) -> Vec { + if pos.row == 3 && pos.col == 3 { + //println!("{}", path.iter().cloned().collect::()); + return vec![path]; + } + + let mut paths: Vec = Vec::new(); + let mut hasher = Md5::new(); + hasher.input(format!("{}{}", passcode, path.iter().cloned().collect::()).as_bytes()); + let result = hasher.result_str(); + + + if pos.row > 0 && KEYS.contains(&result.clone().chars().nth(0).unwrap()) { + let mut rec_path = path.clone(); rec_path.push('U'); + paths.append(&mut recursive_step(passcode, Pos { row: pos.row - 1, col: pos.col }, rec_path)); + } + + if pos.row + 1 < HEIGHT && KEYS.contains(&result.clone().chars().nth(1).unwrap()) { + let mut rec_path = path.clone(); rec_path.push('D'); + paths.append(&mut recursive_step(passcode, Pos { row: pos.row + 1, col: pos.col }, rec_path)); + } + + if pos.col > 0 && KEYS.contains(&result.clone().chars().nth(2).unwrap()) { + let mut rec_path = path.clone(); rec_path.push('L'); + paths.append(&mut recursive_step(passcode, Pos { row: pos.row, col: pos.col - 1 }, rec_path)); + } + + if pos.col + 1 < WIDTH && KEYS.contains(&result.clone().chars().nth(3).unwrap()) { + let mut rec_path = path.clone(); rec_path.push('R'); + paths.append(&mut recursive_step(passcode, Pos { row: pos.row, col: pos.col + 1 }, rec_path)); + } + + paths +} + +fn get_paths(passcode: &str) -> Vec { + let pos = Pos { row: 0, col: 0 }; + + let paths: Vec = recursive_step(passcode, pos, Vec::new()); + paths +} + +fn main() { + let passcode = "rrrbmfta"; + let paths: Vec = get_paths(passcode); + + //find the shortest size then all paths that size since min_by does not work + let shortest_dist = paths.clone().into_iter().map(|p| p.len()).min().unwrap(); //.min_by(|x, y| x.len().cmp(&y.len()) ); + let longest_dist = paths.clone().into_iter().map(|p| p.len()).max().unwrap(); + + let shortest_paths: Vec = paths.clone().into_iter().filter(|p| p.len() == shortest_dist).collect(); + //let longest_paths: Vec = paths.clone().into_iter().filter(|p| p.len() == longest_dist).collect(); + + println!("Part 1: Shortest path {:?}", shortest_paths[0]); + println!("Part 2: Longest path sized {}", longest_dist); +}