-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03.rs
34 lines (28 loc) · 820 Bytes
/
03.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
#![feature(test)]
use regex::Regex;
type Input = String;
fn setup(input: &str) -> Input {
input.trim().into()
}
fn part1(input: &Input) -> i64 {
let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap();
re.captures_iter(input)
.map(|c| c[1].parse::<i64>().unwrap() * c[2].parse::<i64>().unwrap())
.sum()
}
fn part2(input: &Input) -> i64 {
let re = Regex::new(r"mul\((\d+),(\d+)\)|do\(\)|don't\(\)").unwrap();
let mut enabled = true;
let mut out = 0;
for c in re.captures_iter(input) {
if &c[0] == "do()" {
enabled = true;
} else if &c[0] == "don't()" {
enabled = false;
} else if enabled {
out += c[1].parse::<i64>().unwrap() * c[2].parse::<i64>().unwrap();
}
}
out
}
aoc::main!(2024, 3, ex: 1, 2);