mirror of
https://github.com/RussellChamp/advent-of-code-2016.git
synced 2026-10-28 07:42:30 +00:00
day04 and day07 part2s
This commit is contained in:
1 parent
116bb99b1a
commit
a8b1f29c3a
2 files changed
+53
-10
No files matched your search
+35
-7
@@ -19,6 +19,16 @@ fn ugly_match(text: &String) -> bool {
|
||||
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) {
|
||||
|
||||
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<String> = 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);
|
||||
}
|
||||
Reference in new issue
Block a user