summaryrefslogtreecommitdiff
path: root/src/bin/day15.rs
blob: 648534abcd7a9781284e971b0aa7778531925f22 (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
static DAY: u8 = 15;

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

fn hash(input: &str) -> u32 {
    let mut value = 0;

    for c in input.chars() {
        value += c as u32;
        value *= 17;
        value %= 256;
    }

    value
}

fn hash_sum(input: &str) -> u32 {
    let input = input.trim_end();
    input.split(',')
         .map(|x| hash(x))
         .sum()
}

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

    #[test]
    fn test() {
        let input = "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7";
        assert_eq!(hash("HASH"), 52);
        assert_eq!(hash_sum(input), 1320);
    }
}