day 6
This commit is contained in:
parent
7e0d58a8ea
commit
691b44d1e9
2
input/6.txt
Normal file
2
input/6.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Time: 62 64 91 90
|
||||||
|
Distance: 553 1010 1473 1074
|
@ -1,5 +1,10 @@
|
|||||||
use std::{fs::File, io::{BufReader, BufRead}};
|
use std::{
|
||||||
|
fs::File,
|
||||||
|
io::{BufRead, BufReader},
|
||||||
|
str::FromStr,
|
||||||
|
};
|
||||||
|
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
pub fn load_input(filepath: &str) -> Result<Vec<String>, Box<dyn std::error::Error>> {
|
pub fn load_input(filepath: &str) -> Result<Vec<String>, Box<dyn std::error::Error>> {
|
||||||
let file = File::open(filepath)?;
|
let file = File::open(filepath)?;
|
||||||
@ -13,4 +18,17 @@ pub fn load_input(filepath: &str) -> Result<Vec<String>, Box<dyn std::error::Err
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse_nums<T>(s: &str) -> Vec<T>
|
||||||
|
where
|
||||||
|
T: FromStr,
|
||||||
|
{
|
||||||
|
let regex = Regex::new(r"(?m)(\d+)").unwrap();
|
||||||
|
let result = regex.captures_iter(s);
|
||||||
|
|
||||||
|
result
|
||||||
|
.map(|e| e.get(0).unwrap().as_str())
|
||||||
|
.flat_map(|e| e.parse::<T>())
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
@ -6,6 +6,7 @@ mod two;
|
|||||||
mod three;
|
mod three;
|
||||||
mod four;
|
mod four;
|
||||||
mod five;
|
mod five;
|
||||||
|
mod six;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
@ -22,6 +23,8 @@ fn main() {
|
|||||||
"4p2" => four::run_part2(),
|
"4p2" => four::run_part2(),
|
||||||
"5" => five::run(),
|
"5" => five::run(),
|
||||||
"5p2" => five::run_part2(),
|
"5p2" => five::run_part2(),
|
||||||
|
"6" => six::run(),
|
||||||
|
"6p2" => six::run_part2(),
|
||||||
_ => Ok(-1),
|
_ => Ok(-1),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
54
src/six.rs
Normal file
54
src/six.rs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
use crate::common::{load_input, parse_nums};
|
||||||
|
|
||||||
|
fn parse_input(lines: &[String]) -> (Vec<u64>, Vec<u64>) {
|
||||||
|
(
|
||||||
|
parse_nums(lines.get(0).unwrap()),
|
||||||
|
parse_nums(lines.get(1).unwrap()),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn distance_by_time_pressed(time_pressed: u64, race_time: u64) -> u64 {
|
||||||
|
let speed = (race_time - time_pressed);
|
||||||
|
return speed * time_pressed;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn join_nums(nums: &[u64]) -> u64 {
|
||||||
|
nums.iter()
|
||||||
|
.map(|e| e.to_string())
|
||||||
|
.collect::<String>()
|
||||||
|
.parse()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run_part2() -> Result<i32, Box<dyn std::error::Error>> {
|
||||||
|
let lines = load_input("./input/6.txt")?;
|
||||||
|
let (time, distance) = parse_input(&lines);
|
||||||
|
let new_time = join_nums(&time);
|
||||||
|
let new_dist = join_nums(&distance);
|
||||||
|
|
||||||
|
let res = (0..new_time)
|
||||||
|
.map(|pressed| distance_by_time_pressed(pressed, new_time))
|
||||||
|
.filter(|e| *e > new_dist)
|
||||||
|
.count();
|
||||||
|
|
||||||
|
Ok(res.try_into().unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||||
|
let lines = load_input("./input/6.txt")?;
|
||||||
|
let (time, distance) = parse_input(&lines);
|
||||||
|
|
||||||
|
let res = time
|
||||||
|
.iter()
|
||||||
|
.zip(distance.iter())
|
||||||
|
.map(|e| {
|
||||||
|
(0..*e.0)
|
||||||
|
.map(|pressed| distance_by_time_pressed(pressed, *e.0))
|
||||||
|
.filter(|dist| *dist > *e.1)
|
||||||
|
.count()
|
||||||
|
})
|
||||||
|
.reduce(|acc, e| e * acc)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(res.try_into().unwrap())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user