mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 13:03:19 -09:00
do day 2
This commit is contained in:
parent
213fcdc1d4
commit
8dbc7961b4
4753
2022/input/day_2.txt
4753
2022/input/day_2.txt
File diff suppressed because it is too large
Load Diff
@ -1,14 +1,3 @@
|
||||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
||||
A Y
|
||||
B X
|
||||
C Z
|
||||
|
@ -1,15 +1,72 @@
|
||||
use Shape::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Shape {
|
||||
Rock,
|
||||
Paper,
|
||||
Scissors,
|
||||
}
|
||||
impl Shape {
|
||||
fn from_char(c: char) -> Self {
|
||||
match c {
|
||||
'A' | 'X' => Rock,
|
||||
'B' | 'Y' => Paper,
|
||||
'C' | 'Z' => Scissors,
|
||||
_ => panic!("expected A, Y, B, X, C, or Z"),
|
||||
}
|
||||
}
|
||||
fn as_int(&self) -> i32 {
|
||||
match self {
|
||||
Rock => 1,
|
||||
Paper => 2,
|
||||
Scissors => 3,
|
||||
}
|
||||
}
|
||||
fn result(&self, other: &Self) -> i32 {
|
||||
self.as_int()
|
||||
+ if self.as_int() == other.as_int() {
|
||||
3
|
||||
} else if std::cmp::max((self.as_int() + 1) % 4, 1) == other.as_int() {
|
||||
0
|
||||
} else {
|
||||
6
|
||||
}
|
||||
}
|
||||
fn loses_to(&self) -> Self {
|
||||
match self {
|
||||
Rock => Paper,
|
||||
Paper => Scissors,
|
||||
Scissors => Rock,
|
||||
}
|
||||
}
|
||||
fn wins_to(&self) -> Self {
|
||||
match self {
|
||||
Paper => Rock,
|
||||
Scissors => Paper,
|
||||
Rock => Scissors,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
advent_of_code_macro::solve_problem!(
|
||||
day 2,
|
||||
Input i32,
|
||||
parse |_input: &str| {
|
||||
1 + 1
|
||||
Input Vec<Vec<char>>,
|
||||
parse |input: &str| {
|
||||
input.lines().map(|line| line.split(' ').map(|s| s.chars().next().unwrap()).collect()).collect()
|
||||
},
|
||||
part one |data: Input| {
|
||||
data + 5
|
||||
use super::Shape;
|
||||
data.iter().map(|round| Shape::from_char(round[1]).result(&Shape::from_char(round[0]))).sum()
|
||||
},
|
||||
part two |data: Input| {
|
||||
data + 10
|
||||
use super::Shape;
|
||||
data.iter().map(|round| match round[1] {
|
||||
'X' => Shape::from_char(round[0]).wins_to().as_int(),
|
||||
'Y' => Shape::from_char(round[0]).as_int() + 3,
|
||||
'Z' => Shape::from_char(round[0]).loses_to().as_int() + 6,
|
||||
_ => panic!("expected X, Y, or Z"),
|
||||
}).sum()
|
||||
},
|
||||
sample tests [7, 12],
|
||||
star tests [7, 12]
|
||||
sample tests [15, 12],
|
||||
star tests [11063, 10349]
|
||||
);
|
||||
|
@ -1,3 +1,3 @@
|
||||
#[allow(dead_code)]
|
||||
#![allow(dead_code)]
|
||||
mod day_1;
|
||||
mod day_2;
|
||||
|
Loading…
x
Reference in New Issue
Block a user