Skip to content

Commit 15fd1bc

Browse files
committed
feat(03/2015): solve first part
1 parent 8e1cb42 commit 15fd1bc

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

readme.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@
8585

8686
# 2015
8787

88-
| Day | Solved | Part 1 time (ms) | Part 2 time (ms) |
89-
|-----------------------------------------------------------------------------|:------:|-----------------:|-----------------:|
90-
| [Day 1: Not Quite Lisp](src/solutions/year2015/day01.rs) | ⭐⭐ | 0.013 | 0.001 |
91-
| [Day 2: I Was Told There Would Be No Math](src/solutions/year2015/day02.rs) | ⭐⭐ | 0.074 | 0.105 |
88+
| Day | Solved | Part 1 time (ms) | Part 2 time (ms) |
89+
|----------------------------------------------------------------------------------|:------:|-----------------:|-----------------:|
90+
| [Day 1: Not Quite Lisp](src/solutions/year2015/day01.rs) | ⭐⭐ | 0.013 | 0.001 |
91+
| [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 | - |
9293

9394
# TODO
9495

src/solutions/year2015/day03.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
use crate::solutions::Solution;
2+
use crate::utils::point::Point;
3+
use std::collections::HashMap;
24

35
pub struct Day03;
46

57
impl Solution for Day03 {
6-
fn part_one(&self, _input: &str) -> String {
7-
String::from("0")
8+
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);
13+
14+
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!(),
21+
};
22+
visited_houses
23+
.entry(current)
24+
.and_modify(|e| *e += 1)
25+
.or_insert(1);
26+
}
27+
28+
visited_houses.keys().count().to_string()
829
}
930

1031
fn part_two(&self, _input: &str) -> String {
@@ -18,6 +39,8 @@ mod tests {
1839

1940
#[test]
2041
fn part_one_example_test() {
21-
assert_eq!("0", Day03.part_one("0"));
42+
assert_eq!("2", Day03.part_one(">"));
43+
assert_eq!("4", Day03.part_one("^>v<"));
44+
assert_eq!("2", Day03.part_one("^v^v^v^v^v"));
2245
}
2346
}

0 commit comments

Comments
 (0)