day 06
This commit is contained in:
parent
6d73fba8d0
commit
d6a7fc6fc6
4 changed files with 44 additions and 2 deletions
39
aoc2023/src/day06.rs
Normal file
39
aoc2023/src/day06.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
use std::iter::zip;
|
||||
use shared::{Solution, Answer};
|
||||
|
||||
pub struct Day06;
|
||||
|
||||
impl Solution for Day06 {
|
||||
fn part_1(&self, input: &str) -> Answer {
|
||||
let data: Vec<&str> = input.split("\n").collect();
|
||||
let times: Vec<u64> = data[0].split_whitespace().skip(1).map(|x| x.parse::<u64>().unwrap()).collect();
|
||||
let dists: Vec<u64> = data[1].split_whitespace().skip(1).map(|x| x.parse::<u64>().unwrap()).collect();
|
||||
|
||||
let mut out: u64 = 1;
|
||||
|
||||
for (t, d) in zip(times, dists) {
|
||||
let mut win: u64 = 0;
|
||||
for time in 1..t {
|
||||
if time * (t-time) > d { win +=1 }
|
||||
}
|
||||
out *= win
|
||||
}
|
||||
|
||||
Answer::from(out)
|
||||
}
|
||||
|
||||
fn part_2(&self, input: &str) -> Answer {
|
||||
let data: Vec<&str> = input.split("\n").collect();
|
||||
let time: u64 = data[0].split_whitespace().skip(1).collect::<Vec<&str>>().join("").parse::<u64>().unwrap();
|
||||
let dist: u64 = data[1].split_whitespace().skip(1).collect::<Vec<&str>>().join("").parse::<u64>().unwrap();
|
||||
|
||||
let mut win: u64 = 0;
|
||||
|
||||
for t in 1..time {
|
||||
if t * (time - t) > dist { win += 1 }
|
||||
}
|
||||
|
||||
Answer::from(win)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2,3 +2,4 @@ pub mod day01;
|
|||
pub mod day02;
|
||||
pub mod day03;
|
||||
pub mod day05;
|
||||
pub mod day06;
|
||||
|
|
|
|||
2
data/2023/day06
Normal file
2
data/2023/day06
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Time: 60 80 86 76
|
||||
Distance: 601 1163 1559 1300
|
||||
|
|
@ -9,10 +9,10 @@ use std::time::Instant;
|
|||
fn main() {
|
||||
let now = Instant::now();
|
||||
|
||||
let Ok(data) = load(2023, 5)
|
||||
let Ok(data) = load(2023, 6)
|
||||
else { return; };
|
||||
|
||||
let result = aoc2023::day05::Day05.part_2(&data);
|
||||
let result = aoc2023::day06::Day06.part_2(&data);
|
||||
|
||||
let elapsed = now.elapsed();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue