2022 rust day 2

This commit is contained in:
Maciej Jur 2022-12-02 10:26:12 +01:00
parent 22b6450724
commit 5b38020265
7 changed files with 2630 additions and 16 deletions

2500
2022/rust/inputs/day02.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,14 +1,3 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
A Y
B X
C Z

View file

@ -3,5 +3,5 @@ mod solutions;
fn main() {
solutions::day01::run();
solutions::day02::run();
}

View file

@ -1,5 +1,7 @@
#![allow(dead_code)]
use crate::utils;
pub fn run() -> () {
let data = parse_data(utils::read_lines(utils::Source::Day(1)));

View file

@ -0,0 +1,122 @@
use crate::utils;
pub fn run() -> () {
let data = parse_data(utils::read_lines(utils::Source::Day(2)));
println!("Day 2");
println!("Part 1: {}", solve1(&data));
println!("Part 2: {}", solve2(&data));
}
#[repr(u8)]
#[derive(Clone, Copy)]
enum Shape {
Rock = 0,
Paper = 1,
Scissors = 2,
}
impl Shape {
fn from(char: &char) -> Shape {
match char {
&'A' => Shape::Rock,
&'B' => Shape::Paper,
&'C' => Shape::Scissors,
&'X' => Shape::Rock,
&'Y' => Shape::Paper,
&'Z' => Shape::Scissors,
&_ => panic!(),
}
}
fn from_num(num: u8) -> Shape {
match num {
0 => Shape::Rock,
1 => Shape::Paper,
2 => Shape::Scissors,
_ => panic!()
}
}
fn check_against(&self, other: &Shape) -> MatchResult {
let (this, that) = (*self as u8, *other as u8);
if this == that {
MatchResult::Tie
}
else if (this + 1) % 3 == that {
MatchResult::Loss
}
else {
MatchResult::Win
}
}
fn to_points(&self) -> i32 {
match self {
Shape::Rock => 1,
Shape::Paper => 2,
Shape::Scissors => 3,
}
}
fn find_from(other: &Shape, result: &MatchResult) -> Shape {
match result {
MatchResult::Win => Shape::from_num((*other as u8 + 1) % 3),
MatchResult::Tie => *other,
MatchResult::Loss => Shape::from_num((*other as u8 + 2) % 3),
}
}
}
enum MatchResult {
Win, Tie, Loss
}
impl MatchResult {
fn to_points(&self) -> i32 {
match self {
MatchResult::Win => 6,
MatchResult::Tie => 3,
MatchResult::Loss => 0,
}
}
fn from(char: &char) -> MatchResult {
match char {
&'X' => MatchResult::Loss,
&'Y' => MatchResult::Tie,
&'Z' => MatchResult::Win,
&_ => panic!(),
}
}
}
fn solve1(data: &Vec<(char, char)>) -> i32 {
data.iter()
.fold(0, |acc, (other, my)| {
let my = Shape::from(my);
let result = my.check_against(&Shape::from(other));
acc + my.to_points() + result.to_points()
})
}
fn solve2(data: &Vec<(char, char)>) -> impl std::fmt::Display {
data.iter()
.fold(0, |acc, (other, result)| {
let result = MatchResult::from(result);
let my = Shape::find_from(&Shape::from(other), &result);
acc + my.to_points() + result.to_points()
})
}
fn parse_data(data: Vec<String>) -> Vec<(char, char)> {
data.into_iter()
.map(|line| {
let mut chars = line.chars();
(chars.next().unwrap(), chars.last().unwrap())
})
.collect()
}

View file

@ -1 +1,2 @@
pub mod day01;
pub mod day02;

View file

@ -2,7 +2,7 @@ use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
#[allow(dead_code)]
pub enum Source { Scratch, Day(i32) }