Complete day 2 part 1
This commit is contained in:
parent
d4ab42872d
commit
edb913d662
7
day2/Cargo.lock
generated
Normal file
7
day2/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day2"
|
||||||
|
version = "0.1.0"
|
8
day2/Cargo.toml
Normal file
8
day2/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "day2"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
2500
day2/input.txt
Normal file
2500
day2/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
80
day2/src/main.rs
Normal file
80
day2/src/main.rs
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
use std::error::Error;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
enum Shape {
|
||||||
|
Rock,
|
||||||
|
Paper,
|
||||||
|
Scissors,
|
||||||
|
}
|
||||||
|
use Shape::*;
|
||||||
|
|
||||||
|
impl From<char> for Shape {
|
||||||
|
fn from(c: char) -> Self {
|
||||||
|
match c {
|
||||||
|
'A' | 'X' => Rock,
|
||||||
|
'B' | 'Y' => Paper,
|
||||||
|
'C' | 'Z' => Scissors,
|
||||||
|
_ => panic!("Invalid char: {}", c),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Shape {
|
||||||
|
fn get_score(&self) -> u32 {
|
||||||
|
match self {
|
||||||
|
Rock => 1,
|
||||||
|
Paper => 2,
|
||||||
|
Scissors => 3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_outcome_score(them: Shape, you: Shape) -> u32 {
|
||||||
|
match (them, you) {
|
||||||
|
// Loss
|
||||||
|
(Rock, Scissors) | (Paper, Rock) | (Scissors, Paper) => 0,
|
||||||
|
|
||||||
|
// Tie
|
||||||
|
(Rock, Rock) | (Paper, Paper) | (Scissors, Scissors) => 3,
|
||||||
|
|
||||||
|
// Win
|
||||||
|
(Rock, Paper) | (Paper, Scissors) | (Scissors, Rock) => 6,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_round_score(them: char, you: char) -> u32 {
|
||||||
|
let them = Shape::from(them);
|
||||||
|
let you = Shape::from(you);
|
||||||
|
|
||||||
|
let shape_score = you.get_score();
|
||||||
|
let outcome_score = get_outcome_score(them, you);
|
||||||
|
|
||||||
|
shape_score + outcome_score
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_scores(lines: Vec<&str>) -> Vec<u32> {
|
||||||
|
lines
|
||||||
|
.into_iter()
|
||||||
|
.map(|line| {
|
||||||
|
// This is trying to be too clever
|
||||||
|
let [them, _, you]: [char; 3] = line.chars().collect::<Vec<char>>().try_into().unwrap();
|
||||||
|
|
||||||
|
get_round_score(them, you)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
let file_str = fs::read_to_string("input.txt")?;
|
||||||
|
let lines: Vec<&str> = file_str.lines().collect();
|
||||||
|
let scores = get_scores(lines);
|
||||||
|
let total = scores
|
||||||
|
.into_iter()
|
||||||
|
.reduce(|accum, item| accum + item)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
println!("Part 1: Final score: {}", total);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
Reference in New Issue
Block a user