-
Notifications
You must be signed in to change notification settings - Fork 0
/
day05.rs
64 lines (56 loc) · 1.76 KB
/
day05.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
use regex::Regex;
use std::fs::File;
use std::io::{self, BufRead};
fn parse_stacks() -> std::io::Result<Vec<Vec<char>>> {
let file = File::open("./inputs/day05-1.txt")?;
let reader = io::BufReader::new(file);
let mut stacks = Vec::<Vec<char>>::new();
for line in reader.lines() {
let l = line?;
for pos in (1..l.len()).step_by(4) {
let cargo = l.chars().nth(pos).unwrap();
if cargo != ' ' {
let stack_num = (pos - 1) / 4;
if stacks.len() <= stack_num {
for _ in stacks.len()..=stack_num {
stacks.push(Vec::<char>::new());
}
}
stacks[stack_num].insert(0, cargo);
}
}
}
Ok(stacks)
}
fn main() -> std::io::Result<()> {
let file = File::open("./inputs/day05-2.txt")?;
let reader = io::BufReader::new(file);
let mut stacks_part1 = parse_stacks()?;
let mut stacks_part2 = stacks_part1.clone();
let re = Regex::new(r"move (\d+) from (\d+) to (\d+)").unwrap();
for line in reader.lines() {
for cap in re.captures_iter(&line?) {
let moves = &cap[1].parse::<usize>().unwrap();
let from = &cap[2].parse::<usize>().unwrap();
let to = &cap[3].parse::<usize>().unwrap();
for _ in 0..*moves {
match stacks_part1[*from - 1].pop() {
Some(cargo) => stacks_part1[*to - 1].push(cargo),
None => panic!("Moving from empty stack"),
}
}
let start_offset = stacks_part2[*from - 1].len() - moves;
let to_move = stacks_part2[*from - 1].split_off(start_offset);
stacks_part2[*to - 1].extend(to_move);
}
}
for mut stack in stacks_part1 {
print!("{}", stack.pop().unwrap_or(' '));
}
println!();
for mut stack in stacks_part2 {
print!("{}", stack.pop().unwrap_or(' '));
}
println!();
Ok(())
}