Solve Part 1
This commit is contained in:
parent
81c7bff2d3
commit
4908c5699e
2500
src/input.txt
Normal file
2500
src/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
127
src/main.rs
127
src/main.rs
@ -1,3 +1,126 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
// https://adventofcode.com/2022/day/2
|
||||
|
||||
const DATA: &'static str = include_str!("input.txt");
|
||||
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
enum Move
|
||||
{
|
||||
Rock,
|
||||
Paper,
|
||||
Scissors
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq)]
|
||||
#[repr(i32)]
|
||||
enum Outcome
|
||||
{
|
||||
Loss = 0,
|
||||
Draw = 3,
|
||||
Win = 6
|
||||
}
|
||||
|
||||
impl Outcome
|
||||
{
|
||||
pub fn as_score(&self) -> i32
|
||||
{
|
||||
match self {
|
||||
Self::Loss => 0,
|
||||
Self::Draw => 3,
|
||||
Self::Win => 6,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Move
|
||||
{
|
||||
pub fn as_score(&self) -> i32
|
||||
{
|
||||
match self {
|
||||
Self::Rock => 1,
|
||||
Self::Paper => 2,
|
||||
Self::Scissors => 3
|
||||
}
|
||||
}
|
||||
|
||||
pub fn did_win(&self, other: &Move) -> Outcome
|
||||
{
|
||||
if self == other { Outcome::Draw }
|
||||
else {
|
||||
match self {
|
||||
Self::Rock => {
|
||||
if *other == Move::Paper { Outcome::Loss }
|
||||
else { Outcome::Win }
|
||||
}
|
||||
|
||||
Self::Paper => {
|
||||
if *other == Move::Scissors { Outcome::Loss }
|
||||
else { Outcome::Win }
|
||||
}
|
||||
|
||||
Self::Scissors => {
|
||||
if *other == Move::Rock { Outcome::Loss }
|
||||
else { Outcome::Win }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum MoveParseFailure
|
||||
{
|
||||
InvalidMove,
|
||||
}
|
||||
|
||||
impl FromStr for Move {
|
||||
type Err = MoveParseFailure;
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s {
|
||||
"A" => Ok(Self::Rock),
|
||||
"B" => Ok(Self::Paper),
|
||||
"C" => Ok(Self::Scissors),
|
||||
"X" => Ok(Self::Rock),
|
||||
"Y" => Ok(Self::Paper),
|
||||
"Z" => Ok(Self::Scissors),
|
||||
_ => Err(MoveParseFailure::InvalidMove)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn score_for_round(my_move: &Move, other_move: &Move) -> i32
|
||||
{
|
||||
let mut score = my_move.as_score();
|
||||
|
||||
let outcome = my_move.did_win(other_move);
|
||||
|
||||
score += outcome.as_score();
|
||||
|
||||
score
|
||||
}
|
||||
|
||||
fn parse_round_and_get_score(round_str: &str) -> Option<i32>
|
||||
{
|
||||
let mut moves = round_str.split(" ");
|
||||
|
||||
let first_move = moves.nth(0)?; // Opponent's move goes first
|
||||
let second_move = moves.nth(0)?; // My move goes second
|
||||
|
||||
Some(score_for_round(&second_move.parse().expect("Expected a valid move"),
|
||||
&first_move.parse().expect("Expected a valid move")))
|
||||
}
|
||||
|
||||
fn part1()
|
||||
{
|
||||
let mut score = 0;
|
||||
for slice in DATA.split("\n")
|
||||
{
|
||||
score += parse_round_and_get_score(slice).expect("Input data is not correctly structured");
|
||||
}
|
||||
println!("Your total score is {}", score);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
part1();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user