aboutsummaryrefslogtreecommitdiff
path: root/2023/day-01/src/bin/part1.rs
blob: 3fd213d3d6de013b44c3ec012d12b8e5264c418f (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
37
38
39
40
41
42
43
44
fn main() {
    let input = include_str!("./input.txt");
    let output: u32 = input.lines().map(process_line).sum();
    println!("Result {}", output);
}

fn process_line(s: &str) -> u32 {
    let mut string = String::new();
    let iter: Vec<char> = s.chars().filter(|c| {
        c.to_digit(10).is_some()
    }).map(|c| c).collect();
    assert!(iter.len() >= 1);
    let chars = [iter.first(), iter.last()];
    for char in chars {
	if let Some(c) = char {
            string.push(*c);
	}
    }
    string.parse::<u32>().unwrap()
}

#[test]
fn input_1() {
    let result = process_line("1abc2");
    assert_eq!(result, 12);
}

#[test]
fn input_2() {
    let result = process_line("pqr3stu8vwx");
    assert_eq!(result, 38);
}

#[test]
fn input_3() {
    let result = process_line("a1b2c3d4e5f");
    assert_eq!(result, 15);
}

#[test]
fn input_4() {
    let result = process_line("treb7uchet");
    assert_eq!(result, 77);
}