day6 part1

This commit is contained in:
Candygoblen123 2023-12-06 10:32:43 -05:00
parent aca265c91b
commit fea1f85aee
No known key found for this signature in database
GPG Key ID: 577DA64EBEF10385
4 changed files with 47 additions and 0 deletions

7
day6/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day6"
version = "0.1.0"

8
day6/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[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]

2
day6/input.txt Normal file
View File

@ -0,0 +1,2 @@
Time: 63 78 94 68
Distance: 411 1274 2047 1035

30
day6/src/main.rs Normal file
View File

@ -0,0 +1,30 @@
use std::fs;
fn main() {
let input = fs::read_to_string("input.txt").unwrap();
let input: Vec<_> = input.split('\n') // Seperate the Time and Distance lines
.map(|line| {
&line[line.find(':').unwrap() + 1..] // Drop "Time:" and "Distance:"
}).map(|line| {
line.split_whitespace() // Split the numbers into their own elements.
.map(|num| num.parse::<i32>().expect("Couldn't parse number")) // Parse numbers into i32
.collect::<Vec<_>>()
}).collect(); // collect into Vec
let mut valid_total = 1;
for round in 0..input.first().unwrap().len() {
let time = input[0][round];
let dist = input[1][round];
let mut valid = 0;
for remaining_time in 0..time {
if (time - remaining_time) * remaining_time > dist {
valid += 1;
}
}
valid_total *= valid;
}
println!("{}", valid_total);
}