Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn solve(input: &str, steps: u64) -> u64 {
- fn split(s: u64) -> Vec<u64> {
- if s == 0 {
- vec![1]
- } else {
- let digits = s.checked_ilog10().unwrap_or(0) + 1;
- if digits % 2 == 0 {
- let op = 10_u64.pow(digits / 2);
- vec![s / op, s % op]
- } else {
- vec![s * 2024]
- }
- }
- }
- (0..steps)
- .fold(
- input
- .split(" ")
- .filter_map(|s| s.parse::<u64>().ok())
- .map(|s| (s, 1))
- .collect(),
- |stones: Vec<(u64, u64)>, _| {
- let mut sorted_stones = stones
- .into_iter()
- .flat_map(|(s, c)| split(s).into_iter().map(move |n| (n, c)))
- .collect::<Vec<(u64, u64)>>();
- sorted_stones.sort_by(|(a, _), (b, _)| a.cmp(b));
- sorted_stones
- .into_iter()
- .chunk_by(|(s, _)| *s)
- .into_iter()
- .map(|(s, g)| (s, g.map(|(_, c)| c).sum()))
- .collect()
- },
- )
- .iter()
- .map(|(_, c)| c)
- .sum::<u64>()
- }
- pub fn part_one(input: &str) -> Option<u64> {
- Some(solve(input, 25))
- }
- pub fn part_two(input: &str) -> Option<u64> {
- Some(solve(input, 75))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement