Skip to content

Commit 85c699b

Browse files
committed
Day 3 part 2
1 parent 2b0be30 commit 85c699b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

day-3/src/main.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::fs;
22

33
fn main() {
44
part_1();
5+
part_2();
56
}
67

78
fn part_1() {
@@ -28,3 +29,37 @@ fn part_1() {
2829

2930
println!("Part 1 result: {} unique houses", unique_houses);
3031
}
32+
33+
fn part_2() {
34+
let input = fs::read_to_string("./day-3/input.txt").unwrap();
35+
let mut santa_history = vec![(0, 0)];
36+
let mut robot_santa_history = vec![(0, 0)];
37+
38+
for (i, c) in input.chars().enumerate() {
39+
let history = if i % 2 == 0 {
40+
&mut santa_history
41+
} else {
42+
&mut robot_santa_history
43+
};
44+
45+
let (x, y) = history.last().unwrap().clone();
46+
let (new_x, new_y) = match c {
47+
'^' => (x, y + 1),
48+
'v' => (x, y - 1),
49+
'>' => (x + 1, y),
50+
'<' => (x - 1, y),
51+
_ => (x, y),
52+
};
53+
54+
history.push((new_x, new_y));
55+
}
56+
57+
santa_history.extend(robot_santa_history);
58+
59+
let unique_houses = santa_history
60+
.iter()
61+
.collect::<std::collections::HashSet<_>>()
62+
.len();
63+
64+
println!("Part 2 result: {} unique houses", unique_houses);
65+
}

0 commit comments

Comments
 (0)