-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20.rs
51 lines (43 loc) · 1.31 KB
/
20.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Day 20: Grove Positioning System
* See [https://adventofcode.com/2022/day/20]
*/
use std::collections::VecDeque;
fn solve<T>(nums: T, loops: usize) -> isize where T: Iterator<Item = isize> {
let mut en = nums.enumerate().collect::<VecDeque<_>>();
let il = en.len() as isize;
for _ in 0..loops {
for a in 0..en.len() {
let pos = en.iter().position(|(b, _)| *b == a).unwrap();
en.rotate_left(pos);
let m = en.pop_front().unwrap();
let ix = m.1.rem_euclid(il - 1) as usize;
en.rotate_left(ix);
en.push_back(m);
}
}
let l = en.len();
let pos = en.iter().position(|(_, x)| *x == 0).unwrap();
[1000, 2000, 3000].iter().map(|&i| en[(pos + i) % l].1).sum()
}
fn must_parse(l: &str) -> isize {
l.parse().unwrap()
}
pub fn part_1(input: &str) -> Option<isize> {
let nums = input.lines().map(must_parse);
Some(solve(nums, 1))
}
pub fn part_2(input: &str) -> Option<isize> {
let nums = input.lines().map(must_parse).map(|x| x * 811589153);
Some(solve(nums, 10))
}
aoc2022::solve!(part_1, part_2);
#[cfg(test)]
mod tests {
use aoc2022::assert_ex;
use super::*;
#[test]
fn test_part_1() { assert_ex!(part_1, 3); }
#[test]
fn test_part_2() { assert_ex!(part_2, 1623178306); }
}