Initial solution
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
/target
|
||||||
|
.idea
|
||||||
7
Cargo.lock
generated
Normal file
7
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 = "day02"
|
||||||
|
version = "0.1.0"
|
||||||
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "day02"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
2500
src/input.txt
Normal file
2500
src/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
82
src/main.rs
Normal file
82
src/main.rs
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io;
|
||||||
|
use std::io::BufRead;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
#[derive(PartialEq, Eq)]
|
||||||
|
struct Choice(u32);
|
||||||
|
|
||||||
|
const ROCK: Choice = Choice(1);
|
||||||
|
const PAPER: Choice = Choice(2);
|
||||||
|
const SCISSORS: Choice = Choice(3);
|
||||||
|
|
||||||
|
const LOOSE: Choice = ROCK;
|
||||||
|
const DRAW: Choice = PAPER;
|
||||||
|
const WIN: Choice = SCISSORS;
|
||||||
|
|
||||||
|
impl From<&str> for Choice {
|
||||||
|
fn from(str: &str) -> Self {
|
||||||
|
match str {
|
||||||
|
"A" | "X" => ROCK,
|
||||||
|
"B" | "Y" => PAPER,
|
||||||
|
"C" | "Z" => SCISSORS,
|
||||||
|
_ => panic!("Invalid value")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Choice {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
&ROCK => f.write_str("Rock"),
|
||||||
|
&PAPER => f.write_str("Paper"),
|
||||||
|
&SCISSORS => f.write_str("Scissors"),
|
||||||
|
_ => f.write_str("UNKNOWN"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let path = Path::new("input.txt");
|
||||||
|
let file = File::open(&path).expect("Could not find file");
|
||||||
|
let lines = io::BufReader::new(file).lines();
|
||||||
|
let mut score1 = 0;
|
||||||
|
let mut score2 = 0;
|
||||||
|
|
||||||
|
for line in lines {
|
||||||
|
if let Ok(line) = line {
|
||||||
|
let v = line.split(' ').take(2).map(|str| Choice::from(str)).collect::<Vec<_>>();
|
||||||
|
let opponent = &v[0];
|
||||||
|
let me = &v[1];
|
||||||
|
|
||||||
|
// First challenge
|
||||||
|
let duel = match (opponent, me) {
|
||||||
|
(&ROCK, &ROCK) | (&PAPER, &PAPER) | (&SCISSORS, &SCISSORS) => 3,
|
||||||
|
(&SCISSORS, &ROCK) | (&PAPER, &SCISSORS) | (&ROCK, &PAPER) => 6,
|
||||||
|
_ => 0,
|
||||||
|
};
|
||||||
|
//println!("{} me {} opponent {} -> duel {}", score, me, opponent, duel);
|
||||||
|
score1 += duel + me.0;
|
||||||
|
|
||||||
|
// second challenge
|
||||||
|
score2 += match (opponent, me) {
|
||||||
|
(&ROCK, &LOOSE) => SCISSORS.0,
|
||||||
|
(&PAPER, &LOOSE) => ROCK.0,
|
||||||
|
(&SCISSORS, &LOOSE) => PAPER.0,
|
||||||
|
(hand, &DRAW) => 3 + hand.0,
|
||||||
|
(&ROCK, &WIN) => 6 + PAPER.0,
|
||||||
|
(&PAPER, &WIN) => 6 + SCISSORS.0,
|
||||||
|
(&SCISSORS, &WIN) => 6 + ROCK.0,
|
||||||
|
_ => 0, // Will never happen, but rustc can't know it
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
println!("Failed to parse : `{:?}`", line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("first answer {}", score1);
|
||||||
|
println!("second answer {}", score2);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user