-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.rs
78 lines (68 loc) · 2.24 KB
/
day15.rs
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
use std::fs;
pub(crate) fn day15() {
println!("{}", part_a(fs::read_to_string("input/2023/day15/input.txt").unwrap()));
println!("{}", part_b(fs::read_to_string("input/2023/day15/input.txt").unwrap()));
}
fn part_a(input: String) -> u32 {
input.split(",").map(|seq| {
let mut ans = 0;
for c in seq.chars() {
let ci = c as u32;
ans += ci;
ans *= 17;
ans = ans % 256;
}
ans
}).sum()
}
fn part_b(input: String) -> usize {
let mut boxes: Vec<Vec<(&str, usize)>> = vec! { vec!(); 256 };
input.split(",").for_each(|seq| {
let mut label = String::new();
let mut ans = 0;
for c in seq.chars() {
if c == '-' || c == '=' { break; }
label.push(c);
let ci = c as usize;
ans += ci;
ans *= 17;
ans = ans % 256;
}
if seq.ends_with("-") {
boxes[ans] = boxes[ans].iter()
.filter(|(l2, _)| l2 != &label.as_str())
.map(|v| *v)
.collect();
} else {
let (label, focal_length) = seq.split_once("=").unwrap();
if let Some(pos) = boxes[ans].iter().position(|(l2, _)| l2 == &label) {
boxes[ans][pos] = (label, focal_length.parse::<usize>().unwrap());
} else {
boxes[ans].push((label, focal_length.parse::<usize>().unwrap()));
}
}
});
let mut ans = 0;
for box_i in 0..256 {
for slot_i in 0..boxes[box_i].len() {
let (_label, focal_length) = boxes[box_i][slot_i];
ans += (box_i + 1) * (slot_i + 1) * focal_length;
}
}
ans
}
#[cfg(test)]
mod day15_tests {
use std::fs;
use crate::y2023::day15::{part_a, part_b};
#[test]
fn test_works() {
assert_eq!(1320, part_a(fs::read_to_string("input/2023/day15/test.txt").unwrap()));
assert_eq!(145, part_b(fs::read_to_string("input/2023/day15/test.txt").unwrap()));
}
#[test]
fn input_works() {
assert_eq!(495972, part_a(fs::read_to_string("input/2023/day15/input.txt").unwrap()));
assert_eq!(245223, part_b(fs::read_to_string("input/2023/day15/input.txt").unwrap()));
}
}