diff --git a/2016/day04/src/main.rs b/2016/day04/src/main.rs index a23f50d..9bb4883 100644 --- a/2016/day04/src/main.rs +++ b/2016/day04/src/main.rs @@ -3,6 +3,7 @@ extern crate regex; use std::io::Read; use std::fs::File; use regex::Regex; +use regex::RegexSet; //use std::collections::HashMap; ///// THIS IS NOT WORKING RIGHT. :( @@ -24,7 +25,7 @@ struct Room { check: String, } -fn part_1(input: &String) { +fn part_1_2(input: &String) { let re = Regex::new(r"([a-z\\-]+)-([:digit:]+)\[([:lower:]+)\]").unwrap(); let mut sum = 0; @@ -69,9 +70,23 @@ fn part_1(input: &String) { } } //phew! we successfully checked this room. the sector is old, but it checks out - println!("Valid! {:?}", room); + //println!("Valid! {:?}", room); sum = sum + room.sector; + //decrypt room name + let real_name = room.name.chars().map(|c| + match c { + '-' => ' ', + _ => ((((c as i32 - 'a' as i32) + room.sector as i32) % 26) as u8 + 'a' as u8) as char, + }) + .collect::(); + let set = RegexSet::new(&[r"north", r"pole", r"snow", r"santa", r"christmas"]).unwrap(); + if set.is_match(&real_name) { + println!("Part 2: {} ({})", real_name, room.sector); + } + //println!("{} ({})", real_name, 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); @@ -109,5 +124,5 @@ fn main() { let mut input = String::new(); file.read_to_string(&mut input).expect("could not read file"); - part_1(&input); + part_1_2(&input); } diff --git a/2016/day07/src/main.rs b/2016/day07/src/main.rs index 27c7416..7021ea2 100644 --- a/2016/day07/src/main.rs +++ b/2016/day07/src/main.rs @@ -19,6 +19,16 @@ fn ugly_match(text: &String) -> bool { return false } +fn uglier_match(text: &String) -> Vec { + let mut ret: Vec = Vec::new(); + for idx in 0..text.len() - 2 { + if text.chars().nth(idx).unwrap() == text.chars().nth(idx+2).unwrap() { + ret.push(text[idx..idx+3].to_string()); + } + } + ret +} + fn part_1(input: &String) { let re = Regex::new(r"\[([:lower:]+)\]").unwrap(); @@ -60,15 +70,33 @@ fn part_2(input: &String) { let mut count = 0; 'outer: for line in input.lines() { - + //println!("{}", line); + let mut abas: Vec = Vec::new(); 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 + for m in uglier_match(&cap.to_string()) { + abas.push(m.to_string()); //add ABA to big list + } } + //check for related BAB + for cap in re.captures_iter(line) { + let text = cap.at(0).unwrap_or(""); + for aba in abas.iter() { + let mut bab: String = String::new(); + bab.push(aba.chars().nth(1).unwrap()); + bab.push(aba.chars().nth(0).unwrap()); + bab.push(aba.chars().nth(1).unwrap()); + //println!("looking for {} in {}", bab, text); + let bre = Regex::new(bab.as_str()).unwrap(); + if bre.is_match(text) { + //println!("{}", line); + count = count + 1; + continue 'outer; + } + } + } + //println!("{} - {:?}", line, abas); } + println!("Part 2: Found {} matches", count); } fn main() { @@ -76,6 +104,6 @@ fn main() { let mut input = String::new(); file.read_to_string(&mut input).expect("could not read file"); - //part_1(&input); + part_1(&input); part_2(&input); }