This commit is contained in:
Alie 2023-12-06 10:01:35 +01:00
parent 9a2109e728
commit 0747e3b180
6 changed files with 117 additions and 23 deletions

4
Cargo.lock generated
View File

@ -34,6 +34,10 @@ version = "0.1.0"
name = "day05"
version = "0.1.0"
[[package]]
name = "day06"
version = "0.1.0"
[[package]]
name = "memchr"
version = "2.6.4"

View File

@ -7,7 +7,7 @@ members = [
"day03",
"day04",
"day05",
# "day06",
"day06",
# "day07",
# "day08",
# "day09",

8
day06/Cargo.toml Normal file
View File

@ -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]

2
day06/input.txt Normal file
View File

@ -0,0 +1,2 @@
Time: 42 89 91 89
Distance: 308 1170 1291 1467

80
day06/src/main.rs Normal file
View File

@ -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);
}
}

View File

@ -2,18 +2,18 @@ use std::fs;
fn main() {
const FILE_PATH: &str = "input.txt";
println!("Welcome to AOC2023 day NUMBER, 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!(
"Welcome to AOC2023 day NUMBER, 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!");
//let mut data = parse(&contents);
//println!("First thing: {}", calculate_1(&data));
//println!("Second thing: {}", calculate_2(&data));
}
#[cfg(test)]
mod test {
const INPUT: &str = r#""#;
@ -24,7 +24,7 @@ mod test {
assert_eq!(calculate_1(&tst), 24000);
} */
/* #[test]
fn testtemplate2() {
fn test2() {
let tst = parse(INPUT);
assert_eq!(calculate_2(&tst), 24000);
} */