Day 1, 2 and 3

This commit is contained in:
Max
2022-12-03 13:49:36 +01:00
commit 129c9f85a0
82 changed files with 5859 additions and 0 deletions

1
day2/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
day2/Cargo.lock generated Normal file
View 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
View 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

File diff suppressed because it is too large Load Diff

117
day2/src/main.rs Normal file
View File

@ -0,0 +1,117 @@
use std::fs::read_to_string;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum Symbol {
Rock,
Paper,
Scissors,
}
impl From<char> for Symbol {
fn from(value: char) -> Self {
match value {
'A' => Self::Rock,
'B' => Self::Paper,
'C' => Self::Scissors,
'X' => Self::Rock,
'Y' => Self::Paper,
'Z' => Self::Scissors,
_ => panic!("invalid input"),
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum Outcome {
Win,
Draw,
Lose,
}
impl From<char> for Outcome {
fn from(value: char) -> Self {
match value {
'X' => Self::Lose,
'Y' => Self::Draw,
'Z' => Self::Win,
_ => panic!("invalid input"),
}
}
}
impl Symbol {
fn play(self, opponent: Self) -> Outcome {
if self == opponent {
Outcome::Draw
} else if (self == Self::Rock && opponent == Self::Scissors)
|| (self == Self::Paper && opponent == Self::Rock)
|| (self == Self::Scissors && opponent == Self::Paper)
{
Outcome::Win
} else {
Outcome::Lose
}
}
}
fn score(play: Symbol, result: Outcome) -> u32 {
let score = match play {
Symbol::Rock => 1,
Symbol::Paper => 2,
Symbol::Scissors => 3,
};
let score2 = match result {
Outcome::Lose => 0,
Outcome::Draw => 3,
Outcome::Win => 6,
};
score + score2
}
fn get_play(opponent: Symbol, outcome: Outcome) -> Symbol {
match outcome {
Outcome::Win => match opponent {
Symbol::Rock => Symbol::Paper,
Symbol::Paper => Symbol::Scissors,
Symbol::Scissors => Symbol::Rock,
},
Outcome::Draw => opponent,
Outcome::Lose => match opponent {
Symbol::Rock => Symbol::Scissors,
Symbol::Paper => Symbol::Rock,
Symbol::Scissors => Symbol::Paper,
},
}
}
fn initial(input: &str) -> (u32, u32) {
let first = input
.split_terminator('\n')
.map(|line| {
line.split_whitespace()
.map(|s| Symbol::from(s.chars().next().unwrap()))
.collect::<Vec<_>>()
})
.map(|p| score(p[1], p[1].play(p[0])))
.sum();
let second = input
.split_terminator('\n')
.map(|line| {
let chars: Vec<_> = line
.split_whitespace()
.map(|s| s.chars().next().unwrap())
.collect();
let opponent = Symbol::from(chars[0]);
let outcome = Outcome::from(chars[1]);
let play = get_play(opponent, outcome);
score(play, outcome)
})
.sum();
(first, second)
}
fn main() {
let input = read_to_string("input.txt").expect("input needed");
let score = initial(&input);
println!("score: {score:?}");
}