day04 and day07 part2s

This commit is contained in:
Russell Champoux committed 2016-12-08 15:03:07 -05:00
1 parent 116bb99b1a
commit a8b1f29c3a
2 files changed
+52 -9

No files matched your search

+18 -3
View File
@@ -3,6 +3,7 @@ extern crate regex;
use std::io::Read; use std::io::Read;
use std::fs::File; use std::fs::File;
use regex::Regex; use regex::Regex;
use regex::RegexSet;
//use std::collections::HashMap; //use std::collections::HashMap;
///// THIS IS NOT WORKING RIGHT. :( ///// THIS IS NOT WORKING RIGHT. :(
@@ -24,7 +25,7 @@ struct Room {
check: String, check: String,
} }
fn part_1(input: &String) { fn part_1_2(input: &String) {
let re = Regex::new(r"([a-z\\-]+)-([:digit:]+)\[([:lower:]+)\]").unwrap(); let re = Regex::new(r"([a-z\\-]+)-([:digit:]+)\[([:lower:]+)\]").unwrap();
let mut sum = 0; 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 //phew! we successfully checked this room. the sector is old, but it checks out
println!("Valid! {:?}", room); //println!("Valid! {:?}", room);
sum = sum + room.sector; 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::<String>();
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<char, i32> = HashMap::new(); // let mut char_map: HashMap<char, i32> = HashMap::new();
// for ch in room.name.replace("-", "").chars() { // for ch in room.name.replace("-", "").chars() {
// let l = char_map.entry(ch).or_insert(0); // let l = char_map.entry(ch).or_insert(0);
@@ -109,5 +124,5 @@ fn main() {
let mut input = String::new(); let mut input = String::new();
file.read_to_string(&mut input).expect("could not read file"); file.read_to_string(&mut input).expect("could not read file");
part_1(&input); part_1_2(&input);
} }
+34 -6
View File
@@ -19,6 +19,16 @@ fn ugly_match(text: &String) -> bool {
return false return false
} }
fn uglier_match(text: &String) -> Vec<String> {
let mut ret: Vec<String> = 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) { fn part_1(input: &String) {
let re = Regex::new(r"\[([:lower:]+)\]").unwrap(); let re = Regex::new(r"\[([:lower:]+)\]").unwrap();
@@ -60,22 +70,40 @@ fn part_2(input: &String) {
let mut count = 0; let mut count = 0;
'outer: for line in input.lines() { 'outer: for line in input.lines() {
//println!("{}", line);
let mut abas: Vec<String> = Vec::new();
for cap in re.replace_all(line, ",").split(",") { 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()); for m in uglier_match(&cap.to_string()) {
println!("Generated bab {}", bab); abas.push(m.to_string()); //add ABA to big list
let bre = Regex::new(&format!("{}{}{}", r"\[(", bab, r")\]")).unwrap(); }
//check for ABAs }
//check for related BAB //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() { fn main() {
let mut file = File::open("./data").expect("could not open file"); let mut file = File::open("./data").expect("could not open file");
let mut input = String::new(); let mut input = String::new();
file.read_to_string(&mut input).expect("could not read file"); file.read_to_string(&mut input).expect("could not read file");
//part_1(&input); part_1(&input);
part_2(&input); part_2(&input);
} }