Progress to day 11
This commit is contained in:
23
9-encoding_error/Cargo.lock
generated
Normal file
23
9-encoding_error/Cargo.lock
generated
Normal file
@@ -0,0 +1,23 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||
|
||||
[[package]]
|
||||
name = "encoding_error"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37d572918e350e82412fe766d24b15e6682fb2ed2bbe018280caa810397cb319"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
@@ -7,3 +7,4 @@ edition = "2018"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.10.0"
|
||||
|
||||
@@ -1,3 +1,90 @@
|
||||
use itertools::Itertools;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
let input = include_str!("../data/input");
|
||||
let input_slice = parse(input);
|
||||
let step1 = brk_enc_step1(&input_slice, 25).unwrap();
|
||||
println!("Step 1: {}", step1);
|
||||
let step2 = brk_enc_step2(&input_slice, *step1).unwrap();
|
||||
println!("Step 2: {}", step2);
|
||||
}
|
||||
|
||||
fn brk_enc_step1<'a, U>(input: &'a[U], window_size: usize) -> Option<&'a U> where
|
||||
U: PartialEq,
|
||||
&'a U: std::ops::Add<Output = U> {
|
||||
input
|
||||
.windows(window_size + 1)
|
||||
.find(
|
||||
|&w| {
|
||||
let last = w.last().unwrap();
|
||||
let firsts = w[..(w.len()-1)].iter().combinations(2);
|
||||
!firsts.map(|f | {
|
||||
*f.first().unwrap() + *f.last().unwrap() == *last
|
||||
}).any(|t|t)
|
||||
}
|
||||
)?.last()
|
||||
}
|
||||
|
||||
fn brk_enc_step2<'a, U>(input: &'a[U], s1key: U) -> Option<U> where
|
||||
U: PartialEq + std::cmp::Ord + std::iter::Sum<&'a U>,
|
||||
&'a U: std::ops::Add<Output = U> {
|
||||
(2..input.len())
|
||||
.find_map(|x| {
|
||||
input
|
||||
.windows(x)
|
||||
.find_map(|w| {
|
||||
if w.iter().sum::<U>() == s1key {
|
||||
Some(w.iter().min().unwrap() + w.iter().max().unwrap())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
})
|
||||
// (input)
|
||||
// .find(|w| {
|
||||
// w.sum() == s1key
|
||||
// })
|
||||
}
|
||||
|
||||
fn parse(input: &'static str) -> Vec<i64> {
|
||||
input
|
||||
.lines()
|
||||
.map(|l| l.parse::<i64>().unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
static INPUT: &str = r"35
|
||||
20
|
||||
15
|
||||
25
|
||||
47
|
||||
40
|
||||
62
|
||||
55
|
||||
65
|
||||
95
|
||||
102
|
||||
117
|
||||
150
|
||||
182
|
||||
127
|
||||
219
|
||||
299
|
||||
277
|
||||
309
|
||||
576";
|
||||
|
||||
#[test]
|
||||
fn test_step1() {
|
||||
assert_eq!(brk_enc_step1(&parse(INPUT), 5), Some(&127));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_step2() {
|
||||
let a = brk_enc_step2(&parse(INPUT), 127i64).unwrap();
|
||||
assert_eq!(a, 62);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user