generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
76 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use itertools::{multizip, Itertools}; | ||
|
||
advent_of_code::solution!(6); | ||
|
||
pub fn part_one(input: &str) -> Option<usize> { | ||
// Parse input into tuples of (time, speed) in usize based on | ||
// Time: 7 15 30 | ||
// Distance: 9 40 200 | ||
let mut times_distances = input | ||
.lines() | ||
.map(|s| s.split_whitespace().filter_map(|s| s.parse::<usize>().ok())); | ||
|
||
let times = times_distances.next().unwrap(); | ||
let distances = times_distances.next().unwrap(); | ||
let rounds = times.zip(distances); | ||
|
||
let score = rounds | ||
.map(|(time, record_distance)| { | ||
(0..time) | ||
.map(|press| { | ||
let remaining = time - press; | ||
let distance = press * remaining; | ||
distance | ||
}) | ||
.filter(|&distance| distance > record_distance) | ||
.count() | ||
}) | ||
.reduce(|a, b| a * b); | ||
|
||
score | ||
} | ||
|
||
pub fn part_two(input: &str) -> Option<usize> { | ||
let [time, speed]: [usize; 2] = input | ||
.lines() | ||
.map(|s| s.chars().filter(|c| c.is_numeric()).collect::<String>()) | ||
.filter_map(|s| s.parse::<usize>().ok()) | ||
.collect_vec() | ||
.try_into() | ||
.unwrap(); | ||
|
||
(0..time) | ||
.map(|press| { | ||
let remaining = time - press; | ||
let distance = press * remaining; | ||
distance | ||
}) | ||
.filter(|&distance| distance > speed) | ||
.count() | ||
.into() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_part_one() { | ||
let result = part_one(&advent_of_code::template::read_file("examples", DAY)); | ||
assert_eq!(result, Some(288)); | ||
} | ||
|
||
#[test] | ||
fn test_part_two() { | ||
let result = part_two(&advent_of_code::template::read_file("examples", DAY)); | ||
assert_eq!(result, Some(71503)); | ||
} | ||
} |