-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclimb.rs
142 lines (124 loc) · 3.11 KB
/
climb.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
use std::collections::{BinaryHeap, HashMap};
use std::io;
use std::io::BufRead as _;
#[derive(Copy, Clone, Eq, Hash, PartialEq, PartialOrd, Ord)]
struct Coords {
x: isize,
y: isize,
}
struct Map {
width: isize,
height: isize,
data: HashMap<Coords, u8>,
}
fn read_input() -> (Coords, Coords, Map, Vec<Coords>) {
let mut w: isize = 0;
let mut h: isize = 0;
let mut data = HashMap::<Coords, u8>::new();
let mut start: Option<Coords> = None;
let mut end: Option<Coords> = None;
let mut zeroes = vec![];
for line in io::BufReader::new(io::stdin()).lines() {
let line = line.unwrap();
let trimmed = line.trim();
if w != 0 {
if (trimmed.len() as isize) != w {
panic!("Error: line {} is {} characters long, expected {}", (h+1), trimmed.len(), w);
}
} else {
w = (trimmed.len() as isize)
}
for (i, b) in trimmed.bytes().enumerate() {
let position = Coords{ x: i as isize, y: h };
let tile = match b {
83 => {
start = Some(position);
0
},
69 => {
end = Some(position);
25
},
_ => (b - 97)
};
if tile == 0 {
zeroes.push(position);
}
data.insert(position, tile);
}
h += 1;
}
if start.is_none() {
panic!("Error: No starting point found in input");
}
if end.is_none() {
panic!("Error: No ending point found in input");
}
let map = Map {
data: data,
width: w,
height: h,
};
return (start.unwrap(), end.unwrap(), map, zeroes);
}
fn find_path(map: &Map, start: Coords, end: Coords) -> Option<usize> {
use std::cmp::Reverse;
#[derive(Eq, Ord, PartialEq, PartialOrd)]
struct Candidate {
cost: usize,
coords: Coords,
parent: Coords,
}
let mut queue = BinaryHeap::<Reverse::<Candidate>>::new();
queue.push(Reverse(Candidate {
cost: 0,
coords: start,
parent: start,
}));
let mut parent = HashMap::<Coords, Coords>::new();
loop {
let current = match queue.pop() {
Some(value) => value.0,
None => return None,
};
if current.coords == end {
return Some(current.cost);
}
if parent.contains_key(¤t.coords) {
continue
}
parent.insert(current.coords, current.parent);
let current_value = *(map.data.get(¤t.coords).unwrap()) as usize;
let possibilities = vec![
Coords { x: current.coords.x + 1, y: current.coords.y },
Coords { x: current.coords.x, y: current.coords.y + 1 },
Coords { x: current.coords.x - 1, y: current.coords.y },
Coords { x: current.coords.x, y: current.coords.y - 1 },
];
for next in possibilities {
if next.x < 0 || next.x >= map.width || next.y < 0 || next.y >= map.height {
continue
}
let next_value = *(map.data.get(&next).unwrap()) as usize;
if next_value > (current_value + 1) {
continue
}
queue.push(Reverse(Candidate {
cost: current.cost + 1,
coords: next,
parent: current.coords
}));
}
}
}
fn main() {
let (start, end, map, zeroes) = read_input();
println!("Part 1: {}", find_path(&map, start, end).unwrap());
println!("Part 2: {}", zeroes.iter()
.map(|zero| { find_path(&map, *zero, end) })
.filter(|i| { i.is_some() })
.map(|i| { i.unwrap() })
.reduce(|a, i| { if i < a { i } else { a }})
.unwrap()
);
}