Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@0e4ef622
Last active December 16, 2019 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0e4ef622/0f5c4ccdcf81d9e64d912d3edfe58d92 to your computer and use it in GitHub Desktop.
Save 0e4ef622/0f5c4ccdcf81d9e64d912d3edfe58d92 to your computer and use it in GitHub Desktop.
aoc 2019 day 16 part 2
pub fn part2(input: &str) -> i64 {
// parse input
let onums = input
.trim()
.bytes()
.map(|x| (x - b'0') as i64)
.collect::<Vec<_>>();
let offset = input[..7].parse::<usize>().unwrap();
// calculate new input
let mut nums = std::iter::repeat(&onums)
.take(10000)
.flatten()
.copied()
.skip(offset as usize)
.collect::<Vec<_>>();
// 100 phases
for n in 0..100 {
// reverse prefix sum
for i in (0..nums.len()-1).rev() {
nums[i] += nums[i+1];
}
nums.iter_mut().for_each(|x| *x %= 10);
}
// final answer
nums[..8].iter().fold(0i64, |a, &n| a*10 + n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment