Skip to content

Commit 3c475f5

Browse files
committed
feat(03/2015): solve second part
1 parent 15fd1bc commit 3c475f5

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
|----------------------------------------------------------------------------------|:------:|-----------------:|-----------------:|
9090
| [Day 1: Not Quite Lisp](src/solutions/year2015/day01.rs) | ⭐⭐ | 0.013 | 0.001 |
9191
| [Day 2: I Was Told There Would Be No Math](src/solutions/year2015/day02.rs) | ⭐⭐ | 0.074 | 0.105 |
92-
| [Day 3: Perfectly Spherical Houses in a Vacuum](src/solutions/year2015/day03.rs) | | 0.358 | - |
92+
| [Day 3: Perfectly Spherical Houses in a Vacuum](src/solutions/year2015/day03.rs) | | 0.358 | 0.373 |
9393

9494
# TODO
9595

src/solutions/year2015/day03.rs

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,64 @@ use crate::solutions::Solution;
22
use crate::utils::point::Point;
33
use std::collections::HashMap;
44

5+
type Houses = HashMap<Point, u64>;
6+
57
pub struct Day03;
68

79
impl Solution for Day03 {
810
fn part_one(&self, input: &str) -> String {
9-
let mut visited_houses: HashMap<Point, u64> = HashMap::new();
10-
let mut current = Point::new(0, 0);
11-
12-
visited_houses.insert(current, 1);
11+
let mut visited_houses: Houses = HashMap::new();
12+
let mut santa = Point::new(0, 0);
13+
visited_houses.insert(santa, 1);
1314

1415
for b in input.bytes() {
15-
current = match b {
16-
b'>' => current.east(),
17-
b'<' => current.west(),
18-
b'^' => current.north(),
19-
b'v' => current.south(),
20-
_ => unreachable!(),
16+
santa = self.make_move(santa, b);
17+
18+
self.visit_house(&mut visited_houses, santa);
19+
}
20+
21+
visited_houses.len().to_string()
22+
}
23+
24+
fn part_two(&self, input: &str) -> String {
25+
let mut visited_houses: Houses = HashMap::new();
26+
let mut santa = Point::new(0, 0);
27+
visited_houses.insert(santa, 1);
28+
29+
let mut robo_santa = Point::new(0, 0);
30+
visited_houses.insert(robo_santa, 1);
31+
32+
for (i, b) in input.bytes().enumerate() {
33+
let visited = if i % 2 == 0 {
34+
santa = self.make_move(santa, b);
35+
36+
santa
37+
} else {
38+
robo_santa = self.make_move(robo_santa, b);
39+
40+
robo_santa
2141
};
22-
visited_houses
23-
.entry(current)
24-
.and_modify(|e| *e += 1)
25-
.or_insert(1);
42+
43+
self.visit_house(&mut visited_houses, visited);
2644
}
2745

28-
visited_houses.keys().count().to_string()
46+
visited_houses.len().to_string()
2947
}
48+
}
3049

31-
fn part_two(&self, _input: &str) -> String {
32-
String::from("0")
50+
impl Day03 {
51+
fn make_move(&self, point: Point, b: u8) -> Point {
52+
match b {
53+
b'>' => point.east(),
54+
b'<' => point.west(),
55+
b'^' => point.north(),
56+
b'v' => point.south(),
57+
_ => unreachable!(),
58+
}
59+
}
60+
61+
fn visit_house(&self, houses: &mut Houses, house: Point) {
62+
houses.entry(house).and_modify(|e| *e += 1).or_insert(1);
3363
}
3464
}
3565

@@ -43,4 +73,11 @@ mod tests {
4373
assert_eq!("4", Day03.part_one("^>v<"));
4474
assert_eq!("2", Day03.part_one("^v^v^v^v^v"));
4575
}
76+
77+
#[test]
78+
fn part_two_example_test() {
79+
assert_eq!("3", Day03.part_two("^v"));
80+
assert_eq!("3", Day03.part_two("^>v<"));
81+
assert_eq!("11", Day03.part_two("^v^v^v^v^v"));
82+
}
4683
}

0 commit comments

Comments
 (0)