-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.rs
46 lines (36 loc) · 1.14 KB
/
day03.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use regex::Regex;
use std::fmt::Display;
use crate::days::Day;
pub struct Day03;
impl Day for Day03 {
fn part_one(input: &str) -> impl Display {
let re = Regex::new(r"mul\((\d*),(\d*)\)").unwrap();
let mut sum: i128 = 0;
for (_, [arg1, arg2]) in re.captures_iter(input).map(|c| c.extract()) {
sum += arg1.parse::<i128>().unwrap() * arg2.parse::<i128>().unwrap();
}
sum
}
fn part_two(input: &str) -> impl Display {
let re = Regex::new(r"(mul|don't|do)\((?:(\d*),(\d*))?\)").unwrap();
let mut sum: i128 = 0;
let mut multiplying = true;
for captures in re.captures_iter(input) {
match &captures[1] {
"mul" => {
if multiplying {
sum += captures[2].parse::<i128>().unwrap()
* captures[3].parse::<i128>().unwrap();
}
}
"do" => multiplying = true,
"don't" => multiplying = false,
_ => (),
}
}
sum
}
fn get_day_num() -> u8 {
return 3;
}
}