-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_2019_03.rs
143 lines (108 loc) · 3.72 KB
/
solution_2019_03.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#![feature(map_try_insert)]
use std::collections::{HashMap, HashSet};
use advent_of_code_common::coords2d::Coords2D;
use crate::Command::{Down, Left, Right, Up};
type Number = i32;
type Coords = Coords2D<Number>;
#[derive(Debug)]
enum Command {
Left(Number),
Right(Number),
Up(Number),
Down(Number),
}
fn parse_commands(line: &str) -> Vec<Command> {
line.split(',')
.map(|element| {
let n = element[1 .. element.len()].parse::<Number>().unwrap();
match element.chars().next().unwrap() {
'U' => Up(n),
'D' => Down(n),
'L' => Left(n),
'R' => Right(n),
x => panic!("Unknown command {x} in {line}"),
}
})
.collect()
}
fn visited_with_steps(commands: &[Command]) -> HashMap<Coords, Number> {
let mut current = Coords::origin();
let mut result: HashMap<Coords, Number> = HashMap::new();
let mut steps: Number = 0;
for command in commands {
match command {
Left(n) => {
((current.x - n) .. current.x).rev().for_each(|x| {
steps += 1;
let _ = result.try_insert(Coords { x, y: current.y }, steps);
});
current.x -= n;
},
Right(n) => {
((current.x + 1) ..= (current.x + n)).for_each(|x| {
steps += 1;
let _ = result.try_insert(Coords { x, y: current.y }, steps);
});
current.x += n;
},
Up(n) => {
((current.y + 1) ..= (current.y + n)).for_each(|y| {
steps += 1;
let _ = result.try_insert(Coords { x: current.x, y }, steps);
});
current.y += n;
},
Down(n) => {
((current.y - n) .. current.y).rev().for_each(|y| {
steps += 1;
let _ = result.try_insert(Coords { x: current.x, y }, steps);
});
current.y -= n;
},
}
}
result
}
type CostF = fn(&Coords, &HashMap<Coords, Number>, &HashMap<Coords, Number>) -> Number;
fn solve(data: &str, f: CostF) -> Number {
let lines: Vec<&str> = data.lines().filter(|x| !x.is_empty()).collect();
assert_eq!(lines.len(), 2);
let commands1 = parse_commands(lines[0]);
let commands2 = parse_commands(lines[1]);
let visited1 = visited_with_steps(&commands1);
let visited2 = visited_with_steps(&commands2);
let visited_coords1 = visited1.keys().copied().collect::<HashSet<Coords>>();
let visited_coords2 = visited2.keys().copied().collect::<HashSet<Coords>>();
visited_coords1
.intersection(&visited_coords2)
.map(|c| f(c, &visited1, &visited2))
.min()
.unwrap()
}
fn solve1(data: &str) -> Number {
solve(data, |c, _a, _b| c.manhattan_distance(Coords2D::origin()))
}
fn solve2(data: &str) -> Number {
solve(data, |c, a, b| a[c] + b[c])
}
fn main() {
let test1 = "R8,U5,L5,D3
U7,R6,D4,L4";
let test2 = "R75,D30,R83,U83,L12,D49,R71,U7,L72
U62,R66,U55,R34,D71,R55,D58,R83";
let test3 = "R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51
U98,R91,D20,R16,D67,R40,U7,R15,U6,R7";
assert_eq!(solve1(test1), 6);
assert_eq!(solve1(test2), 159);
assert_eq!(solve1(test3), 135);
let data = include_str!("../../resources/03.txt");
let part1 = solve1(data);
assert_eq!(part1, 258);
println!("Part 1: {part1}");
assert_eq!(solve2(test1), 30);
assert_eq!(solve2(test2), 610);
assert_eq!(solve2(test3), 410);
let part2 = solve2(data);
assert_eq!(part2, 12304);
println!("Part 2: {part2}");
}