summaryrefslogtreecommitdiffstats
path: root/2015/rs/code/three/src/main.rs
blob: 4e7d083566d48eeeb503338c56e4c24c564a6669 (plain) (blame)
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
use std::collections::HashSet;

static INPUT: &str = include_str!("input.txt");

fn main() {
  let valueified = INPUT.chars().map(|c| match c { '<' => (-1, 0), '>' => (1, 0), '^' => (0, 1), 'v' => (0, -1), _ => (0, 0) });

  let mut visited = HashSet::new();
  append_visits(valueified.clone(), &mut visited);
  println!("Number visited normally: {}", visited.len());

  let santa_iter = valueified.clone().step_by(2);
  let robot_iter = valueified.skip(1).step_by(2);

  let mut visited = HashSet::new();
  append_visits(santa_iter.clone(), &mut visited);
  append_visits(robot_iter.clone(), &mut visited);
  println!("Number visited w/ robot: {}", visited.len());
}

fn append_visits<I: Iterator<Item = (i32, i32)>>(iter: I, visited: &mut HashSet<(i32, i32)>) {
  let (mut x, mut y) = (0, 0);
  visited.insert((0, 0));
  for (move_x, move_y) in iter {
    x += move_x;
    y += move_y;
    visited.insert((x, y));
  }
}