LD
Change your colour scheme

Advent of Code: Day Three

Published:

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.

Tags:

About the author

My face

I'm Lewis Dale, a software engineer and web developer based in the UK. I write about writing software, silly projects, and cycling. A lot of cycling. Too much, maybe.

Responses