Advent of Code: Day Three
Spoilers for Advent of Code below
Day three, checked off ✅. I’m rapidly closing in on a high score here (although that’s only Day 4, so it’s a very low bar).
This wasn’t too much of a challenge either, the IterTools
crate gave me some useful helpers that made parsing the data slightly easier, and HashMaps
means I could pretty quickly construct a unique set of data.
To calculate the score, I was lazy. I constructed a string containing every character a-zA-Z
, and just used the index of a character + 1:
fn score_char(character: &char) -> i32 {
let mut priorities = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".chars();
(priorities.position(|c| c == *character).unwrap() as i32) + 1
}
Dead simple, and works well. Will break dramatically if an unexpected character is entered (well, it’ll panic anyway). PArt two was more-or-less the same, I just needed to write some extra code to produce chunks of data (spoiler: I used iter.chunk
):
pub fn group_and_score(inputs: Vec<String>) -> i32 {
inputs
.chunks_exact(3)
.map(|chunk| {
ElfGroup::from_lines([
String::from(&chunk[0]),
String::from(&chunk[1]),
String::from(&chunk[2]),
])
.score
})
.sum()
}
I’m quite enjoying this so far, let’s see what Day 4 brings.