Skip to content

Commit 32423f9

Browse files
committed
Complete day9 part2
1 parent 33aeabd commit 32423f9

File tree

1 file changed

+49
-38
lines changed

1 file changed

+49
-38
lines changed

src/day9.rs

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -97,24 +97,20 @@ pub fn part1(input: &[Disk]) -> usize {
9797
pos += 1;
9898
}
9999
}
100-
Disk::Free { blocks } => {
101-
for _ in 0..blocks {
102-
pos += 1;
103-
}
104-
}
100+
_ => unreachable!(),
105101
}
106102
}
107103
res
108104
}
109105

110106
#[aoc(day9, part2)]
111107
pub fn part2(input: &[Disk]) -> usize {
112-
let mut i = 0;
108+
let mut j = input.len() - 1;
113109
let mut input = input.to_vec();
114110

115111
loop {
116-
// for j in &input {
117-
// match j {
112+
// for file in &input {
113+
// match file {
118114
// Disk::File { id, blocks } => {
119115
// print!("{}", id.to_string().repeat(*blocks));
120116
// }
@@ -125,45 +121,56 @@ pub fn part2(input: &[Disk]) -> usize {
125121
// }
126122
// println!();
127123

128-
match input[i] {
129-
Disk::File { id: _, blocks: _ } => {
130-
i += 1;
124+
match input[j] {
125+
Disk::Free { blocks: _ } => {
126+
j -= 1;
131127
}
132-
Disk::Free {
133-
blocks: free_blocks,
128+
Disk::File {
129+
id: file_id,
130+
blocks: file_blocks,
134131
} => {
135-
if let Some(last) = input.pop() {
136-
match last {
137-
Disk::Free { blocks: _ } => continue,
138-
Disk::File { id, blocks } => match blocks.cmp(&free_blocks) {
132+
let mut i = 0;
133+
while i < j {
134+
match input[i] {
135+
Disk::File { id: _, blocks: _ } => i += 1,
136+
Disk::Free {
137+
blocks: free_blocks,
138+
} => match file_blocks.cmp(&free_blocks) {
139139
std::cmp::Ordering::Equal => {
140-
input[i] = last;
141-
i += 1;
140+
input[i] = Disk::File {
141+
id: file_id,
142+
blocks: file_blocks,
143+
};
144+
input[j] = Disk::Free {
145+
blocks: free_blocks,
146+
};
147+
break;
142148
}
143149
std::cmp::Ordering::Less => {
144-
input.insert(i, last);
150+
input.insert(
151+
i,
152+
Disk::File {
153+
id: file_id,
154+
blocks: file_blocks,
155+
},
156+
);
157+
j += 1;
145158
input[i + 1] = Disk::Free {
146-
blocks: free_blocks - blocks,
159+
blocks: free_blocks - file_blocks,
147160
};
148-
i += 1;
149-
}
150-
std::cmp::Ordering::Greater => {
151-
input[i] = Disk::File {
152-
id,
153-
blocks: free_blocks,
161+
input[j] = Disk::Free {
162+
blocks: file_blocks,
154163
};
155-
input.push(Disk::File {
156-
id,
157-
blocks: blocks - free_blocks,
158-
});
159-
i += 1;
164+
break;
160165
}
166+
std::cmp::Ordering::Greater => i += 1,
161167
},
162168
}
163169
}
170+
j -= 1;
164171
}
165172
}
166-
if i >= input.len() {
173+
if j == 0 {
167174
break;
168175
}
169176
}
@@ -178,7 +185,11 @@ pub fn part2(input: &[Disk]) -> usize {
178185
pos += 1;
179186
}
180187
}
181-
_ => unreachable!(),
188+
Disk::Free { blocks } => {
189+
for _ in 0..blocks {
190+
pos += 1;
191+
}
192+
}
182193
}
183194
}
184195
res
@@ -195,8 +206,8 @@ mod tests {
195206
assert_eq!(1928, part1(&input_generator(INPUT)))
196207
}
197208

198-
// #[test]
199-
// fn test_part2() {
200-
// assert_eq!(1, part2(&input_generator(INPUT)))
201-
// }
209+
#[test]
210+
fn test_part2() {
211+
assert_eq!(2858, part2(&input_generator(INPUT)))
212+
}
202213
}

0 commit comments

Comments
 (0)