This commit is contained in:
BlackDemonFire 2023-12-12 08:25:33 +01:00
parent 38ccaeef6e
commit 9d8c5708fc
Signed by: lucy
SSH key fingerprint: SHA256:5NG0Um+vQQHuWkYn+Vd84YFm10edFBJiOIpOuHjl8UE
8 changed files with 138 additions and 0 deletions

8
Cargo.lock generated
View file

@ -65,3 +65,11 @@ version = "0.1.0"
[[package]]
name = "d08t2"
version = "0.1.0"
[[package]]
name = "d09t1"
version = "0.1.0"
[[package]]
name = "d09t2"
version = "0.1.0"

View file

@ -18,4 +18,6 @@ members = [
"d07t2",
"d08t1",
"d08t2",
"d09t1",
"d09t2",
]

8
d09t1/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "d09t1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

3
d09t1/demo_input.txt Normal file
View file

@ -0,0 +1,3 @@
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45

50
d09t1/src/main.rs Normal file
View file

@ -0,0 +1,50 @@
use std::fs::read_to_string;
fn main() {
let input = read_to_string("input.txt").unwrap();
println!("d09t1: {}", d09t1(&input));
}
pub fn d09t1(input: &str) -> isize {
input
.lines()
.map(str::split_whitespace)
.map(|line| {
line.map(str::parse::<isize>)
.map(Result::unwrap)
.collect::<Vec<_>>()
})
.map(find_next_in_sequence)
.sum()
}
pub fn find_next_in_sequence(sequence: Vec<isize>) -> isize {
let deltas = get_deltas(&sequence);
if *deltas.last().unwrap() == 0 {
*sequence.last().unwrap()
} else {
*sequence.last().unwrap() + find_next_in_sequence(deltas)
}
}
fn get_deltas(sequence: &Vec<isize>) -> Vec<isize> {
sequence
.windows(2)
.map(|window| window[1] - window[0])
.collect::<Vec<_>>()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_d09t1() {
let input = include_str!("../demo_input.txt");
assert_eq!(d09t1(input), 114);
}
#[test]
fn test_deltas() {
assert_eq!(get_deltas(&vec![0, 3, 6, 9, 12, 15]), vec![3, 3, 3, 3, 3])
}
}

8
d09t2/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "d09t2"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

3
d09t2/demo_input.txt Normal file
View file

@ -0,0 +1,3 @@
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45

56
d09t2/src/main.rs Normal file
View file

@ -0,0 +1,56 @@
use std::fs::read_to_string;
fn main() {
let input = read_to_string("input.txt").unwrap();
println!("d09t2: {}", d09t2(&input));
}
pub fn d09t2(input: &str) -> isize {
input
.lines()
.map(str::split_whitespace)
.map(|line| {
line.map(str::parse::<isize>)
.map(Result::unwrap)
.rev()
.collect::<Vec<_>>()
})
.map(find_next_in_sequence)
.sum()
}
pub fn find_next_in_sequence(sequence: Vec<isize>) -> isize {
let deltas = get_deltas(&sequence);
if deltas.iter().all(|&e| e == 0) {
*sequence.last().unwrap()
} else {
*sequence.last().unwrap() + find_next_in_sequence(deltas)
}
}
fn get_deltas(sequence: &Vec<isize>) -> Vec<isize> {
sequence
.windows(2)
.map(|window| window[1] - window[0])
.collect::<Vec<_>>()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_d09t2() {
let input = include_str!("../demo_input.txt");
assert_eq!(d09t2(input), 2);
}
#[test]
fn test_deltas() {
assert_eq!(get_deltas(&vec![0, 3, 6, 9, 12, 15]), vec![3, 3, 3, 3, 3])
}
#[test]
fn test_line_1() {
assert_eq!(d09t2("10 13 16 21 30 45"), 5)
}
}