/ Rust / 2015 / 03.rs
03.rs
 1  #![feature(test)]
 2  
 3  use aoc::grid::Direction;
 4  use rustc_hash::FxHashSet;
 5  
 6  type Input = Vec<Direction>;
 7  
 8  fn setup(input: &str) -> Input {
 9      input.trim().chars().map(|b| b.into()).collect()
10  }
11  
12  fn part1(input: &Input) -> usize {
13      input
14          .iter()
15          .scan((0, 0), |acc, &x| {
16              *acc = x.step_signed(*acc);
17              Some(*acc)
18          })
19          .chain([(0, 0)])
20          .collect::<FxHashSet<_>>()
21          .len()
22  }
23  
24  fn part2(input: &Input) -> usize {
25      input
26          .iter()
27          .scan(((0, 0), (0, 0)), |(a, b), &x| {
28              std::mem::swap(a, b);
29              *a = x.step_signed(*a);
30              Some(*a)
31          })
32          .chain([(0, 0)])
33          .collect::<FxHashSet<_>>()
34          .len()
35  }
36  
37  aoc::main!(2015, 3, ex: 1);