1
0

Added Day 2 part 1

This commit is contained in:
Ishan Jain 2022-12-02 11:12:08 +05:30
parent de2df0f8c5
commit 8e83830b1d
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
3 changed files with 2552 additions and 2269 deletions

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -1,34 +1,68 @@
use std::cmp::Reverse;
const INPUTS: [&str; 2] = [
include_str!("../inputs/sample.txt"),
include_str!("../inputs/input.txt"),
];
fn parse(input: &'static str) -> Vec<Vec<i64>> {
fn parse(input: &'static str) -> Vec<(Move, Move)> {
input
.trim()
.split("\n\n")
.lines()
.map(|set| {
set.split('\n')
.map(|c| c.trim())
.map(|c| c.parse::<i64>().unwrap())
.collect()
let set = set.trim();
let (a, b) = set.split_at(1);
let x = match a.trim() {
"A" => Move::Rock,
"B" => Move::Paper,
"C" => Move::Scissors,
_ => unreachable!(),
};
let y = match b.trim() {
"X" => Move::Rock,
"Y" => Move::Paper,
"Z" => Move::Scissors,
v => {
println!("{:?}", v);
unreachable!();
}
};
(x, y)
})
.collect()
}
#[derive(Clone, Copy)]
enum Move {
Rock = 1,
Paper = 2,
Scissors = 3,
}
fn calc_score(m1: Move, m2: Move) -> i32 {
match (m1, m2) {
(Move::Rock, Move::Rock) => 3,
(Move::Rock, Move::Paper) => 0,
(Move::Rock, Move::Scissors) => 6,
(Move::Paper, Move::Rock) => 6,
(Move::Paper, Move::Paper) => 3,
(Move::Paper, Move::Scissors) => 0,
(Move::Scissors, Move::Rock) => 0,
(Move::Scissors, Move::Paper) => 6,
(Move::Scissors, Move::Scissors) => 3,
}
}
fn main() {
for input in INPUTS.iter() {
let output = parse(input);
let mut score = 0;
let mut sets: Vec<i64> = output
.into_iter()
.map(|c| c.into_iter().sum::<i64>())
.collect();
for (a, b) in output {
score += b as i32;
score += calc_score(b, a);
}
sets.select_nth_unstable_by_key(2, |c| Reverse(*c));
println!("{}", sets.into_iter().take(3).sum::<i64>());
println!("{:?}", score);
}
}