Compare commits
9 Commits
master
...
a96624e0a0
Author | SHA1 | Date | |
---|---|---|---|
a96624e0a0 | |||
be1cd454fe | |||
38f1b5a037 | |||
8dc9b8fcbb | |||
d8f191fd9b | |||
77cb399748 | |||
beeb89a2f6 | |||
64851dd68b | |||
2fd479d02d |
46
Cargo.toml
46
Cargo.toml
@ -10,30 +10,30 @@ chrono = "0.4.23"
|
||||
regex = "1.7.0"
|
||||
|
||||
[features]
|
||||
default = ["a", "b", "today"]
|
||||
a = []
|
||||
b = []
|
||||
default = ["part1", "part2", "today"]
|
||||
part1 = []
|
||||
part2 = []
|
||||
today = []
|
||||
01 = []
|
||||
02 = []
|
||||
03 = []
|
||||
04 = []
|
||||
05 = []
|
||||
06 = []
|
||||
07 = []
|
||||
08 = []
|
||||
09 = []
|
||||
10 = []
|
||||
11 = []
|
||||
12 = []
|
||||
13 = []
|
||||
14 = []
|
||||
15 = []
|
||||
16 = []
|
||||
17 = []
|
||||
18 = []
|
||||
19 = []
|
||||
20 = []
|
||||
day01 = []
|
||||
day02 = []
|
||||
day03 = []
|
||||
day04 = []
|
||||
day05 = []
|
||||
day06 = []
|
||||
day07 = []
|
||||
day08 = []
|
||||
day09 = []
|
||||
day10 = []
|
||||
day11 = []
|
||||
day12 = []
|
||||
day13 = []
|
||||
day14 = []
|
||||
day15 = []
|
||||
day16 = []
|
||||
day17 = []
|
||||
day18 = []
|
||||
day19 = []
|
||||
day20 = []
|
||||
|
||||
|
||||
|
||||
|
2235
input/01.txt
2235
input/01.txt
File diff suppressed because it is too large
Load Diff
2500
input/02.txt
Normal file
2500
input/02.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -2,4 +2,29 @@ use crate::read;
|
||||
|
||||
pub fn run() {
|
||||
let input = read("01");
|
||||
let elves = vec![0];
|
||||
|
||||
let mut elves = input.lines().fold(elves, |mut acc, x|{
|
||||
if x.is_empty() {
|
||||
acc.push(0);
|
||||
} else {
|
||||
let num: i32 = x.parse().unwrap();
|
||||
*acc.last_mut().unwrap() += num;
|
||||
}
|
||||
acc
|
||||
});
|
||||
elves.sort();
|
||||
elves.reverse();
|
||||
|
||||
#[cfg(feature="part1")]
|
||||
{
|
||||
let a = elves.first().unwrap();
|
||||
eprintln!("Part 01: {}", *a);
|
||||
}
|
||||
|
||||
#[cfg(feature="part2")]
|
||||
{
|
||||
let b = elves[..3].iter().sum::<i32>();
|
||||
eprintln!("Part 02: {}", b);
|
||||
}
|
||||
}
|
||||
|
107
src/day02/mod.rs
Normal file
107
src/day02/mod.rs
Normal file
@ -0,0 +1,107 @@
|
||||
use crate::read;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Hand {
|
||||
pub letter: char
|
||||
}
|
||||
|
||||
impl From<&str> for Hand {
|
||||
fn from(s: &str) -> Self {
|
||||
Self { letter: s.0.chars().next().unwrap() }
|
||||
}
|
||||
}
|
||||
|
||||
impl Hand {
|
||||
pub fn score(&self) -> i32 {
|
||||
match self.letter {
|
||||
'A' => 1,
|
||||
'B' => 2,
|
||||
'C' => 3,
|
||||
'X' => 1,
|
||||
'Y' => 2,
|
||||
'Z' => 3,
|
||||
_ => panic!("Unknown hand {}", self.letter)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn outcome(&self, other: &Hand) -> i32 {
|
||||
match self.score() - other.score() {
|
||||
-1 => 0,
|
||||
-2 => 6,
|
||||
0 => 3,
|
||||
1 => 6,
|
||||
2 => 0,
|
||||
_ => panic!("Unknown match {} vs {}", self.letter, other.letter)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn outcome_b(&self) -> i32 {
|
||||
match self.letter {
|
||||
'X' => 0,
|
||||
'Y' => 3,
|
||||
'Z' => 6,
|
||||
_ => panic!("Unknown outcome {}", self.letter)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn for_outcome(&self, enemy: &Hand) -> Self {
|
||||
let letter = match (self.outcome_b(), enemy.letter) {
|
||||
(o, l) if o == 3 => l,
|
||||
(o, l) if o == 0 && l == 'A' => 'C',
|
||||
(o, l) if o == 0 && l == 'B' => 'A',
|
||||
(o, l) if o == 0 && l == 'C' => 'B',
|
||||
(o, l) if o == 6 && l == 'A' => 'B',
|
||||
(o, l) if o == 6 && l == 'B' => 'C',
|
||||
(o, l) if o == 6 && l == 'C' => 'A',
|
||||
(o, l)=> panic!("Unknown pairing {}/{}", o, l)
|
||||
};
|
||||
Self {
|
||||
letter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Round {
|
||||
pub enemy: Hand,
|
||||
pub me: Hand,
|
||||
}
|
||||
|
||||
impl Round {
|
||||
pub fn score(&self) -> i32 {
|
||||
self.me.score() + self.me.outcome(&self.enemy)
|
||||
}
|
||||
|
||||
pub fn score_b(&self) -> i32 {
|
||||
let me = self.me.for_outcome(&self.enemy);
|
||||
me.score() + self.me.outcome_b()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run() {
|
||||
let input = read("02");
|
||||
let rounds = input
|
||||
.lines()
|
||||
.map(|l| {
|
||||
let hands = l
|
||||
.split_once(' ')
|
||||
.map(|c|
|
||||
(c.0.into(),
|
||||
c.1.into())
|
||||
).unwrap();
|
||||
Round {
|
||||
enemy: hands.0,
|
||||
me: hands.1,
|
||||
}
|
||||
}).map(|r|(r.score(), r.score_b()))
|
||||
.fold((0, 0), |agg, cur|(agg.0 + cur.0, agg.1 + cur.1));
|
||||
|
||||
#[cfg(feature = "part1")]
|
||||
{
|
||||
eprintln!("Day 2, Part 01: {}", rounds.0);
|
||||
}
|
||||
|
||||
#[cfg(feature = "part2")]
|
||||
{
|
||||
eprintln!("Day 2, Part 02: {}", rounds.1);
|
||||
}
|
||||
}
|
@ -1,7 +1,8 @@
|
||||
use chrono::prelude::*;
|
||||
use std::path::Path;
|
||||
use std::time::Instant;
|
||||
use chrono::prelude::*;
|
||||
mod day01;
|
||||
mod day02;
|
||||
|
||||
fn main() {
|
||||
let today = Local::now().day();
|
||||
@ -10,8 +11,7 @@ fn main() {
|
||||
day01::run();
|
||||
}
|
||||
if cfg!(feature = "day02") || (cfg!(feature = "today") && today == 2) {
|
||||
todo!()
|
||||
//day02::run();
|
||||
day02::run();
|
||||
}
|
||||
println!("Finished, time taken: {:?}", now.elapsed())
|
||||
}
|
||||
|
Reference in New Issue
Block a user