From 102b456eaf67c759a819f022340adb3b22ef2c80 Mon Sep 17 00:00:00 2001 From: Russell Champoux Date: Wed, 7 Dec 2016 16:00:13 -0500 Subject: [PATCH] Finished a few more --- 2016/day01/src/main.rs | 19 +------- 2016/day04/src/main.rs | 55 +++++++++++++++++++---- 2016/day06/Cargo.lock | 4 ++ 2016/day06/Cargo.toml | 6 +++ 2016/day06/src/main.rs | 99 ++++++++++++++++++++++++++++++++++++++++++ 2016/day07/Cargo.lock | 98 +++++++++++++++++++++++++++++++++++++++++ 2016/day07/Cargo.toml | 7 +++ 2016/day07/src/main.rs | 81 ++++++++++++++++++++++++++++++++++ 8 files changed, 344 insertions(+), 25 deletions(-) create mode 100644 2016/day06/Cargo.lock create mode 100644 2016/day06/Cargo.toml create mode 100644 2016/day06/src/main.rs create mode 100644 2016/day07/Cargo.lock create mode 100644 2016/day07/Cargo.toml create mode 100644 2016/day07/src/main.rs diff --git a/2016/day01/src/main.rs b/2016/day01/src/main.rs index e784a22..f92f6cb 100644 --- a/2016/day01/src/main.rs +++ b/2016/day01/src/main.rs @@ -20,21 +20,6 @@ fn step(state: &(i32, i32), pos: &(i32, i32), dist: i32) -> (i32, i32) { (pos.0 + state.0 * dist, pos.1 + state.1 * dist) } -// fn state_to_direction(state: &(i32, i32)) -> String { -// match state.0 { -// 1 => "N", -// -1 => "S", -// 0 => { -// match state.1 { -// 1 => "E", -// -1 => "W", -// _ => "Invalid", -// } -// }, -// _ => "Invalid", -// } -// } - fn part_1(input: &String) { let mut state = (1, 0); let mut pos = (0, 0); @@ -62,14 +47,14 @@ fn part_2(input: &String) { state = turn(&state, &dir); let pos = step(&state, positions.last().unwrap(), dist); - print!("Move {} {} to ({}, {}) - ", dist, dir, pos.0, pos.1); + print!("{}: {}{} -> ({}, {}), ", positions.len(), dist, dir, pos.0, pos.1); match positions.iter().position(|&p| p == pos) { Some(v) => { println!("Part 2: Stopped twice at ({}, {}) on element {}, {} away from the start", pos.0, pos.1, v, pos.0.abs() + pos.1.abs()); break; }, None => { - println!("Did not match {} locations", positions.len()); + //println!("Did not match {} locations", positions.len()); positions.push(pos); }, } diff --git a/2016/day04/src/main.rs b/2016/day04/src/main.rs index d41d782..a23f50d 100644 --- a/2016/day04/src/main.rs +++ b/2016/day04/src/main.rs @@ -3,7 +3,7 @@ extern crate regex; use std::io::Read; use std::fs::File; use regex::Regex; -use std::collections::HashMap; +//use std::collections::HashMap; ///// THIS IS NOT WORKING RIGHT. :( @@ -27,18 +27,57 @@ struct Room { fn part_1(input: &String) { let re = Regex::new(r"([a-z\\-]+)-([:digit:]+)\[([:lower:]+)\]").unwrap(); - let sum = 0; - for cap in re.captures_iter(input) { + let mut sum = 0; + 'outer: for cap in re.captures_iter(input) { let room = Room { name: String::from(cap.at(1).unwrap_or("")), sector: cap.at(2).unwrap_or("0").parse().unwrap_or(0), check: String::from(cap.at(3).unwrap_or("")), }; - let mut charMap: HashMap = HashMap::new(); - for ch in room.name.replace("-", "").chars() { - let l = charMap.entry(ch).or_insert(0); - *l += 1; + //println!("{:?}", room); + let mut accumulator = [0; 26]; + for letter in room.name.replace("-", "").chars() { + let letter_idx = letter as u8 - 'a' as u8; + accumulator[letter_idx as usize] = accumulator[letter_idx as usize] + 1; + //println!("The letter is {} with a value {} - {:?}", letter, letter_idx, accumulator); } - println!("{:?}", charMap); + + //println!("Accumulated {:?}", accumulator); + + let mut letters_to_match = room.check.len(); + let mut biggest_value = -1; + let mut biggest_idxs: Vec = Vec::new(); + while letters_to_match > 0 { + for idx in 0..accumulator.len() { + if accumulator[idx] > biggest_value { + biggest_value = accumulator[idx]; + biggest_idxs = vec![idx as u8]; + } else if accumulator[idx] == biggest_value { + biggest_idxs.push(idx as u8); //add it with the others + } + } + biggest_idxs.reverse(); //we want to take items off in the order we put them on + //println!("These letters had a score of {}: {:?}", biggest_value, biggest_idxs); + while letters_to_match > 0 && biggest_idxs.len() > 0 { + let letter_idx = biggest_idxs.pop().unwrap(); + let letter_value = (letter_idx + 'a' as u8) as char; + if ! room.check.as_str().contains(letter_value) { + continue 'outer; + } + accumulator[letter_idx as usize] = 0; //nope this broke things + biggest_value = -1; + letters_to_match = letters_to_match - 1; + } + } + //phew! we successfully checked this room. the sector is old, but it checks out + println!("Valid! {:?}", room); + sum = sum + room.sector; + + // let mut char_map: HashMap = HashMap::new(); + // for ch in room.name.replace("-", "").chars() { + // let l = char_map.entry(ch).or_insert(0); + // *l += 1; + // } + // println!("{:?}", char_map); //println!("{:?}", room); //let mut letters: Vec = .collect(); //println!("{:?}", letters.sort()); diff --git a/2016/day06/Cargo.lock b/2016/day06/Cargo.lock new file mode 100644 index 0000000..39522d6 --- /dev/null +++ b/2016/day06/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "day06" +version = "0.1.0" + diff --git a/2016/day06/Cargo.toml b/2016/day06/Cargo.toml new file mode 100644 index 0000000..c6f31d1 --- /dev/null +++ b/2016/day06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day06" +version = "0.1.0" +authors = ["Russell Champoux "] + +[dependencies] diff --git a/2016/day06/src/main.rs b/2016/day06/src/main.rs new file mode 100644 index 0000000..f309ea0 --- /dev/null +++ b/2016/day06/src/main.rs @@ -0,0 +1,99 @@ +use std::io::Read; +use std::fs::File; +//use std::collections::HashMap; + +fn part_1(input: &String) { + // This would have been great if it actually worked. + // let values = input.lines() + // .map(|l| l + // .trim() + // .chars() + // .collect::>() + // ) + // .collect::>(); + // println!("first item {:?}", values[0]); + // println!("second item {:?}", values[1]); + + // //let mut letters: Vec = vec![String::new(); values[0].len()]; + // let mut letters: Vec> = vec![vec![]; values[0].len()]; + // for v in values { + // for (idx, l) in v.iter().enumerate() { + // letters[idx].push(*l); + // } + // } + // for (idx, line) in letters.iter().enumerate() { + // //println!("Letter {}: {:?}", idx, s); + // let mut hash: HashMap = HashMap::new(); + // let mut largest: (char, i32) = ('a', -1); + // for l in line { + // match hash.get(l) { + // Some(v) => { + // v = &(v + 1); + // // if v > largest.1 { + // // largest = (l, v); + // // } + // }, + // None => hash.insert(*l, 1), + // } + // } + + // //let s: String = l.iter().cloned().collect(); + // } + + let size = input.lines().nth(0).unwrap().len(); + print!("Part 1: Answer "); + for idx in 0..size { + let mut accumulator = [0; 26]; + for line in input.lines() { + let letter = line.chars().nth(idx).unwrap(); + let letter_idx = letter as u8 - 'a' as u8; + accumulator[letter_idx as usize] = accumulator[letter_idx as usize] + 1; + //println!("The {}th letter is {} with a value {} - {:?}", idx, letter, letter_idx, accumulator); + } + let mut biggest = (0, -1); //idx and value + for idx in 0..accumulator.len() { + if accumulator[idx] > biggest.1 { + biggest = (idx, accumulator[idx]); + } + } + let biggest_letter = (biggest.0 as u8 + 'a' as u8) as char; + // println!("Accumulator is {:?}", accumulator); + // println!("Biggest is {:?} which is {}", biggest, biggest_letter); + print!("{}", biggest_letter); + } + println!(""); //newline +} + +fn part_2(input: &String) { + let size = input.lines().nth(0).unwrap().len(); + print!("Part 2: Answer "); + for idx in 0..size { + let mut accumulator = [0; 26]; + for line in input.lines() { + let letter = line.chars().nth(idx).unwrap(); + let letter_idx = letter as u8 - 'a' as u8; + accumulator[letter_idx as usize] = accumulator[letter_idx as usize] + 1; + //println!("The {}th letter is {} with a value {} - {:?}", idx, letter, letter_idx, accumulator); + } + let mut smallest = (0, ::max_value()); //idx and value + for idx in 0..accumulator.len() { + if accumulator[idx] > 0 && accumulator[idx] < smallest.1 { + smallest = (idx, accumulator[idx]); + } + } + let smallest_letter = (smallest.0 as u8 + 'a' as u8) as char; + // println!("Accumulator is {:?}", accumulator); + // println!("Smallest is {:?} which is {}", smallest, smallest_letter); + print!("{}", smallest_letter); + } + println!(""); //newline +} + +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"); + + part_1(&input); + part_2(&input); +} diff --git a/2016/day07/Cargo.lock b/2016/day07/Cargo.lock new file mode 100644 index 0000000..ab740c0 --- /dev/null +++ b/2016/day07/Cargo.lock @@ -0,0 +1,98 @@ +[root] +name = "day07" +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/day07/Cargo.toml b/2016/day07/Cargo.toml new file mode 100644 index 0000000..9a6b0ff --- /dev/null +++ b/2016/day07/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day07" +version = "0.1.0" +authors = ["~krowbar"] + +[dependencies] +regex = "0.1" \ No newline at end of file diff --git a/2016/day07/src/main.rs b/2016/day07/src/main.rs new file mode 100644 index 0000000..27c7416 --- /dev/null +++ b/2016/day07/src/main.rs @@ -0,0 +1,81 @@ +extern crate regex; + +use regex::Regex; +use std::io::Read; +use std::fs::File; + +fn ugly_match(text: &String) -> bool { + if text.len() < 4 { + return false + } + //check for ABBA pattern + for idx in 0..text.len() - 3 { + if text.chars().nth(idx).unwrap() == text.chars().nth(idx+3).unwrap() && + text.chars().nth(idx+1).unwrap() == text.chars().nth(idx+2).unwrap() && + text.chars().nth(idx).unwrap() != text.chars().nth(idx+1).unwrap() { + return true + } + } + return false +} + +fn part_1(input: &String) { + + let re = Regex::new(r"\[([:lower:]+)\]").unwrap(); + let mut count = 0; + let mut rejected = 0; + let mut none = 0; + + 'outer: for line in input.lines() { + + //hypernet sequences + for cap in re.captures_iter(line) { + let cap = cap.at(1).unwrap_or(""); + //reject if cap contains an ABBA + if ugly_match(&cap.to_string()) { + //println!("- {}", cap); + rejected = rejected + 1; + continue 'outer; + } + } + + //addresses + for cap in re.replace_all(line, ",").split(",") { + //println!("Matched bit {}", cap); + if ugly_match(&cap.to_string()) { + //println!("+ {}", cap); + count = count + 1; + continue 'outer; + } + } + + none = none + 1; + } + println!("Count: {} Rejected: {} None: {}", count, rejected, none); + println!("Part 1 Answer: {}", count); +} + +fn part_2(input: &String) { + let re = Regex::new(r"\[([:lower:]+)\]").unwrap(); + let mut count = 0; + + 'outer: for line in input.lines() { + + for cap in re.replace_all(line, ",").split(",") { + let bab: String = format!(cap.chars().nth(1).unwrap(), cap.chars().nth(0).unwrap(), cap.chars().nth(1).unwrap()); + println!("Generated bab {}", bab); + let bre = Regex::new(&format!("{}{}{}", r"\[(", bab, r")\]")).unwrap(); + //check for ABAs + //check for related BAB + } + } +} + +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"); + + //part_1(&input); + part_2(&input); +}