Day 6
This commit is contained in:
parent
3ba7f6066f
commit
1a0f1acf74
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -41,3 +41,11 @@ version = "0.1.0"
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "d05t2"
|
name = "d05t2"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "d06t1"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "d06t2"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
|
@ -12,4 +12,6 @@ members = [
|
||||||
"d04t2",
|
"d04t2",
|
||||||
"d05t1",
|
"d05t1",
|
||||||
"d05t2",
|
"d05t2",
|
||||||
|
"d06t1",
|
||||||
|
"d06t2",
|
||||||
]
|
]
|
||||||
|
|
23
d01t1/src/lib.rs
Normal file
23
d01t1/src/lib.rs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
pub fn d01t1(input: &str) -> u32 {
|
||||||
|
input
|
||||||
|
.lines()
|
||||||
|
.map(|line| {
|
||||||
|
let digits = line
|
||||||
|
.chars()
|
||||||
|
.filter(char::is_ascii_digit)
|
||||||
|
.collect::<Vec<char>>();
|
||||||
|
let mut num_str = digits.first().unwrap().to_string();
|
||||||
|
num_str.push(*digits.last().unwrap());
|
||||||
|
num_str.parse::<u32>().unwrap()
|
||||||
|
})
|
||||||
|
.sum::<u32>()
|
||||||
|
}
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn test_d01t1() {
|
||||||
|
let input = include_str!("../demo_input.txt");
|
||||||
|
assert_eq!(d01t1(input), 142);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,31 +1,8 @@
|
||||||
use std::fs::read_to_string;
|
use std::fs::read_to_string;
|
||||||
|
|
||||||
|
use d01t1::d01t1;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let input = read_to_string("input.txt").unwrap();
|
let input = read_to_string("input.txt").unwrap();
|
||||||
println!("d01t1: {}", d01t1(&input));
|
println!("d01t1: {}", d01t1(&input));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn d01t1(input: &str) -> u32 {
|
|
||||||
input
|
|
||||||
.lines()
|
|
||||||
.map(|line| {
|
|
||||||
let digits = line
|
|
||||||
.chars()
|
|
||||||
.filter(char::is_ascii_digit)
|
|
||||||
.collect::<Vec<char>>();
|
|
||||||
let mut num_str = digits.first().unwrap().to_string();
|
|
||||||
num_str.push(*digits.last().unwrap());
|
|
||||||
num_str.parse::<u32>().unwrap()
|
|
||||||
})
|
|
||||||
.sum::<u32>()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
#[test]
|
|
||||||
fn test_d01t1() {
|
|
||||||
let input = include_str!("../demo_input.txt");
|
|
||||||
assert_eq!(d01t1(input), 142);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
8
d06t1/Cargo.toml
Normal file
8
d06t1/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "d06t1"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
2
d06t1/demo_input.txt
Normal file
2
d06t1/demo_input.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
Time: 7 15 30
|
||||||
|
Distance: 9 40 200
|
45
d06t1/src/main.rs
Normal file
45
d06t1/src/main.rs
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
use std::fs::read_to_string;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let input = read_to_string("input.txt").unwrap();
|
||||||
|
println!("d06t1: {}", d06t1(&input));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn d06t1(input: &str) -> usize {
|
||||||
|
let mut lines = input.lines();
|
||||||
|
let mut timings = lines.next().unwrap().split_whitespace();
|
||||||
|
let mut distances = lines.next().unwrap().split_whitespace();
|
||||||
|
assert!(lines.next().is_none());
|
||||||
|
assert_eq!(timings.next().unwrap(), "Time:");
|
||||||
|
assert_eq!(distances.next().unwrap(), "Distance:");
|
||||||
|
let timings = timings
|
||||||
|
.map(str::parse::<usize>)
|
||||||
|
.map(Result::unwrap)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
let distances = distances
|
||||||
|
.map(str::parse::<usize>)
|
||||||
|
.map(Result::unwrap)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
timings
|
||||||
|
.into_iter()
|
||||||
|
.zip(distances)
|
||||||
|
.map(min_press_time)
|
||||||
|
.reduce(|a, b| a * b)
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn min_press_time((time, distance): (usize, usize)) -> usize {
|
||||||
|
(1..=time)
|
||||||
|
.filter(|acceleration_time| acceleration_time * (time - acceleration_time) > distance)
|
||||||
|
.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn test_d06t1() {
|
||||||
|
let input = include_str!("../demo_input.txt");
|
||||||
|
assert_eq!(d06t1(input), 288);
|
||||||
|
}
|
||||||
|
}
|
8
d06t2/Cargo.toml
Normal file
8
d06t2/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "d06t2"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
2
d06t2/demo_input.txt
Normal file
2
d06t2/demo_input.txt
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
Time: 7 15 30
|
||||||
|
Distance: 9 40 200
|
34
d06t2/src/main.rs
Normal file
34
d06t2/src/main.rs
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
use std::fs::read_to_string;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let input = read_to_string("input.txt").unwrap();
|
||||||
|
println!("d06t2: {}", d06t2(&input));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn d06t2(input: &str) -> usize {
|
||||||
|
let mut lines = input.lines();
|
||||||
|
let mut timings = lines.next().unwrap().split_whitespace();
|
||||||
|
let mut distances = lines.next().unwrap().split_whitespace();
|
||||||
|
assert!(lines.next().is_none());
|
||||||
|
assert_eq!(timings.next().unwrap(), "Time:");
|
||||||
|
assert_eq!(distances.next().unwrap(), "Distance:");
|
||||||
|
let timing = timings.collect::<String>().parse::<usize>().unwrap();
|
||||||
|
let distance = distances.collect::<String>().parse::<usize>().unwrap();
|
||||||
|
min_press_time((timing, distance))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn min_press_time((time, distance): (usize, usize)) -> usize {
|
||||||
|
(1..=time)
|
||||||
|
.filter(|acceleration_time| acceleration_time * (time - acceleration_time) > distance)
|
||||||
|
.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
#[test]
|
||||||
|
fn test_d06t2() {
|
||||||
|
let input = include_str!("../demo_input.txt");
|
||||||
|
assert_eq!(d06t2(input), 71503);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue