From df49cc1866448440d32383b889bd6e07ccd42d18 Mon Sep 17 00:00:00 2001 From: logaritmisk Date: Fri, 23 Dec 2016 19:52:02 +0100 Subject: [PATCH] Small changes. --- 2015/09/Cargo.lock | 105 +++++++++++++++++++++++++++++++++++++ 2015/09/Cargo.toml | 8 +++ 2015/09/input.txt | 28 ++++++++++ 2015/09/src/main.rs | 125 ++++++++++++++++++++++++++++++++++++++++++++ 2015/11/Cargo.lock | 4 ++ 2015/11/Cargo.toml | 6 +++ 2015/11/src/main.rs | 61 +++++++++++++++++++++ 2016/04/src/main.rs | 2 +- 2016/13/src/main.rs | 1 - 9 files changed, 338 insertions(+), 2 deletions(-) create mode 100644 2015/09/Cargo.lock create mode 100644 2015/09/Cargo.toml create mode 100644 2015/09/input.txt create mode 100644 2015/09/src/main.rs create mode 100644 2015/11/Cargo.lock create mode 100644 2015/11/Cargo.toml create mode 100644 2015/11/src/main.rs diff --git a/2015/09/Cargo.lock b/2015/09/Cargo.lock new file mode 100644 index 0000000..b35a4dc --- /dev/null +++ b/2015/09/Cargo.lock @@ -0,0 +1,105 @@ +[root] +name = "09" +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/2015/09/Cargo.toml b/2015/09/Cargo.toml new file mode 100644 index 0000000..f676056 --- /dev/null +++ b/2015/09/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "09" +version = "0.1.0" +authors = ["logaritmisk "] + +[dependencies] +lazy_static = "0.1" +regex = "0.1" diff --git a/2015/09/input.txt b/2015/09/input.txt new file mode 100644 index 0000000..97a6b63 --- /dev/null +++ b/2015/09/input.txt @@ -0,0 +1,28 @@ +Faerun to Tristram = 65 +Faerun to Tambi = 129 +Faerun to Norrath = 144 +Faerun to Snowdin = 71 +Faerun to Straylight = 137 +Faerun to AlphaCentauri = 3 +Faerun to Arbre = 149 +Tristram to Tambi = 63 +Tristram to Norrath = 4 +Tristram to Snowdin = 105 +Tristram to Straylight = 125 +Tristram to AlphaCentauri = 55 +Tristram to Arbre = 14 +Tambi to Norrath = 68 +Tambi to Snowdin = 52 +Tambi to Straylight = 65 +Tambi to AlphaCentauri = 22 +Tambi to Arbre = 143 +Norrath to Snowdin = 8 +Norrath to Straylight = 23 +Norrath to AlphaCentauri = 136 +Norrath to Arbre = 115 +Snowdin to Straylight = 101 +Snowdin to AlphaCentauri = 84 +Snowdin to Arbre = 96 +Straylight to AlphaCentauri = 107 +Straylight to Arbre = 14 +AlphaCentauri to Arbre = 46 diff --git a/2015/09/src/main.rs b/2015/09/src/main.rs new file mode 100644 index 0000000..4996721 --- /dev/null +++ b/2015/09/src/main.rs @@ -0,0 +1,125 @@ +#[macro_use] extern crate lazy_static; +extern crate regex; + +use std::io::{self, Read}; +use std::collections::BTreeMap; + +use regex::Regex; + + +type Map<'a> = BTreeMap<&'a str, BTreeMap<&'a str, i64>>; + + +#[derive(Clone, Debug)] +struct Route<'a> { + vec: Vec<&'a str>, + distance: i64 +} + +impl<'a> Route<'a> { + fn new() -> Route<'a> { + Route { + vec: Vec::new(), + distance: 0 + } + } +} + + +fn process<'a>(input: &'a str) -> Route<'a> { + lazy_static! { + static ref RE: Regex = Regex::new(r"^([a-zA-Z]+) to ([a-zA-Z]+) = (\d+)$").unwrap(); + } + + let mut map: Map = BTreeMap::new(); + + for line in input.lines() { + let caps = RE.captures(&line).unwrap(); + + let a = caps.at(1).unwrap(); + let b = caps.at(2).unwrap(); + + let distance = caps.at(3).unwrap().parse::().unwrap(); + + map.entry(a).or_insert(BTreeMap::new()).insert(b, distance); + map.entry(b).or_insert(BTreeMap::new()).insert(a, distance); + } + + let mut routes = iterate(&map, None); + + routes.sort_by_key(|x| x.distance); + + routes.remove(0) +} + +fn iterate<'a>(map: &Map<'a>, route: Option>) -> Vec> { + let mut routes = Vec::new(); + + if let Some(mut route) = route { + let prev = *route.vec.last().unwrap(); + + let mut end = true; + + for (city, distance) in map.get(prev).unwrap().iter() { + if route.vec.contains(city) { + continue; + } + + route.vec.push(*city); + route.distance += *distance; + + { + let mut sub_routes = iterate(&map, Some(route.clone())); + + routes.append(&mut sub_routes); + } + + end = false; + } + + if end { + routes.push(route); + } + } else { + for city in map.keys() { + let mut route = Route::new(); + + route.vec.push(*city); + + { + let mut sub_routes = iterate(&map, Some(route)); + + routes.append(&mut sub_routes); + } + } + } + + routes +} + +fn main() { + let mut input = String::new(); + + io::stdin().read_to_string(&mut input).unwrap(); + + let result = process(&input); + + println!("{:?}", result); +} + + +#[cfg(test)] +mod tests { + use super::process; + + #[test] + fn example_01() { + let input = "London to Dublin = 464\n\ + London to Belfast = 518\n\ + Dublin to Belfast = 141"; + + let result = process(input); + + assert_eq!(605, result.distance); + } +} diff --git a/2015/11/Cargo.lock b/2015/11/Cargo.lock new file mode 100644 index 0000000..5f55a37 --- /dev/null +++ b/2015/11/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "11" +version = "0.1.0" + diff --git a/2015/11/Cargo.toml b/2015/11/Cargo.toml new file mode 100644 index 0000000..fb5facf --- /dev/null +++ b/2015/11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "11" +version = "0.1.0" +authors = ["logaritmisk "] + +[dependencies] diff --git a/2015/11/src/main.rs b/2015/11/src/main.rs new file mode 100644 index 0000000..65c822f --- /dev/null +++ b/2015/11/src/main.rs @@ -0,0 +1,61 @@ +fn main() { + println!("Hello, world!"); +} + +fn validate(input: &str) -> bool { + let count = input.chars() + .collect::>() + .windows(3) + .filter(|x| x[0] >= 'a' && x[2] <= 'z') + .filter(|x| (x[1] as i8 - x[0] as i8) == 1 && (x[2] as i8 - x[1] as i8) == 1) + .count(); + + if count <= 0 { + return false; + } + + for c in &['i', 'o', 'l'] { + if input.contains(*c) { + return false; + } + } + + let count = input.chars() + .collect::>() + .windows(2) + .filter(|x| x[0] == x[1]) + .count(); + + if count < 2 { + return false; + } + + true +} + +fn generate(input: &str) -> String { + let mut password = String::from(input); + + password +} + + +#[cfg(test)] +mod tests { + use super::{validate, generate}; + + #[test] + fn test_validate() { + assert_eq!(false, validate("hijklmmn")); + assert_eq!(false, validate("abbceffg")); + assert_eq!(false, validate("abbcegjk")); + assert_eq!(true, validate("abcdffaa")); + assert_eq!(true, validate("ghjaabcc")); + } + + #[test] + fn test_generate() { + assert_eq!(String::from("abcdffaa"), generate("abcdefgh")); + assert_eq!(String::from("ghjaabcc"), generate("ghijklmn")); + } +} diff --git a/2016/04/src/main.rs b/2016/04/src/main.rs index b2704a4..f5d4dcb 100644 --- a/2016/04/src/main.rs +++ b/2016/04/src/main.rs @@ -70,7 +70,7 @@ impl FromStr for Room { } if let Some(caps) = RE.captures(s) { - let enc_name = String::from(caps.at(1).unwrap()); + let enc_name = caps.at(1).unwrap().parse::().unwrap(); let sector_id = caps.at(2).unwrap().parse::().unwrap(); let checksum = caps.at(3).unwrap().chars().collect::>(); diff --git a/2016/13/src/main.rs b/2016/13/src/main.rs index 9157b17..a9431a1 100644 --- a/2016/13/src/main.rs +++ b/2016/13/src/main.rs @@ -145,7 +145,6 @@ impl Office { Err(()) } - } impl fmt::Display for Office {