Skip to content

Commit

Permalink
Year 2023, Day 06
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Helbing committed Dec 7, 2023
1 parent 1b4b27f commit f5f48b2
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
82 changes: 82 additions & 0 deletions year2023/src/day06.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use common::day::Day;

pub struct Day06 {}

impl Day for Day06 {
fn star1(&self, input: &str) -> String {
let races = parse_part1(input);
races
.into_iter()
.map(|race| race.ways_to_win())
.product::<usize>()
.to_string()
}

fn star2(&self, input: &str) -> String {
let race = parse_part2(input);
race.ways_to_win().to_string()
}
}

struct Race {
time: usize,
distance: usize,
}

fn parse_part1(input: &str) -> Vec<Race> {
let lines: Vec<_> = input.lines().collect();
let times: Vec<_> = lines[0]
.split_whitespace()
.skip(1)
.map(|n| n.parse().unwrap())
.collect();
let distances: Vec<_> = lines[1]
.split_whitespace()
.skip(1)
.map(|n| n.parse().unwrap())
.collect();

times
.into_iter()
.zip(distances)
.map(|(time, distance)| Race { time, distance })
.collect()
}

fn parse_part2(input: &str) -> Race {
let lines: Vec<_> = input.lines().collect();
let time: String = lines[0].split_whitespace().skip(1).collect();
let distance: String = lines[1].split_whitespace().skip(1).collect();
Race {
time: time.parse().unwrap(),
distance: distance.parse().unwrap(),
}
}

impl Race {
fn ways_to_win(&self) -> usize {
(1..self.time)
.filter(|n| (self.time - n) * n > self.distance)
.count()
}
}

#[cfg(test)]
mod tests {
use super::*;

const INPUT: &str = r#"Time: 7 15 30
Distance: 9 40 200"#;

#[test]
fn star1() {
let d = Day06 {};
assert_eq!(d.star1(INPUT), "288");
}

#[test]
fn star2() {
let d = Day06 {};
assert_eq!(d.star2(INPUT), "71503");
}
}
2 changes: 2 additions & 0 deletions year2023/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod day02;
mod day03;
mod day04;
mod day05;
mod day06;

pub struct Year2023 {}

Expand All @@ -17,6 +18,7 @@ impl Year for Year2023 {
3 => Some(Box::new(day03::Day03 {})),
4 => Some(Box::new(day04::Day04 {})),
5 => Some(Box::new(day05::Day05 {})),
6 => Some(Box::new(day06::Day06 {})),
_ => None,
}
}
Expand Down

0 comments on commit f5f48b2

Please sign in to comment.