Add solution for 2022 day 02
This commit is contained in:
parent
19cd82b79d
commit
3c9a208ba6
4
2022/day-01/Cargo.lock → 2022/Cargo.lock
generated
4
2022/day-01/Cargo.lock → 2022/Cargo.lock
generated
@ -5,3 +5,7 @@ version = 3
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "aoc-2022-01"
|
name = "aoc-2022-01"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aoc-2022-02"
|
||||||
|
version = "0.1.0"
|
8
2022/Cargo.toml
Normal file
8
2022/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[workspace]
|
||||||
|
members = ["./day-01", "./day-02"]
|
||||||
|
|
||||||
|
|
||||||
|
[workspace.package]
|
||||||
|
edition = "2021"
|
||||||
|
rust-version = "1.65"
|
||||||
|
authors = ["Patrick Auernig"]
|
@ -5,7 +5,7 @@
|
|||||||
| Day | Part 1 | Part 2 | Language |
|
| Day | Part 1 | Part 2 | Language |
|
||||||
| :-: | :----: | :----: | :------- |
|
| :-: | :----: | :----: | :------- |
|
||||||
| 01 | ✓ | ✓ | [Rust] |
|
| 01 | ✓ | ✓ | [Rust] |
|
||||||
| 02 | | | |
|
| 02 | ✓ | ✓ | [Rust] |
|
||||||
| 03 | | | |
|
| 03 | | | |
|
||||||
| 04 | | | |
|
| 04 | | | |
|
||||||
| 05 | | | |
|
| 05 | | | |
|
||||||
|
6
2022/day-02/Cargo.toml
Normal file
6
2022/day-02/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "aoc-2022-02"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
5
2022/day-02/Justfile
Normal file
5
2022/day-02/Justfile
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
@part PART INPUT_FILE="inputs/puzzle.txt":
|
||||||
|
cargo --quiet run --bin part_{{PART}} -- {{INPUT_FILE}}
|
||||||
|
|
||||||
|
clean:
|
||||||
|
cargo clean
|
2500
2022/day-02/inputs/puzzle.txt
Normal file
2500
2022/day-02/inputs/puzzle.txt
Normal file
File diff suppressed because it is too large
Load Diff
3
2022/day-02/inputs/test.txt
Normal file
3
2022/day-02/inputs/test.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
A Y
|
||||||
|
B X
|
||||||
|
C Z
|
19
2022/day-02/src/bin/part_one.rs
Normal file
19
2022/day-02/src/bin/part_one.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
use std::{env, io};
|
||||||
|
|
||||||
|
use aoc_2022_02::{calculate_round_score, parse_lines, Player};
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let infile_path = env::args().nth(1).expect("input file");
|
||||||
|
|
||||||
|
let rounds = parse_lines(infile_path)?.map(|(a, b)| (Player::parse(&a), Player::parse(&b)));
|
||||||
|
|
||||||
|
let mut total_score = 0u64;
|
||||||
|
|
||||||
|
for (enemy, myself) in rounds {
|
||||||
|
total_score += calculate_round_score(&myself, &enemy);
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{total_score}");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
20
2022/day-02/src/bin/part_two.rs
Normal file
20
2022/day-02/src/bin/part_two.rs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
use std::{env, io};
|
||||||
|
|
||||||
|
use aoc_2022_02::{calculate_round_score, parse_lines, Player, RoundResult};
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let infile_path = env::args().nth(1).expect("input file");
|
||||||
|
let rounds =
|
||||||
|
parse_lines(infile_path)?.map(|(a, b)| (Player::parse(&a), RoundResult::parse(&b)));
|
||||||
|
|
||||||
|
let mut total_score = 0u64;
|
||||||
|
|
||||||
|
for (enemy, expected_result) in rounds {
|
||||||
|
let myself = enemy.versus(&expected_result);
|
||||||
|
total_score += calculate_round_score(&myself, &enemy);
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{total_score}");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
101
2022/day-02/src/lib.rs
Normal file
101
2022/day-02/src/lib.rs
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
use std::{
|
||||||
|
fs::File,
|
||||||
|
io::{self, BufRead, BufReader},
|
||||||
|
path::Path,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum Player {
|
||||||
|
Rock,
|
||||||
|
Paper,
|
||||||
|
Scissors,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Player {
|
||||||
|
pub fn shape_score(&self) -> u64 {
|
||||||
|
match self {
|
||||||
|
Self::Rock => 1,
|
||||||
|
Self::Paper => 2,
|
||||||
|
Self::Scissors => 3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn wins_against(&self, other: &Self) -> RoundResult {
|
||||||
|
match (self, other) {
|
||||||
|
(Self::Rock, Self::Paper) => RoundResult::Loss,
|
||||||
|
(Self::Rock, Self::Scissors) => RoundResult::Win,
|
||||||
|
(Self::Paper, Self::Rock) => RoundResult::Win,
|
||||||
|
(Self::Paper, Self::Scissors) => RoundResult::Loss,
|
||||||
|
(Self::Scissors, Self::Rock) => RoundResult::Loss,
|
||||||
|
(Self::Scissors, Self::Paper) => RoundResult::Win,
|
||||||
|
_ => RoundResult::Draw,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn versus(&self, expected_result: &RoundResult) -> Self {
|
||||||
|
use Player::*;
|
||||||
|
use RoundResult::*;
|
||||||
|
|
||||||
|
match (expected_result, self) {
|
||||||
|
(Win, Rock) => Paper,
|
||||||
|
(Win, Paper) => Scissors,
|
||||||
|
(Win, Scissors) => Rock,
|
||||||
|
(Loss, Rock) => Scissors,
|
||||||
|
(Loss, Paper) => Rock,
|
||||||
|
(Loss, Scissors) => Paper,
|
||||||
|
(Draw, other) => *other,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse(val: &str) -> Self {
|
||||||
|
match val {
|
||||||
|
"A" | "X" => Self::Rock,
|
||||||
|
"B" | "Y" => Self::Paper,
|
||||||
|
"C" | "Z" => Self::Scissors,
|
||||||
|
_ => panic!("Invalid input"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum RoundResult {
|
||||||
|
Win,
|
||||||
|
Draw,
|
||||||
|
Loss,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RoundResult {
|
||||||
|
pub fn parse(val: &str) -> Self {
|
||||||
|
match val {
|
||||||
|
"X" => Self::Loss,
|
||||||
|
"Y" => Self::Draw,
|
||||||
|
"Z" => Self::Win,
|
||||||
|
_ => panic!("Invalid input"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn calculate_round_score(player1: &Player, player2: &Player) -> u64 {
|
||||||
|
player1.shape_score()
|
||||||
|
+ match player1.wins_against(&player2) {
|
||||||
|
RoundResult::Win => 6,
|
||||||
|
RoundResult::Draw => 3,
|
||||||
|
RoundResult::Loss => 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_lines<P>(path: P) -> io::Result<impl Iterator<Item = (String, String)>>
|
||||||
|
where
|
||||||
|
P: AsRef<Path>,
|
||||||
|
{
|
||||||
|
let file = File::open(path)?;
|
||||||
|
let reader = BufReader::new(file);
|
||||||
|
|
||||||
|
let rounds = reader.lines().map(|line| {
|
||||||
|
let line = line.unwrap();
|
||||||
|
let pair: Vec<_> = line.split_whitespace().collect();
|
||||||
|
(pair[0].into(), pair[1].into())
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(rounds)
|
||||||
|
}
|
@ -9,4 +9,4 @@
|
|||||||
- [2019](2019/README.md) (0% completed)
|
- [2019](2019/README.md) (0% completed)
|
||||||
- [2020](2020/README.md) (20% completed)
|
- [2020](2020/README.md) (20% completed)
|
||||||
- [2021](2021/README.md) (68% completed)
|
- [2021](2021/README.md) (68% completed)
|
||||||
- [2022](2022/README.md) (4% completed)
|
- [2022](2022/README.md) (8% completed)
|
||||||
|
Loading…
Reference in New Issue
Block a user