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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::collections::HashMap;
use std::fmt;
use std::ops::Add;
use std::str::Chars;

pub fn solve(input: &str) -> i32 {
  let mut santa = String::new();
  let mut robo_santa = String::new();
  let mut iter = input.chars();

  loop {
    match iter.next() {
      Some(c) => santa.push(c),
      None => break,
    }
    match iter.next() {
      Some(c) => robo_santa.push(c),
      None => break,
    }
  }

  let mut houses: HashMap<Point, u32> = HashMap::new();
  Santa::houses_visited(&santa, &mut houses);
  Santa::houses_visited(&robo_santa, &mut houses);
  houses.len() as i32
}

struct Santa<'a> {
  location: Point,
  directions: Chars<'a>,
}

impl<'a> Santa<'a> {
  fn new(directions: &str) -> Santa {
    Santa {
      location: Point::origin(),
      directions: directions.chars(),
    }
  }

  fn houses_visited(directions: &str, houses: &mut HashMap<Point, u32>) {
    let santa = Santa::new(directions);
    houses.insert(santa.location, 1);

    for point in santa {
      *houses.entry(point).or_insert(0) += 1;
    }
  }
}

impl<'a> Iterator for Santa<'a> {
  type Item = Point;

  fn next(&mut self) -> Option<Point> {
    let offset = match self.directions.next() {
      Some('^') => Point { x: 0, y: 1 },
      Some('v') => Point { x: 0, y: -1 },
      Some('>') => Point { x: 1, y: 0 },
      Some('<') => Point { x: -1, y: 0 },
      _ => return None,
    };
    self.location = self.location + offset;
    Some(self.location)
  }
}

#[test]
fn test_one() {
  let directions = ">";
  let santa = Santa::new(directions);

  let points = santa.collect::<Vec<Point>>();
  let expected = vec![Point { x: 1, y: 0 }];
  assert_eq!(expected, points);

  let mut houses: HashMap<Point, u32> = HashMap::new();
  Santa::houses_visited(directions, &mut houses);
  assert_eq!(2, houses.len());
  assert_eq!(&1, houses.get(&Point::origin()).unwrap());
  assert_eq!(&1, houses.get(&Point { x: 1, y: 0 }).unwrap());
}

#[test]
fn test_two() {
  let directions = "^>v<";

  let mut houses: HashMap<Point, u32> = HashMap::new();
  Santa::houses_visited(directions, &mut houses);
  assert_eq!(4, houses.len());
  assert_eq!(&2, houses.get(&Point::origin()).unwrap());
  assert_eq!(&1, houses.get(&Point { x: 1, y: 0 }).unwrap());
}

#[test]
fn test_three() {
  let directions = "^v^v^v^v^v";

  let mut houses: HashMap<Point, u32> = HashMap::new();
  Santa::houses_visited(directions, &mut houses);
  assert_eq!(2, houses.len());
  assert_eq!(&6, houses.get(&Point::origin()).unwrap());
  assert_eq!(&5, houses.get(&Point { x: 0, y: 1 }).unwrap());
}

#[derive(Clone,Copy,Debug,Eq,Hash,PartialEq)]
struct Point {
  x: i32,
  y: i32,
}

impl Point {
  fn origin() -> Point {
    Point { x: 0, y: 0 }
  }
}

impl Add for Point {
  type Output = Point;

  fn add(self, other: Point) -> Point {
    Point {
      x: self.x + other.x,
      y: self.y + other.y,
    }
  }
}

impl fmt::Display for Point {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "({}, {})", self.x, self.y)
  }
}

#[test]
fn test_point() {
  let point = Point { x: 10, y: -10 };
  let offset = Point { x: -10, y: 10 };
  assert_eq!(Point::origin(), point + offset);
}