Add day 5, 6, 7
This commit is contained in:
1
2023/day6/day6/.gitignore
vendored
Normal file
1
2023/day6/day6/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
54
2023/day6/day6/Cargo.lock
generated
Normal file
54
2023/day6/day6/Cargo.lock
generated
Normal file
@@ -0,0 +1,54 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day6"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
|
9
2023/day6/day6/Cargo.toml
Normal file
9
2023/day6/day6/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "day6"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
regex = "1.10.2"
|
3
2023/day6/day6/input.txt
Normal file
3
2023/day6/day6/input.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
Time: 63 78 94 68
|
||||
Distance: 411 1274 2047 1035
|
||||
|
43
2023/day6/day6/src/main.rs
Normal file
43
2023/day6/day6/src/main.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{BufRead, BufReader},
|
||||
};
|
||||
|
||||
fn main() {
|
||||
// Open the file
|
||||
let file = File::open("input.txt").expect("Failed to open file");
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let mut lines = reader.lines().map(|l| l.unwrap());
|
||||
|
||||
let line1 = lines.next().unwrap();
|
||||
let line2 = lines.next().unwrap();
|
||||
|
||||
let times: Vec<i64> = line1.split(' ').filter_map(|s| s.parse().ok()).collect();
|
||||
|
||||
let distances: Vec<i64> = line2.split(' ').filter_map(|s| s.parse().ok()).collect();
|
||||
|
||||
println!("Part 1: {}", calc(times, distances));
|
||||
|
||||
println!("Part 2: {}", calc(vec![63789468], vec![411127420471035]));
|
||||
}
|
||||
|
||||
fn calc(times: Vec<i64>, distances: Vec<i64>) -> i32 {
|
||||
let mut out = 1;
|
||||
|
||||
for (&t, &d) in times.iter().zip(distances.iter()) {
|
||||
let mut count = 0;
|
||||
|
||||
for speed in 1..t {
|
||||
let total = (t - speed) * speed;
|
||||
|
||||
if total > d {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
||||
out *= count;
|
||||
}
|
||||
|
||||
out
|
||||
}
|
Reference in New Issue
Block a user