summaryrefslogtreecommitdiff
path: root/src/bin/day15.rs
blob: 2b3b04aff80b92b60755877a7f5b8643ad54cd8b (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::collections::VecDeque;

static DAY: u8 = 15;

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

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

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

    value
}

#[derive(Clone, Debug)]
struct Lens {
    label: String,
    focal_length: usize,
}

enum Operation {
    Assign(Lens),
    Remove(String),
}

impl Operation {
    fn from(input: &str) -> Operation {
        if let Some((label, value)) = input.split_once('=') {
            Operation::Assign(Lens {
                label: label.to_string(),
                focal_length: value.parse().unwrap()
            })
        } else if let Some((label, _)) = input.split_once('-') {
            Operation::Remove(label.to_string())
        } else {
            panic!("invalid operation");
        }
    }

    fn label(&self) -> String {
        match self {
            Operation::Assign(lens) => lens.label.clone(),
            Operation::Remove(label) => label.to_string(),
        }
    }
}

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

fn init_sequence(boxes: &mut [VecDeque::<Lens>], operations: &[Operation]) {
    for operation in operations {
        let box_nr = hash(&operation.label());
        match operation {
            Operation::Assign(lens) => {
                if let Some(boxed_lens) = boxes[box_nr].iter_mut().find(|l| l.label == lens.label) {
                    boxed_lens.focal_length = lens.focal_length;
                } else {
                    boxes[box_nr].push_back(lens.clone());
                }
            },
            Operation::Remove(label) => {
                boxes[box_nr].retain(|lens| lens.label != *label);
            }
        }
    }
}

fn focusing_power(input: &str) -> usize {
    let input = input.trim_end();
    let operations = input.split(',')
                          .map(Operation::from)
                          .collect::<Vec<_>>();

    let mut boxes = vec![VecDeque::<Lens>::new(); 256];
    init_sequence(&mut boxes, &operations);

    boxes.iter()
         .enumerate()
         .map(|(box_nr, lenses)| lenses.iter().enumerate().map(|(slot, lens)| (box_nr + 1) * (slot + 1) * lens.focal_length).sum::<usize>())
         .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);
        assert_eq!(focusing_power(input), 145);
    }
}