day 6
This commit is contained in:
parent
9a2109e728
commit
0747e3b180
|
@ -34,6 +34,10 @@ version = "0.1.0"
|
||||||
name = "day05"
|
name = "day05"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day06"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.6.4"
|
version = "2.6.4"
|
||||||
|
|
|
@ -7,7 +7,7 @@ members = [
|
||||||
"day03",
|
"day03",
|
||||||
"day04",
|
"day04",
|
||||||
"day05",
|
"day05",
|
||||||
# "day06",
|
"day06",
|
||||||
# "day07",
|
# "day07",
|
||||||
# "day08",
|
# "day08",
|
||||||
# "day09",
|
# "day09",
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "day06"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,2 @@
|
||||||
|
Time: 42 89 91 89
|
||||||
|
Distance: 308 1170 1291 1467
|
|
@ -0,0 +1,80 @@
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
const FILE_PATH: &str = "input.txt";
|
||||||
|
println!(
|
||||||
|
"Welcome to AOC2023 day 6, first we will read the file {}",
|
||||||
|
FILE_PATH
|
||||||
|
);
|
||||||
|
let contents = fs::read_to_string(FILE_PATH).expect("Should have been able to read the file");
|
||||||
|
|
||||||
|
println!("And we parse the file!");
|
||||||
|
println!("First thing: {}", calculate_1(&parse1(&contents)));
|
||||||
|
println!("Second thing: {}", calculate(&parse2(&contents)));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse1(input: &str) -> Vec<(u64, u64)> {
|
||||||
|
let mut lines = input.lines().map(|line| {
|
||||||
|
line
|
||||||
|
.split(": ")
|
||||||
|
.nth(1)
|
||||||
|
.unwrap()
|
||||||
|
.trim()
|
||||||
|
.split_ascii_whitespace()
|
||||||
|
.map(|n| n.parse().expect("NAN"))
|
||||||
|
});
|
||||||
|
let time = lines.next().unwrap();
|
||||||
|
let mut distance = lines.next().unwrap();
|
||||||
|
let mut output = Vec::new();
|
||||||
|
time.for_each(|n| output.push((n, distance.next().unwrap())));
|
||||||
|
output
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse2(input: &str) -> (u64, u64) {
|
||||||
|
let mut lines = input.lines().map(|line| {
|
||||||
|
line
|
||||||
|
.split(": ")
|
||||||
|
.nth(1)
|
||||||
|
.unwrap()
|
||||||
|
.chars()
|
||||||
|
.filter(|c| !c.is_ascii_whitespace())
|
||||||
|
.collect::<String>()
|
||||||
|
});
|
||||||
|
(
|
||||||
|
lines.next().unwrap().parse().expect("NAN"),
|
||||||
|
lines.next().unwrap().parse().expect("NAN"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calculate_1(races: &Vec<(u64, u64)>) -> u64 {
|
||||||
|
races
|
||||||
|
.iter()
|
||||||
|
.map(|race| calculate(race))
|
||||||
|
.fold(1, |a, n| a * n)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calculate((time, distance): &(u64, u64)) -> u64 {
|
||||||
|
(1..*time)
|
||||||
|
.map(|p| p * (time - p))
|
||||||
|
.filter(|n| n > &distance)
|
||||||
|
.count()
|
||||||
|
.try_into()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
const INPUT: &str = r#"Time: 7 15 30
|
||||||
|
Distance: 9 40 200"#;
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn test1() {
|
||||||
|
let tst = parse1(INPUT);
|
||||||
|
assert_eq!(calculate_1(&tst), 288);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test2() {
|
||||||
|
let tst = parse2(INPUT);
|
||||||
|
assert_eq!(calculate(&tst), 71503);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,31 +1,31 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
const FILE_PATH: &str = "input.txt";
|
const FILE_PATH: &str = "input.txt";
|
||||||
println!("Welcome to AOC2023 day NUMBER, first we will read the file {}", FILE_PATH);
|
println!(
|
||||||
let contents = fs::read_to_string(FILE_PATH)
|
"Welcome to AOC2023 day NUMBER, first we will read the file {}",
|
||||||
.expect("Should have been able to read the file");
|
FILE_PATH
|
||||||
|
);
|
||||||
println!("And we parse the file!");
|
let contents = fs::read_to_string(FILE_PATH).expect("Should have been able to read the file");
|
||||||
//let mut data = parse(&contents);
|
|
||||||
//println!("First thing: {}", calculate_1(&data));
|
|
||||||
//println!("Second thing: {}", calculate_2(&data));
|
|
||||||
|
|
||||||
|
println!("And we parse the file!");
|
||||||
|
//let mut data = parse(&contents);
|
||||||
|
//println!("First thing: {}", calculate_1(&data));
|
||||||
|
//println!("Second thing: {}", calculate_2(&data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
const INPUT: &str = r#""#;
|
const INPUT: &str = r#""#;
|
||||||
use super::*;
|
use super::*;
|
||||||
/* #[test]
|
/* #[test]
|
||||||
fn test1() {
|
fn test1() {
|
||||||
let tst = parse(INPUT);
|
let tst = parse(INPUT);
|
||||||
assert_eq!(calculate_1(&tst), 24000);
|
assert_eq!(calculate_1(&tst), 24000);
|
||||||
} */
|
} */
|
||||||
/* #[test]
|
/* #[test]
|
||||||
fn testtemplate2() {
|
fn test2() {
|
||||||
let tst = parse(INPUT);
|
let tst = parse(INPUT);
|
||||||
assert_eq!(calculate_2(&tst), 24000);
|
assert_eq!(calculate_2(&tst), 24000);
|
||||||
} */
|
} */
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue