From 82a2bfd9d6e1fce06337bc38a9d460be26efe01a Mon Sep 17 00:00:00 2001 From: foosinn Date: Sun, 14 Jun 2020 11:24:43 +0200 Subject: [PATCH] day16 --- Cargo.toml | 4 ++ input16 | 1 + src/day16/lost.rs | 32 ++++++++++++ src/day16/main.rs | 122 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 input16 create mode 100644 src/day16/lost.rs create mode 100644 src/day16/main.rs diff --git a/Cargo.toml b/Cargo.toml index 32204e2..a6a9d81 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,3 +58,7 @@ path = "src/day14/main.rs" [[bin]] name = "day15" path = "src/day15/main.rs" + +[[bin]] +name = "day16" +path = "src/day16/main.rs" diff --git a/input16 b/input16 new file mode 100644 index 0000000..1c2f915 --- /dev/null +++ b/input16 @@ -0,0 +1 @@ +59713137269801099632654181286233935219811755500455380934770765569131734596763695509279561685788856471420060118738307712184666979727705799202164390635688439701763288535574113283975613430058332890215685102656193056939765590473237031584326028162831872694742473094498692690926378560215065112055277042957192884484736885085776095601258138827407479864966595805684283736114104361200511149403415264005242802552220930514486188661282691447267079869746222193563352374541269431531666903127492467446100184447658357579189070698707540721959527692466414290626633017164810627099243281653139996025661993610763947987942741831185002756364249992028050315704531567916821944 \ No newline at end of file diff --git a/src/day16/lost.rs b/src/day16/lost.rs new file mode 100644 index 0000000..6d26924 --- /dev/null +++ b/src/day16/lost.rs @@ -0,0 +1,32 @@ +// baesed on the idear of an recurring pattern +// not required for the day 16 task + +struct EnumeratedCycle { + pattern: Vec, + position: usize, + iteration: usize, +} + +impl EnumeratedCycle { + fn new(pattern: Vec, start: usize) -> Self { + Self { + pattern, + position: 0, + iteration: start, + } + } +} + +impl Iterator for EnumeratedCycle { + type Item = (usize, i64); + fn next(&mut self) -> Option { + let item = self.pattern.get(self.position)?; + let iteration = self.iteration; + self.position += 1; + if self.position == self.pattern.len() { + self.position = 0; + self.iteration += 1; + } + Some((iteration, *item)) + } +} diff --git a/src/day16/main.rs b/src/day16/main.rs new file mode 100644 index 0000000..6cc3ffd --- /dev/null +++ b/src/day16/main.rs @@ -0,0 +1,122 @@ +use std::fs::File; +use std::io::prelude::*; +use std::iter; + +fn main() { + /* + let pattern = EnumeratedCycle::new(vec![0, 1, 0, -1], 1); + let pattern = pattern + .map(|(iteration, item)| iter::repeat(item).take(iteration)) + .flatten(); + */ + + // part 1 + let f = File::open("input16").expect("unable to load \"input16\""); + let transmission: Vec<_> = f.bytes().map(|b| (b.unwrap() - b'0') as i64).collect(); + print!("output: "); + phase_n(transmission, 100) + .iter() + .take(8) + .for_each(|i| print!("{}", i)); + println!(); +} + +fn phase_n(input: Vec, phases: usize) -> Vec { + let mut output = input; + for _ in 0..phases { + output = phase(output) + } + output +} + +fn phase(input: Vec) -> Vec { + let mut output: Vec = Vec::with_capacity(input.len()); + for (pos, _) in input.iter().enumerate() { + let mut pattern = repeated_cycle(vec![0, 1, 0, -1], pos + 1); + pattern.next(); + let mut value = + input + .iter() + .zip(pattern) + .map(|(a, b)| (a * b) % 10) + .fold(0, |i, mut acc| { + acc += i; + acc + }); + if value < 0 { + value *= -1 + } + output.push(value % 10); + } + output +} + +fn repeated_cycle(input: Vec, repeats: usize) -> impl Iterator { + input + .clone() + .into_iter() + .map(move |item| iter::repeat(item).take(repeats)) + .flatten() + .cycle() +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn example1() { + assert_eq!( + phase(vec![1, 2, 3, 4, 5, 6, 7, 8]), + vec![4, 8, 2, 2, 6, 1, 5, 8] + ); + assert_eq!( + phase(vec![4, 8, 2, 2, 6, 1, 5, 8]), + vec![3, 4, 0, 4, 0, 4, 3, 8] + ); + assert_eq!( + phase(vec![3, 4, 0, 4, 0, 4, 3, 8]), + vec![0, 3, 4, 1, 5, 5, 1, 8] + ); + assert_eq!( + phase(vec![0, 3, 4, 1, 5, 5, 1, 8]), + vec![0, 1, 0, 2, 9, 4, 9, 8] + ); + } + + #[test] + fn example2() { + let data: Vec<_> = "80871224585914546619083218645595" + .bytes() + .map(|i| (i - b'0') as i64) + .collect(); + assert!(phase_n(data, 100) + .into_iter() + .take(8) + .eq(vec![2, 4, 1, 7, 6, 1, 7, 6])); + } + + #[test] + fn example3() { + let data: Vec<_> = "19617804207202209144916044189917" + .bytes() + .map(|i| (i - b'0') as i64) + .collect(); + assert!(phase_n(data, 100) + .into_iter() + .take(8) + .eq(vec![7, 3, 7, 4, 5, 4, 1, 8])); + } + + #[test] + fn example4() { + let data: Vec<_> = "69317163492948606335995924319873" + .bytes() + .map(|i| (i - b'0') as i64) + .collect(); + assert!(phase_n(data, 100) + .into_iter() + .take(8) + .eq(vec![5, 2, 4, 3, 2, 1, 3, 3])); + } +}