From 81f604249c7ab0b29b479ebcb2c3a543db616c1f Mon Sep 17 00:00:00 2001 From: Ty Date: Tue, 5 Dec 2023 22:50:36 -0700 Subject: [PATCH] Day 6 was easy af --- src/day6/mod.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 5 ++++- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/day6/mod.rs diff --git a/src/day6/mod.rs b/src/day6/mod.rs new file mode 100644 index 0000000..ff2357a --- /dev/null +++ b/src/day6/mod.rs @@ -0,0 +1,45 @@ +use crate::utils::Day; + +#[derive(Debug, Default)] +pub struct Day6 { + pub input: String, +} + +impl Day for Day6 { + fn part1(&mut self) -> String { + let lines = self.input.lines().map(|l| l.split_ascii_whitespace().skip(1).collect::>()).collect::>(); + let races = [ + (lines[0][0].parse().unwrap(), lines[1][0].parse().unwrap()), + (lines[0][1].parse().unwrap(), lines[1][1].parse().unwrap()), + (lines[0][2].parse().unwrap(), lines[1][2].parse().unwrap()), + (lines[0][3].parse().unwrap(), lines[1][3].parse().unwrap()), + ]; + + let mut acc = 1usize; + + for (time, record_distance) in races { + let mut wins = 0usize; + for time_holding_button in 0..time { + let distance = (time - time_holding_button) * time_holding_button /* velocity */; + if distance > record_distance { wins += 1; } + } + + acc *= wins; + } + + acc.to_string() + } + + fn part2(&mut self) -> String { + let lines = self.input.lines().map(|l| l[10..].split_ascii_whitespace().collect::()).collect::>(); + + let (time, record_distance): (u128, u128) = (lines[0].parse().unwrap(), lines[1].parse().unwrap()); + let mut wins = 0u128; + for time_holding_button in 0..time { + let distance = (time - time_holding_button) * time_holding_button /* velocity */; + if distance > record_distance { wins += 1; } + } + + wins.to_string() + } +} diff --git a/src/main.rs b/src/main.rs index c0c5f0e..2ddfbd1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,13 +6,14 @@ use std::{ use clap::{Parser, Subcommand}; -use crate::{day1::Day1, day2::Day2, day3::Day3, day4::Day4, day5::Day5, utils::Day}; +use crate::{day1::Day1, day2::Day2, day3::Day3, day4::Day4, day5::Day5, day6::Day6, utils::Day}; pub mod day1; pub mod day2; pub mod day3; pub mod day4; pub mod day5; +pub mod day6; pub mod fetcher; pub mod utils; @@ -92,6 +93,7 @@ async fn main() { cards: HashMap::new(), }), 5 => Box::new(Day5 { input }), + 6 => Box::new(Day6 { input }), _ => panic!("Invalid day #"), }; let start = std::time::Instant::now(); @@ -146,6 +148,7 @@ async fn main() { cards: HashMap::new(), }), 5 => Box::new(Day5 { input: response }), + 6 => Box::new(Day6 { input: response }), _ => panic!("Invalid day #"), }; let mut timings = Vec::::new();