This commit is contained in:
JonOfUs 2023-12-06 13:26:20 +01:00
parent 83b170f6b9
commit 64429bfaf2
4 changed files with 56 additions and 1 deletions

2
res/06/example Normal file
View file

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200

2
res/06/input Normal file
View file

@ -0,0 +1,2 @@
Time: 51 92 68 90
Distance: 222 2031 1126 1225

51
src/days/d06.rs Normal file
View file

@ -0,0 +1,51 @@
use std::fs;
pub fn solve() {
let path = "res/06/input";
let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let times: Vec<usize> = contents.lines().next().unwrap().split_whitespace().skip(1).map(|s| s.parse::<usize>().unwrap()).collect();
let records: Vec<usize> = contents.lines().skip(1).next().unwrap().split_whitespace().skip(1).map(|s| s.parse::<usize>().unwrap()).collect();
let distances: Vec<Vec<usize>> = times
.iter()
.map(|t| {
(0..*t).map(|load_t| {
load_t * (t-load_t)
})
.collect::<Vec::<usize>>()
})
.collect();
let result: usize = distances.iter()
.enumerate()
.map(|(i, dist)| {
dist.iter()
.filter(|d| **d > records[i])
.count()
})
.product();
println!("Result 1: {}", result);
let act_time: usize = times.iter()
.map(|t| t.to_string())
.collect::<String>()
.parse()
.unwrap();
let act_record: usize = records.iter()
.map(|t| t.to_string())
.collect::<String>()
.parse()
.unwrap();
let result: usize = (0..act_time).map(|load_t| {
load_t * (act_time-load_t)
})
.filter(|d| *d > act_record)
.count();
println!("Result 2: {}", result);
}

View file

@ -1,7 +1,7 @@
pub mod days; pub mod days;
fn main() { fn main() {
days::d05::solve() days::d06::solve()
//_all_days() //_all_days()
} }