Day 05 Part 1

This commit is contained in:
Max Nuding 2022-12-05 06:05:05 +00:00
parent a9127c3301
commit f73bc65d9f
3 changed files with 554 additions and 1005 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,14 +1,52 @@
use crate::read; use crate::read;
use tuple_map::*;
pub fn run() { pub fn run() {
let input = read("05"); let input = read("05");
let lines = input.lines().map(|l| l).collect::<Vec<_>>(); let lines = input.split_once("\n\n").unwrap();
let stackslines = lines.0.lines().collect::<Vec<_>>();
let mut stacks = std::collections::HashMap::new();
for l in stackslines {
let length = l.len();
let mut idx = 0;
while idx <= length {
let charnum = 1 + idx * 4;
if let Some(s1) = l.chars().nth(charnum) {
if !s1.is_whitespace() && !s1.is_numeric() {
let e = stacks.entry(idx + 1).or_insert(vec![]);
e.push(s1);
}
}
idx += 1;
}
}
let k = stacks.keys().max().unwrap();
for i in 1..=*k {
stacks.entry(i).and_modify(|s| s.reverse());
}
let re = regex::Regex::new(r"move (\d+) from (\d+) to (\d+)").unwrap();
for l in lines.1.lines().filter(|l| !l.is_empty()) {
let capt = re.captures(l).unwrap();
let count: usize = capt[1].parse().unwrap();
let from: usize = capt[2].parse().unwrap();
let to: usize = capt[3].parse().unwrap();
for _i in 0..count {
let fr = stacks.get_mut(&from).unwrap();
let c = fr.pop().unwrap();
stacks.entry(to).and_modify(|s| s.push(c));
}
}
#[cfg(feature = "part1")] #[cfg(feature = "part1")]
{ {
let count = lines.iter().count(); let mut keys = stacks.keys().copied().collect::<Vec<_>>();
println!("Day 5, Part 01: {}", count); keys.sort();
let chars = keys
.iter()
.map(|s| stacks.get(s).unwrap().last().unwrap())
.collect::<String>();
println!("Day 5, Part 01: {}", chars);
} }
#[cfg(feature = "part2")] #[cfg(feature = "part2")]

View File

@ -23,7 +23,7 @@ fn main() {
day04::run(); day04::run();
} }
if cfg!(feature = "day05") || (cfg!(feature = "today") && today == 5) { if cfg!(feature = "day05") || (cfg!(feature = "today") && today == 5) {
day04::run(); day05::run();
} }
println!("Finished, time taken: {:?}", now.elapsed()) println!("Finished, time taken: {:?}", now.elapsed())
} }