-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
111 lines (96 loc) · 2.68 KB
/
main.rs
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
use std::cmp;
use std::collections::HashMap;
use std::collections::HashSet;
fn manhattan_distance(point: (isize, isize)) -> isize {
point.0.abs() + point.1.abs()
}
fn parse_intructions(s: &str) -> Vec<(isize, isize, isize)> {
s.split(',')
.map(|inst| {
let n = inst[1..].parse::<isize>().unwrap();
match &inst[0..1] {
"U" => (n, 1, 0),
"D" => (n, -1, 0),
"L" => (n, 0, -1),
"R" => (n, 0, 1),
_ => panic!(),
}
})
.collect::<Vec<_>>()
}
fn part1(input: &str) -> isize {
let paths = input.split('\n').map(parse_intructions).collect::<Vec<_>>();
let mut map: HashSet<(isize, isize)> = HashSet::new();
let mut x: isize = 0;
let mut y: isize = 0;
for inst in &paths[0] {
let (n, dx, dy) = inst;
for _ in 0..*n {
x += dx;
y += dy;
map.insert((x, y));
}
}
let mut result: isize = isize::MAX;
let mut x: isize = 0;
let mut y: isize = 0;
for inst in &paths[1] {
let (n, dx, dy) = inst;
for _ in 0..*n {
x += dx;
y += dy;
if map.contains(&(x, y)) {
result = cmp::min(manhattan_distance((x, y)), result);
}
}
}
result
}
fn part2(input: &str) -> isize {
let paths = input.split('\n').map(parse_intructions).collect::<Vec<_>>();
let mut map: HashMap<(isize, isize), isize> = HashMap::new();
let mut x: isize = 0;
let mut y: isize = 0;
let mut steps: isize = 0;
for inst in &paths[0] {
let (n, dx, dy) = inst;
for _ in 0..*n {
x += dx;
y += dy;
steps += 1;
if !map.contains_key(&(x, y)) {
map.insert((x, y), steps);
}
}
}
let mut result: isize = isize::MAX;
let mut x: isize = 0;
let mut y: isize = 0;
let mut steps: isize = 0;
for inst in &paths[1] {
let (n, dx, dy) = inst;
for _ in 0..*n {
x += dx;
y += dy;
steps += 1;
if let Some(other_steps) = map.get(&(x, y)) {
result = cmp::min(steps + other_steps, result);
}
}
}
result
}
fn main() {
println!("Part 1: {}", part1(include_str!("in.txt")));
println!("Part 2: {}", part2(include_str!("in.txt")));
}
#[test]
fn test_part1() {
assert_eq!(part1(include_str!("test1.txt")), 159);
assert_eq!(part1(include_str!("test2.txt")), 135);
}
#[test]
fn test_part2() {
assert_eq!(part2(include_str!("test1.txt")), 610);
assert_eq!(part2(include_str!("test2.txt")), 410);
}