summaryrefslogtreecommitdiff
path: root/day03/src/main.rs
blob: 6d8672770d017026a77c1ad5d7d384ecf19124fe (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
use std::io;

fn highest_jolt(batteries: &str) -> i64 {
    let batteries: Vec<char> = batteries.chars().collect();
    let mut indexes = Vec::new();
    let mut last = 0;
    for x in 0..12 {
        let mut index = last;
        for i in last + 1..batteries.len() - 11 + x {
            if batteries[i] > batteries[index] {
                index = i;
            }
        }
        indexes.push(index);
        last = index + 1;
    }
    let mut tmp = String::new();
    for i in indexes {
        tmp.push(batteries[i]);
    }
    tmp.parse().unwrap()
}

fn main() {
    let mut total = 0;

    loop {
        let mut buffer = String::new();
        let bytes = io::stdin().read_line(&mut buffer).unwrap();
        if bytes == 0 {
            break;
        }
        let jolt = highest_jolt(buffer.trim_end());
        total += jolt;
    }

    println!("Total: {}", total);
}