Day 9
This commit is contained in:
parent
38ccaeef6e
commit
9d8c5708fc
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -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"
|
||||
|
|
|
@ -18,4 +18,6 @@ members = [
|
|||
"d07t2",
|
||||
"d08t1",
|
||||
"d08t2",
|
||||
"d09t1",
|
||||
"d09t2",
|
||||
]
|
||||
|
|
8
d09t1/Cargo.toml
Normal file
8
d09t1/Cargo.toml
Normal 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
3
d09t1/demo_input.txt
Normal 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
50
d09t1/src/main.rs
Normal 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
8
d09t2/Cargo.toml
Normal 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
3
d09t2/demo_input.txt
Normal 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
56
d09t2/src/main.rs
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue