-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22.rs
161 lines (141 loc) · 4.83 KB
/
day22.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::fs;
use regex::Regex;
use crate::utils::toolbox::parse_usize;
pub(crate) fn day22() {
println!("{}", part_a(fs::read_to_string("input/2016/day22/input.txt").unwrap()));
println!("{}", part_b(fs::read_to_string("input/2016/day22/input.txt").unwrap()));
}
fn part_a(input: String) -> usize {
let mut devices: Vec<(usize, usize, usize, usize, usize)> = vec!();
input.lines().for_each(|line| {
if line.starts_with("root") || line.starts_with("Filesystem") { return; }
let re = Regex::new("^/dev/grid/node\\-x([0-9]+)\\-y([0-9]+)\\s+([0-9]+)T\\s+([0-9]+)T\\s+([0-9]+)T\\s+[0-9]+%$").unwrap();
let caps = re.captures(line).unwrap();
devices.push((
parse_usize(caps.get(1)), // x
parse_usize(caps.get(2)), // y
parse_usize(caps.get(3)), // size
parse_usize(caps.get(4)), // used
parse_usize(caps.get(5)), // avail
))
});
let mut ans = 0;
for i in 0..devices.len() {
let a = devices[i];
if a.3 == 0 { continue; }
for j in 0..devices.len() {
if i == j { continue; }
let b = devices[j];
if b.4 >= a.3 { ans += 1 }
}
}
ans
}
fn part_b(input: String) -> usize {
let mut devices: HashMap<(usize, usize), (usize, usize, usize)> = HashMap::new();
let mut x_max = 0;
let mut y_max = 0;
input.lines().for_each(|line| {
if line.starts_with("root") || line.starts_with("Filesystem") { return; }
let re = Regex::new("^/dev/grid/node\\-x([0-9]+)\\-y([0-9]+)\\s+([0-9]+)T\\s+([0-9]+)T\\s+([0-9]+)T\\s+[0-9]+%$").unwrap();
let caps = re.captures(line).unwrap();
let x = parse_usize(caps.get(1));
if x > x_max { x_max = x }
let y = parse_usize(caps.get(2));
if y > x_max { y_max = y }
devices.insert((x, y), (
parse_usize(caps.get(3)), // size
parse_usize(caps.get(4)), // used
parse_usize(caps.get(5)), // avail
));
});
// Find used=0 (empty device)
let mut empty = (0, 0);
'outer: for x in 0..=x_max {
for y in 0..=y_max {
let (_, used, _) = devices.get(&(x, y)).unwrap();
if *used == 0 {
empty = (x, y);
break 'outer
}
}
}
if empty == (0, 0) {
panic!("did not find empty device")
}
// Find shortest path from empty to (x_max - 1, 0) - field before G
let mut stack = BinaryHeap::new();
let mut visited = HashSet::new();
stack.push(Pos { x: empty.0, y: empty.1, dist: 0 });
let mut dist_from_empty = 0;
while let Some(pos) = stack.pop() {
if !visited.insert((pos.x, pos.y)) { continue }
if pos.x == x_max - 1 && pos.y == 0 {
dist_from_empty = pos.dist;
break
}
let (a_size, _, _) = devices.get(&(pos.x, pos.y)).unwrap();
// Left
if pos.x > 0 {
let (_, b_used, _) = devices.get(&(pos.x - 1, pos.y)).unwrap();
if b_used <= a_size {
stack.push(Pos { x: pos.x - 1, y: pos.y, dist: pos.dist + 1 })
}
}
// Right
if pos.x < x_max {
let (_, b_used, _) = devices.get(&(pos.x + 1, pos.y)).unwrap();
if b_used <= a_size {
stack.push(Pos { x: pos.x + 1, y: pos.y, dist: pos.dist + 1 })
}
}
// Up
if pos.y > 0 {
let (_, b_used, _) = devices.get(&(pos.x, pos.y - 1)).unwrap();
if b_used <= a_size {
stack.push(Pos { x: pos.x, y: pos.y - 1, dist: pos.dist + 1 })
}
}
// Down
if pos.y < y_max {
let (_, b_used, _) = devices.get(&(pos.x, pos.y + 1)).unwrap();
if b_used <= a_size {
stack.push(Pos { x: pos.x, y: pos.y + 1, dist: pos.dist + 1 })
}
}
}
// 5 moves for shifting G left 1 pos, plus one to the final 0,0
dist_from_empty + 5 * (x_max - 1) + 1
}
#[derive(Copy, Clone, Eq, PartialEq)]
struct Pos {
x: usize,
y: usize,
dist: usize,
}
impl Ord for Pos {
fn cmp(&self, other: &Self) -> Ordering {
other.dist.cmp(&self.dist)
}
}
impl PartialOrd for Pos {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[cfg(test)]
mod day22_tests {
use std::fs;
use crate::y2016::day22::{part_a, part_b};
#[test]
fn test_works() {
assert_eq!(7, part_b(fs::read_to_string("input/2016/day22/test.txt").unwrap()));
}
#[test]
fn input_works() {
assert_eq!(981, part_a(fs::read_to_string("input/2016/day22/input.txt").unwrap()));
assert_eq!(233, part_b(fs::read_to_string("input/2016/day22/input.txt").unwrap()));
}
}