Compare commits

..

11 Commits

Author SHA1 Message Date
8ae82163b4 Add day03 boilerplate 2022-12-02 07:53:41 +01:00
5a02a5bbe9 Fix from trait 2022-12-02 07:53:27 +01:00
a96624e0a0 Improve hand creation 2022-12-02 07:48:10 +01:00
be1cd454fe Cleanup iteration 2022-12-02 07:28:36 +01:00
38f1b5a037 Day 02 2022-12-02 07:06:34 +01:00
8dc9b8fcbb WIP day 2 2022-12-02 05:15:03 +00:00
d8f191fd9b Renamed daily fearures 2022-12-02 04:37:00 +00:00
77cb399748 Improved formatting 2022-12-02 04:36:50 +00:00
beeb89a2f6 Day 02 boilerplate 2022-12-01 07:36:30 +01:00
64851dd68b Fix part 1 feature flag 2022-12-01 07:23:51 +01:00
2fd479d02d Day 01 2022-12-01 07:14:55 +01:00
7 changed files with 4922 additions and 28 deletions

View File

@ -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 = []

File diff suppressed because it is too large Load Diff

2500
input/02.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -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
View 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.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);
}
}

23
src/day03/mod.rs Normal file
View File

@ -0,0 +1,23 @@
use crate::read;
pub fn run() {
let input = read("02");
let lines = input
.lines()
.map(|l| {
l
});
#[cfg(feature = "part1")]
{
let a = "TODO";
eprintln!("Day 2, Part 01: {}", a);
}
#[cfg(feature = "part2")]
{
let b = "TODO";
eprintln!("Day 2, Part 02: {}", b);
}
}

View File

@ -1,17 +1,21 @@
use chrono::prelude::*;
use std::path::Path;
use std::time::Instant;
use chrono::prelude::*;
mod day01;
mod day02;
mod day03;
fn main() {
let today = Local::now().day();
let now = Instant::now();
if cfg!(feature ="day01") || (cfg!(feature ="today") && today == 1) {
if cfg!(feature = "day01") || (cfg!(feature = "today") && today == 1) {
day01::run();
}
if cfg!(feature ="day02") || (cfg!(feature ="today") && today == 2) {
todo!()
//day02::run();
if cfg!(feature = "day02") || (cfg!(feature = "today") && today == 2) {
day02::run();
}
if cfg!(feature = "day03") || (cfg!(feature = "today") && today == 3) {
day03::run();
}
println!("Finished, time taken: {:?}", now.elapsed())
}