feat: implement day 06
This commit is contained in:
parent
f165c8fa66
commit
39aa33152f
104
src/day06.rs
Normal file
104
src/day06.rs
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
pub fn execute_task01(content: &str) {
|
||||||
|
let start = Instant::now();
|
||||||
|
let product_correct_races = solve_01(content).unwrap();
|
||||||
|
let duration = start.elapsed();
|
||||||
|
|
||||||
|
assert_eq!(1624896, product_correct_races);
|
||||||
|
println!("Day06 - Task01 - Duration: {duration:?} - Product Correct Races: {}", product_correct_races)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn execute_task02(content: &str) {
|
||||||
|
let start = Instant::now();
|
||||||
|
let product_correct_races = solve_02(content).unwrap();
|
||||||
|
let duration = start.elapsed();
|
||||||
|
|
||||||
|
//assert_eq!(322500873, product_correct_races);
|
||||||
|
println!("Day06 - Task02 - Duration: {duration:?} - Correct Race: {}", product_correct_races)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_01(content: &str) -> anyhow::Result<usize> {
|
||||||
|
let result = content
|
||||||
|
.lines()
|
||||||
|
.map(|line| line
|
||||||
|
.split_once(":")
|
||||||
|
.unwrap()
|
||||||
|
.1
|
||||||
|
.trim())
|
||||||
|
.map(|line| line
|
||||||
|
.split(" ")
|
||||||
|
.filter(|part| part.len() > 0)
|
||||||
|
.map(|data| data.parse::<i64>().unwrap())
|
||||||
|
.collect::<Vec<i64>>())
|
||||||
|
.collect::<Vec<Vec<i64>>>();
|
||||||
|
|
||||||
|
let mut races = vec![];
|
||||||
|
|
||||||
|
for i in 0..result[0].len() {
|
||||||
|
races.push((result[0][i].clone(), result[1][i].clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(races
|
||||||
|
.iter()
|
||||||
|
.map(|(time, distance)| calc_possible_options(*time, *distance))
|
||||||
|
.map(|options| options.len())
|
||||||
|
.product())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_02(content: &str) -> anyhow::Result<usize> {
|
||||||
|
let result = content
|
||||||
|
.lines()
|
||||||
|
.map(|line| line
|
||||||
|
.split_once(":")
|
||||||
|
.unwrap()
|
||||||
|
.1
|
||||||
|
.trim())
|
||||||
|
.map(|line| line
|
||||||
|
.replace(" ", "")
|
||||||
|
.parse::<i64>().unwrap())
|
||||||
|
.collect::<Vec<i64>>();
|
||||||
|
|
||||||
|
let mut races = vec![(result[0].clone(), result[1].clone())];
|
||||||
|
Ok(races
|
||||||
|
.iter()
|
||||||
|
.map(|(time, distance)| calc_possible_options(*time, *distance))
|
||||||
|
.map(|options| options.len())
|
||||||
|
.product())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn calc_possible_options(time: i64, distance: i64) -> Vec<i64> {
|
||||||
|
let mut data = vec![];
|
||||||
|
// TODO Performance!!!
|
||||||
|
for i in 1..time {
|
||||||
|
let y = i * (time - i);
|
||||||
|
if y > distance {
|
||||||
|
data.push(i)
|
||||||
|
} else if data.len() > 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_solve_01() {
|
||||||
|
let example = r#"Time: 7 15 30
|
||||||
|
Distance: 9 40 200"#;
|
||||||
|
|
||||||
|
let result = solve_01(example).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(288, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_solve_02() {
|
||||||
|
let example = r#"Time: 7 15 30
|
||||||
|
Distance: 9 40 200"#;
|
||||||
|
|
||||||
|
let result = solve_02(example).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(71503, result);
|
||||||
|
}
|
2
src/input/day06/input.txt
Normal file
2
src/input/day06/input.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Time: 56 97 78 75
|
||||||
|
Distance: 546 1927 1131 1139
|
@ -4,12 +4,14 @@ mod day02;
|
|||||||
mod day03;
|
mod day03;
|
||||||
mod day04;
|
mod day04;
|
||||||
mod day05;
|
mod day05;
|
||||||
|
mod day06;
|
||||||
|
|
||||||
const CONTENT01: &'static str = include_str!("input/day01/input.txt");
|
const CONTENT01: &'static str = include_str!("input/day01/input.txt");
|
||||||
const CONTENT02: &'static str = include_str!("input/day02/input.txt");
|
const CONTENT02: &'static str = include_str!("input/day02/input.txt");
|
||||||
const CONTENT03: &'static str = include_str!("input/day03/input.txt");
|
const CONTENT03: &'static str = include_str!("input/day03/input.txt");
|
||||||
const CONTENT04: &'static str = include_str!("input/day04/input.txt");
|
const CONTENT04: &'static str = include_str!("input/day04/input.txt");
|
||||||
const CONTENT05: &'static str = include_str!("input/day05/input.txt");
|
const CONTENT05: &'static str = include_str!("input/day05/input.txt");
|
||||||
|
const CONTENT06: &'static str = include_str!("input/day06/input.txt");
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
day01::execute_task01(CONTENT01);
|
day01::execute_task01(CONTENT01);
|
||||||
@ -25,5 +27,8 @@ fn main() {
|
|||||||
day04::execute_task02(CONTENT04);
|
day04::execute_task02(CONTENT04);
|
||||||
println!();
|
println!();
|
||||||
day05::execute_task01(CONTENT05);
|
day05::execute_task01(CONTENT05);
|
||||||
day05::execute_task02(CONTENT05);
|
//day05::execute_task02(CONTENT05); // Performance Issues
|
||||||
|
println!();
|
||||||
|
day06::execute_task01(CONTENT06);
|
||||||
|
day06::execute_task02(CONTENT06);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user