2024/03
This commit is contained in:
parent
3bff18f34f
commit
da9781026a
2 changed files with 32 additions and 3 deletions
|
|
@ -1,13 +1,42 @@
|
||||||
|
use regex::Regex;
|
||||||
use shared::{Solution, Answer};
|
use shared::{Solution, Answer};
|
||||||
|
|
||||||
pub struct Day03;
|
pub struct Day03;
|
||||||
|
|
||||||
impl Solution for Day03 {
|
impl Solution for Day03 {
|
||||||
fn part_1(&self, input: &str) -> Answer {
|
fn part_1(&self, input: &str) -> Answer {
|
||||||
Answer::Unimplemented
|
let reg = Regex::new(
|
||||||
|
r"mul\((\d+),(\d+)\)"
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
let res : u32 = reg.captures_iter(input)
|
||||||
|
.map(|mat| mat.extract::<2>().1
|
||||||
|
.map(|x| x.parse::<u32>().unwrap())
|
||||||
|
.iter()
|
||||||
|
.product::<u32>()
|
||||||
|
).sum();
|
||||||
|
|
||||||
|
Answer::Number(res as u64)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part_2(&self, input: &str) -> Answer {
|
fn part_2(&self, input: &str) -> Answer {
|
||||||
Answer::Unimplemented
|
let reg = Regex::new(
|
||||||
|
r"(do)\(\)|(don't)\(\)|(mul)\((\d+),(\d+)\)"
|
||||||
|
).unwrap();
|
||||||
|
|
||||||
|
let mut enabled = true;
|
||||||
|
let mut res = 0;
|
||||||
|
|
||||||
|
for m in reg.captures_iter(input) {
|
||||||
|
if m.get(1).is_some() {
|
||||||
|
enabled = true;
|
||||||
|
} else if m.get(2).is_some() {
|
||||||
|
enabled = false;
|
||||||
|
} else if enabled {
|
||||||
|
res += m.get(4).unwrap().as_str().parse::<u32>().unwrap() * m.get(5).unwrap().as_str().parse::<u32>().unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Answer::Number(res as u64)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ fn run_daily() {
|
||||||
let data = load_actual(year, day).unwrap();
|
let data = load_actual(year, day).unwrap();
|
||||||
|
|
||||||
println!("Solved 1: {}", solution(&data, year, day, 1).unwrap());
|
println!("Solved 1: {}", solution(&data, year, day, 1).unwrap());
|
||||||
println!("Solved 2: {}", solution(&data, year, day, 1).unwrap());
|
println!("Solved 2: {}", solution(&data, year, day, 2).unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_all() {
|
fn run_all() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue