-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.rs
136 lines (123 loc) · 3.61 KB
/
07.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
/*
* Day 7: No Space Left On Device
* See [https://adventofcode.com/2022/day/7]
*/
#[derive(PartialEq)]
struct FTree {
parent: Option<usize>,
name: String,
dir: bool,
children: Vec<usize>,
size: u32
}
struct FS {
nodes: Vec<FTree>
}
impl FS {
fn new() -> Self {
Self { nodes: vec![
FTree { parent: None, name: "".to_string(), dir: true, children: Vec::new(), size: 0 }
] }
}
fn find_dir(&self, top: usize, name: &str) -> Option<&usize> {
let children = &self.nodes[top].children;
children.iter().find(|&&id| name == self.nodes[id].name)
}
fn add(&mut self, top: usize, name: &str, dir: bool, size: u32) {
let ft = FTree {
parent: Some(top),
name: name.to_string(),
dir,
children: vec![],
size
};
let l = self.nodes.len();
self.nodes.push(ft);
let mut tft = &mut self.nodes[top];
tft.children.push(l);
tft.size += size;
while let Some(pp) = tft.parent {
tft = &mut self.nodes[pp];
tft.size += size;
}
}
}
fn parse_tree(input: &str) -> FS {
let mut fs = FS::new();
let mut cd = 0;
for l in input.lines() {
let bl = l.as_bytes()[0];
let mut sp = l.split_ascii_whitespace();
match bl {
b'$' => {
sp.next();
match sp.next().expect("No command given") {
"cd" => {
cd = match sp.next().expect("Unknown dir type") {
".." => { fs.nodes[cd].parent.unwrap() }
"/" => { 0 }
x => {
if let Some(nd) = fs.find_dir(cd, x) {
*nd
} else {
panic!("Cannot find dir {}", x);
}
}
};
}
"ls" => {}
x => { panic!("Unknown command: {}", x); }
}
},
x if x.is_ascii_digit() => {
let n = sp.next().and_then(|d| d.parse::<u32>().ok()).expect("Unknown size");
let d = sp.next().unwrap();
fs.add(cd, d, false, n);
},
b'd' => {
sp.next();
let d = sp.next().unwrap();
fs.add(cd, d, true, 0);
}
_ => {
panic!("Invalid line: {}", l);
}
}
}
fs
}
pub fn part_1(input: &str) -> Option<u32> {
let fs = parse_tree(input);
let vs = fs.nodes.iter()
.filter_map(|ft| {
if !ft.dir { return None; }
let sz = ft.size;
if sz > 0 && sz <= 100000 { Some(sz) } else { None }
})
.sum();
Some(vs)
}
pub fn part_2(input: &str) -> Option<u32> {
let fs = parse_tree(input);
let nn = 30000000 - (70000000 - fs.nodes[0].size);
let mut vs: Vec<u32> = fs.nodes.iter()
.filter_map(|ft| {
if !ft.dir { return None; }
let sz = ft.size;
if sz > 0 { Some(sz) } else { None }
})
.collect();
vs.sort_by(|a, b| b.cmp(a));
let pos = vs.iter().position(|&x| x <= nn).unwrap();
Some(vs[pos - 1])
}
aoc2022::solve!(part_1, part_2);
#[cfg(test)]
mod tests {
use aoc2022::assert_ex;
use super::*;
#[test]
fn test_part_1() { assert_ex!(part_1, 95437); }
#[test]
fn test_part_2() { assert_ex!(part_2, 24933642); }
}