diff --git a/Cargo.lock b/Cargo.lock index aaa326d..60bf18b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 408991f..502c271 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ members = [ "day03", "day04", "day05", -# "day06", + "day06", # "day07", # "day08", # "day09", diff --git a/day06/Cargo.toml b/day06/Cargo.toml new file mode 100644 index 0000000..2f276f3 --- /dev/null +++ b/day06/Cargo.toml @@ -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] diff --git a/day06/input.txt b/day06/input.txt new file mode 100644 index 0000000..12800f6 --- /dev/null +++ b/day06/input.txt @@ -0,0 +1,2 @@ +Time: 42 89 91 89 +Distance: 308 1170 1291 1467 \ No newline at end of file diff --git a/day06/src/main.rs b/day06/src/main.rs new file mode 100644 index 0000000..b12dede --- /dev/null +++ b/day06/src/main.rs @@ -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::() + }); + ( + 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); + } +} diff --git a/template/src/main.rs b/template/src/main.rs index 4293617..a1c32f1 100644 --- a/template/src/main.rs +++ b/template/src/main.rs @@ -1,31 +1,31 @@ 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!("And we parse the file!"); - //let mut data = parse(&contents); - //println!("First thing: {}", calculate_1(&data)); - //println!("Second thing: {}", calculate_2(&data)); + 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!("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#""#; - use super::*; -/* #[test] - fn test1() { - let tst = parse(INPUT); - assert_eq!(calculate_1(&tst), 24000); - } */ + const INPUT: &str = r#""#; + use super::*; + /* #[test] + fn test1() { + let tst = parse(INPUT); + assert_eq!(calculate_1(&tst), 24000); + } */ /* #[test] - fn testtemplate2() { - let tst = parse(INPUT); - assert_eq!(calculate_2(&tst), 24000); - } */ -} \ No newline at end of file + fn test2() { + let tst = parse(INPUT); + assert_eq!(calculate_2(&tst), 24000); + } */ +}