Last active
December 16, 2019 20:55
-
-
Save 0e4ef622/0f5c4ccdcf81d9e64d912d3edfe58d92 to your computer and use it in GitHub Desktop.
aoc 2019 day 16 part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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