summaryrefslogtreecommitdiff
path: root/src/bin/day3.rs
blob: 02cb385725f8090bf15024683b006b99ff198524 (plain)
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
use regex::Regex;

static DAY: u8 = 3;

fn main() {
    let input = advent::read_lines(DAY);
    println!("{DAY}a: {}", sum_multiplications(&input));
    println!("{DAY}b: {}", 0);
}

fn sum_multiplications(input: &[String]) -> u32 {
    let re = Regex::new(r"mul\(([0-9]{1,3}),([0-9]{1,3})\)").unwrap();

    let mut sum = 0;
    for line in input {
        for cap in re.captures_iter(line) {
            let f1 = cap[1].parse::<u32>().unwrap();
            let f2 = cap[2].parse::<u32>().unwrap();
            sum += f1 * f2;
        }
    }
    sum
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test() {
        let input = [
            "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"
        ].iter().map(|&x| String::from(x)).collect::<Vec<_>>();
        assert_eq!(sum_multiplications(&input), 161);
    }
}