Skip to content

Advent of Code Day Two - Rock, Paper, Scissors

December 3, 2022 | 12:26 PM

Rock Paper Scissors

In Day 2 we find the elves determining who gets to sleep next to the food by a rock, paper, scissors tournament. An elf gives us a cheat-sheet to help us out.

Part 1

In part one we get our puzzle input which is 2 columns of upper-case ACII letters that represent positions and each line represents a round played. The goal is to calculate the total of points in the tournament if you follow the cheat-sheet.
The first column represents the opponents hand and the second column is the hand you should play. The break down of the chars and points is as follows:

CharacterPositionPoints
ARock1
BPaper2
CScissors3
XRock1
YPaper2
ZScissors3
A Y
B X
C Z

As yesterday I had to create the input file and read that data in. Rust has a handy convention for that. I then split the elements on the newline to create an array or list of the rounds.

fn main() {
    let input: &str = include_str!("./day2_input.txt");

    let lines = input.split('\n');

/* ... */

}

After a bit of googling and trial and error I landed on this little gem of a line to parse the groups into numbers u32 and then total them, returning a list of totaled amounts. I then sorted the collection and printed the top one, which is the largest.


let mut linesParse: Vec<u32> = lines.map(|line|line.split("\n").flat_map(|num|num.parse::<u32>()).sum::<u32>()).collect();

linesParse.sort_by(|a,b|b.cmp(a));
println!("{:?}", linesParse.iter().take(1).sum::<u32>());

Part 2

For part 2 we needed to find the total of the top 3 elves which was easy since I had struck google-gold with the Part 1 piece.
All I had to do was take(3) and it was ready to go!


println!("{:?}", linesParse.iter().take(3).sum::<u32>());

Final Solution

Here is the whole file after I was done:

fn main() {
    let input: &str = include_str!("./day1_1_input.txt");

    let lines = input.split("\n\n");

    let mut linesParse: Vec<u32> = lines.map(|line|line.split("\n").flat_map(|num|num.parse::<u32>()).sum::<u32>()).collect();

    linesParse.sort_by(|a,b|b.cmp(a));

    println!("{:?}", linesParse.iter().take(3).sum::<u32>());
}