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"
|
regex = "1.7.0"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["a", "b", "today"]
|
default = ["part1", "part2", "today"]
|
||||||
a = []
|
part1 = []
|
||||||
b = []
|
part2 = []
|
||||||
today = []
|
today = []
|
||||||
01 = []
|
day01 = []
|
||||||
02 = []
|
day02 = []
|
||||||
03 = []
|
day03 = []
|
||||||
04 = []
|
day04 = []
|
||||||
05 = []
|
day05 = []
|
||||||
06 = []
|
day06 = []
|
||||||
07 = []
|
day07 = []
|
||||||
08 = []
|
day08 = []
|
||||||
09 = []
|
day09 = []
|
||||||
10 = []
|
day10 = []
|
||||||
11 = []
|
day11 = []
|
||||||
12 = []
|
day12 = []
|
||||||
13 = []
|
day13 = []
|
||||||
14 = []
|
day14 = []
|
||||||
15 = []
|
day15 = []
|
||||||
16 = []
|
day16 = []
|
||||||
17 = []
|
day17 = []
|
||||||
18 = []
|
day18 = []
|
||||||
19 = []
|
day19 = []
|
||||||
20 = []
|
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() {
|
pub fn run() {
|
||||||
let input = read("01");
|
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);
|
||||||
|
}
|
||||||
|
}
|
10
src/main.rs
10
src/main.rs
@ -1,17 +1,17 @@
|
|||||||
|
use chrono::prelude::*;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use chrono::prelude::*;
|
|
||||||
mod day01;
|
mod day01;
|
||||||
|
mod day02;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let today = Local::now().day();
|
let today = Local::now().day();
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
if cfg!(feature ="day01") || (cfg!(feature ="today") && today == 1) {
|
if cfg!(feature = "day01") || (cfg!(feature = "today") && today == 1) {
|
||||||
day01::run();
|
day01::run();
|
||||||
}
|
}
|
||||||
if cfg!(feature ="day02") || (cfg!(feature ="today") && today == 2) {
|
if cfg!(feature = "day02") || (cfg!(feature = "today") && today == 2) {
|
||||||
todo!()
|
day02::run();
|
||||||
//day02::run();
|
|
||||||
}
|
}
|
||||||
println!("Finished, time taken: {:?}", now.elapsed())
|
println!("Finished, time taken: {:?}", now.elapsed())
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user