Complete day 9
This commit is contained in:
parent
49edaf0ae1
commit
af685599f5
1
day-9/.gitignore
vendored
Normal file
1
day-9/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target/
|
94
day-9/Cargo.lock
generated
Normal file
94
day-9/Cargo.lock
generated
Normal file
|
@ -0,0 +1,94 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "day-9"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"indoc",
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||
|
||||
[[package]]
|
||||
name = "indoc"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47741a8bc60fb26eb8d6e0238bbb26d8575ff623fdc97b1a2c00c050b9684ed8"
|
||||
dependencies = [
|
||||
"indoc-impl",
|
||||
"proc-macro-hack",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc-impl"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce046d161f000fffde5f432a0d034d0341dc152643b2598ed5bfce44c4f3a8f0"
|
||||
dependencies = [
|
||||
"proc-macro-hack",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"unindent",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-hack"
|
||||
version = "0.5.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.54"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a2af957a63d6bd42255c359c93d9bfdb97076bd3b820897ce55ffbfbf107f44"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
|
||||
|
||||
[[package]]
|
||||
name = "unindent"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f14ee04d9415b52b3aeab06258a3f07093182b88ba0f9b8d203f211a7a7d41c7"
|
11
day-9/Cargo.toml
Normal file
11
day-9/Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "day-9"
|
||||
version = "0.1.0"
|
||||
authors = ["Tristan Daniël Maat <tm@tlater.net>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
indoc = "0.3"
|
||||
itertools = "0.9"
|
1000
day-9/input
Normal file
1000
day-9/input
Normal file
File diff suppressed because it is too large
Load diff
134
day-9/src/main.rs
Normal file
134
day-9/src/main.rs
Normal file
|
@ -0,0 +1,134 @@
|
|||
use std::fs;
|
||||
use std::num::ParseIntError;
|
||||
|
||||
use itertools::{Itertools, MinMaxResult};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = fs::read_to_string("input")?;
|
||||
let xmas = parse_xmas(&input)?;
|
||||
|
||||
// Part 1
|
||||
let result = find_not_summed(&xmas, 25)?;
|
||||
println!("{}", result);
|
||||
|
||||
// Part 2
|
||||
let result = find_weakness(&xmas, result)?;
|
||||
println!("{}", result);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_xmas(input: &str) -> Result<Vec<u64>, ParseIntError> {
|
||||
input.lines().map(|line| line.parse()).collect()
|
||||
}
|
||||
|
||||
fn find_weakness(xmas: &Vec<u64>, key: u64) -> Result<u64, &str> {
|
||||
// Now we use the key to find the weakness, by getting all
|
||||
// possible sub-sumbs of the list of numbers, and finding the one
|
||||
// that happens to match our key
|
||||
let weak_window = (2..xmas.len())
|
||||
.find_map(|window_size| {
|
||||
xmas.windows(window_size)
|
||||
.find(|window| window.iter().sum::<u64>() == key)
|
||||
})
|
||||
.ok_or("Could not find a weakness")?;
|
||||
|
||||
if let MinMaxResult::MinMax(min, max) = weak_window.iter().minmax() {
|
||||
Ok(min + max)
|
||||
} else {
|
||||
unreachable!("We must have at least two elements due to the window size")
|
||||
}
|
||||
}
|
||||
|
||||
fn find_not_summed(xmas: &Vec<u64>, window_size: usize) -> Result<u64, &str> {
|
||||
xmas.windows(window_size + 1)
|
||||
.find_map(|window| {
|
||||
if let [preamble @ .., number] = window {
|
||||
// If none of the number combinations in the preamble
|
||||
// sum to the final number, we've got the key!
|
||||
if !preamble
|
||||
.iter()
|
||||
.combinations(2)
|
||||
.any(|combination| combination.into_iter().sum::<u64>() == *number)
|
||||
{
|
||||
Some(*number)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
unreachable!("The windows must all have a length of at least 1");
|
||||
}
|
||||
})
|
||||
.ok_or("Could not find the entry")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use indoc::indoc;
|
||||
|
||||
#[test]
|
||||
fn test_simple() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = indoc!(
|
||||
"35
|
||||
20
|
||||
15
|
||||
25
|
||||
47
|
||||
40
|
||||
62
|
||||
55
|
||||
65
|
||||
95
|
||||
102
|
||||
117
|
||||
150
|
||||
182
|
||||
127
|
||||
219
|
||||
299
|
||||
277
|
||||
309
|
||||
576"
|
||||
);
|
||||
|
||||
let xmas = parse_xmas(input)?;
|
||||
let result = find_not_summed(&xmas, 5)?;
|
||||
|
||||
assert_eq!(result, 127);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple2() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = indoc!(
|
||||
"35
|
||||
20
|
||||
15
|
||||
25
|
||||
47
|
||||
40
|
||||
62
|
||||
55
|
||||
65
|
||||
95
|
||||
102
|
||||
117
|
||||
150
|
||||
182
|
||||
127
|
||||
219
|
||||
299
|
||||
277
|
||||
309
|
||||
576"
|
||||
);
|
||||
|
||||
let xmas = parse_xmas(input)?;
|
||||
let key = find_not_summed(&xmas, 5)?;
|
||||
let result = find_weakness(&xmas, key)?;
|
||||
|
||||
assert_eq!(result, 62);
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue