Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee50571

Browse files
committed
Day 03, Part 2
1 parent ff6e8d8 commit ee50571

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

day_03/main.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,32 @@ struct Point {
1111
fn main() {
1212
let stdin = io::stdin();
1313
for line in stdin.lock().lines() {
14-
let mut position = Point {x: 0, y: 0};
14+
let mut santa_position = Point {x: 0, y: 0};
15+
let mut robot_position = Point {x: 0, y: 0};
1516
let mut visits = HashMap::new();
1617

17-
*visits.entry(position).or_insert(0) += 1;
18+
*visits.entry(santa_position).or_insert(0) += 1;
1819

19-
for c in line.unwrap().chars() {
20-
match c {
21-
'>' => position.x += 1,
22-
'v' => position.y -= 1,
23-
'<' => position.x -= 1,
24-
'^' => position.y += 1,
25-
_ => {}
26-
};
27-
*visits.entry(position).or_insert(0) += 1
20+
for (i, c) in line.unwrap().chars().enumerate() {
21+
if i%2 == 0 {
22+
match c {
23+
'>' => santa_position.x += 1,
24+
'v' => santa_position.y -= 1,
25+
'<' => santa_position.x -= 1,
26+
'^' => santa_position.y += 1,
27+
_ => {}
28+
};
29+
*visits.entry(santa_position).or_insert(0) += 1
30+
} else {
31+
match c {
32+
'>' => robot_position.x += 1,
33+
'v' => robot_position.y -= 1,
34+
'<' => robot_position.x -= 1,
35+
'^' => robot_position.y += 1,
36+
_ => {}
37+
};
38+
*visits.entry(robot_position).or_insert(0) += 1
39+
}
2840
}
2941

3042
println!("{:?}", visits.len());

0 commit comments

Comments
 (0)