Added Day 2 part 1
This commit is contained in:
parent
de2df0f8c5
commit
8e83830b1d
4738
inputs/input.txt
4738
inputs/input.txt
File diff suppressed because it is too large
Load Diff
|
@ -1,16 +1,3 @@
|
||||||
1000
|
A Y
|
||||||
2000
|
B X
|
||||||
3000
|
C Z
|
||||||
|
|
||||||
4000
|
|
||||||
|
|
||||||
5000
|
|
||||||
6000
|
|
||||||
|
|
||||||
7000
|
|
||||||
8000
|
|
||||||
9000
|
|
||||||
|
|
||||||
10000
|
|
||||||
|
|
||||||
|
|
||||||
|
|
64
src/main.rs
64
src/main.rs
|
@ -1,34 +1,68 @@
|
||||||
use std::cmp::Reverse;
|
|
||||||
|
|
||||||
const INPUTS: [&str; 2] = [
|
const INPUTS: [&str; 2] = [
|
||||||
include_str!("../inputs/sample.txt"),
|
include_str!("../inputs/sample.txt"),
|
||||||
include_str!("../inputs/input.txt"),
|
include_str!("../inputs/input.txt"),
|
||||||
];
|
];
|
||||||
|
|
||||||
fn parse(input: &'static str) -> Vec<Vec<i64>> {
|
fn parse(input: &'static str) -> Vec<(Move, Move)> {
|
||||||
input
|
input
|
||||||
.trim()
|
.trim()
|
||||||
.split("\n\n")
|
.lines()
|
||||||
.map(|set| {
|
.map(|set| {
|
||||||
set.split('\n')
|
let set = set.trim();
|
||||||
.map(|c| c.trim())
|
let (a, b) = set.split_at(1);
|
||||||
.map(|c| c.parse::<i64>().unwrap())
|
|
||||||
.collect()
|
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()
|
.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() {
|
fn main() {
|
||||||
for input in INPUTS.iter() {
|
for input in INPUTS.iter() {
|
||||||
let output = parse(input);
|
let output = parse(input);
|
||||||
|
let mut score = 0;
|
||||||
|
|
||||||
let mut sets: Vec<i64> = output
|
for (a, b) in output {
|
||||||
.into_iter()
|
score += b as i32;
|
||||||
.map(|c| c.into_iter().sum::<i64>())
|
score += calc_score(b, a);
|
||||||
.collect();
|
}
|
||||||
|
|
||||||
sets.select_nth_unstable_by_key(2, |c| Reverse(*c));
|
println!("{:?}", score);
|
||||||
|
|
||||||
println!("{}", sets.into_iter().take(3).sum::<i64>());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user