Day 6 was easy af

This commit is contained in:
Tyler Beckman 2023-12-05 22:50:36 -07:00
parent 161b341400
commit 81f604249c
Signed by: Ty
GPG key ID: 2813440C772555A4
2 changed files with 49 additions and 1 deletions

45
src/day6/mod.rs Normal file
View file

@ -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::<Vec<_>>()).collect::<Vec<_>>();
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::<String>()).collect::<Vec<_>>();
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()
}
}

View file

@ -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::<Duration>::new();