Compare commits

...
Sign in to create a new pull request.

1 commit

Author SHA1 Message Date
82a2bfd9d6 day16 2020-06-14 11:24:43 +02:00
4 changed files with 159 additions and 0 deletions

View file

@ -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"

1
input16 Normal file
View file

@ -0,0 +1 @@
59713137269801099632654181286233935219811755500455380934770765569131734596763695509279561685788856471420060118738307712184666979727705799202164390635688439701763288535574113283975613430058332890215685102656193056939765590473237031584326028162831872694742473094498692690926378560215065112055277042957192884484736885085776095601258138827407479864966595805684283736114104361200511149403415264005242802552220930514486188661282691447267079869746222193563352374541269431531666903127492467446100184447658357579189070698707540721959527692466414290626633017164810627099243281653139996025661993610763947987942741831185002756364249992028050315704531567916821944

32
src/day16/lost.rs Normal file
View file

@ -0,0 +1,32 @@
// baesed on the idear of an recurring pattern
// not required for the day 16 task
struct EnumeratedCycle {
pattern: Vec<i64>,
position: usize,
iteration: usize,
}
impl EnumeratedCycle {
fn new(pattern: Vec<i64>, start: usize) -> Self {
Self {
pattern,
position: 0,
iteration: start,
}
}
}
impl Iterator for EnumeratedCycle {
type Item = (usize, i64);
fn next(&mut self) -> Option<Self::Item> {
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))
}
}

122
src/day16/main.rs Normal file
View file

@ -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<i64>, phases: usize) -> Vec<i64> {
let mut output = input;
for _ in 0..phases {
output = phase(output)
}
output
}
fn phase(input: Vec<i64>) -> Vec<i64> {
let mut output: Vec<i64> = 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<i64>, repeats: usize) -> impl Iterator<Item = i64> {
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]));
}
}