Some new things
This commit is contained in:
parent
73eb7211e6
commit
8f155ccdc9
@ -1 +1 @@
|
|||||||
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
|
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
|
||||||
|
|||||||
@ -4,7 +4,6 @@ use std::process;
|
|||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
pub fn run_day_3() -> Solution {
|
pub fn run_day_3() -> Solution {
|
||||||
|
|
||||||
let file = fs::read_to_string("./inputs/day3.txt").expect("Unable to parse file");
|
let file = fs::read_to_string("./inputs/day3.txt").expect("Unable to parse file");
|
||||||
let re = Regex::new("mul\\((\\d{1,3}),(\\d{1,3})\\)").unwrap();
|
let re = Regex::new("mul\\((\\d{1,3}),(\\d{1,3})\\)").unwrap();
|
||||||
let mut part1_sum = 0;
|
let mut part1_sum = 0;
|
||||||
@ -15,12 +14,58 @@ pub fn run_day_3() -> Solution {
|
|||||||
let total = num1 * num2;
|
let total = num1 * num2;
|
||||||
part1_sum += total;
|
part1_sum += total;
|
||||||
}
|
}
|
||||||
|
// get all matches of do and their index
|
||||||
|
// get all matches of dont and their index
|
||||||
|
let mut part2_sum = 0;
|
||||||
|
let dont_regex = Regex::new("don't\\(\\)").unwrap();
|
||||||
|
let do_regex = Regex::new("do\\(\\)").unwrap();
|
||||||
|
let mut donts: Vec<u64> = dont_regex.find(&file).map(|m| m.start() as u64).into_iter().collect();
|
||||||
|
let mut dos: Vec<u64> = do_regex.find(&file).map(|m| m.start() as u64).into_iter().collect();
|
||||||
|
for mul in re.captures_iter(&file) {
|
||||||
|
let start: usize = mul.get(1).unwrap().start();
|
||||||
|
// get the closest number, that is lower than the start value
|
||||||
|
let mut closest_dont = 0;
|
||||||
|
for i in (0..start).rev() {
|
||||||
|
// remove 1 becasue we want to search in reverse.
|
||||||
|
let mut index = i as u64;
|
||||||
|
if donts.contains(&index) {
|
||||||
|
closest_dont = index;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut closest_do = 0;
|
||||||
|
for i in (0..start).rev() {
|
||||||
|
let mut index = i as u64;
|
||||||
|
if dos.contains(&index) {
|
||||||
|
closest_do = index;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{:?} -> {:?}", closest_do, closest_dont);
|
||||||
|
|
||||||
|
if closest_do > closest_dont {
|
||||||
|
let num1: &u32 = &mul[1].parse().expect("Conversion failed");
|
||||||
|
let num2: &u32 = &mul[2].parse().expect("Conversion failed");
|
||||||
|
let total = num1 * num2;
|
||||||
|
part2_sum += total;
|
||||||
|
}
|
||||||
|
|
||||||
|
if closest_do == 0 && closest_dont == 0 {
|
||||||
|
let num1: &u32 = &mul[1].parse().expect("Conversion failed");
|
||||||
|
let num2: &u32 = &mul[2].parse().expect("Conversion failed");
|
||||||
|
let total = num1 * num2;
|
||||||
|
part2_sum += total;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// println!("{:?} -> {:?}", dos, donts);
|
||||||
|
|
||||||
|
// Final solutions here.
|
||||||
let solution = Solution {
|
let solution = Solution {
|
||||||
day: 3,
|
day: 3,
|
||||||
answer_1: part1_sum.to_string(),
|
answer_1: part1_sum.to_string(),
|
||||||
answer_2: "Not sure".to_string(),
|
answer_2: part2_sum.to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
solution
|
solution
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user