[TASK] Implemented day 5
This commit is contained in:
120
src/day5.rs
Normal file
120
src/day5.rs
Normal file
@@ -0,0 +1,120 @@
|
||||
use rustc_hash::FxHashMap;
|
||||
use crate::day_solver::DaySolver;
|
||||
|
||||
use super::util;
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
struct StackInstruction {
|
||||
amount: u8,
|
||||
from: u8,
|
||||
to: u8
|
||||
}
|
||||
|
||||
impl StackInstruction {
|
||||
fn parse(str: &str) -> StackInstruction {
|
||||
let mut split = str.split_whitespace()
|
||||
.filter(|s| s.chars().all(|c| c.is_numeric()));
|
||||
return StackInstruction {
|
||||
amount: split.next().unwrap().parse().unwrap(),
|
||||
from: split.next().unwrap().parse().unwrap(),
|
||||
to: split.next().unwrap().parse().unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Day5 {
|
||||
stacks: Vec<Vec<char>>,
|
||||
stack_ids_rev: FxHashMap<u8, usize>,
|
||||
instructions: Vec<StackInstruction>
|
||||
}
|
||||
|
||||
impl Day5 {
|
||||
fn execute_instruction_part1(&mut self, instr: &StackInstruction) {
|
||||
for _ in 0..instr.amount {
|
||||
let moving_crate = self.stacks[self.stack_ids_rev[&instr.from]].pop().unwrap();
|
||||
self.stacks[self.stack_ids_rev[&instr.to]].push(moving_crate);
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_instructions_part1(&self) -> Day5 {
|
||||
|
||||
let mut updated_day = self.clone();
|
||||
for instr in &self.instructions {
|
||||
updated_day.execute_instruction_part1(instr);
|
||||
}
|
||||
updated_day
|
||||
}
|
||||
|
||||
fn execute_instruction_part2(&mut self, instr: &StackInstruction) {
|
||||
|
||||
let from_stack = &self.stacks[self.stack_ids_rev[&instr.from]];
|
||||
let from_stack_remaining = from_stack.len() - instr.amount as usize;
|
||||
let to_move: Vec<char> = from_stack.iter().skip(from_stack_remaining).cloned().collect();
|
||||
self.stacks[self.stack_ids_rev[&instr.to]].extend(to_move);
|
||||
self.stacks[self.stack_ids_rev[&instr.from]].truncate(from_stack_remaining);
|
||||
}
|
||||
|
||||
fn execute_instructions_part2(&self) -> Day5 {
|
||||
|
||||
let mut updated_day = self.clone();
|
||||
for instr in &self.instructions {
|
||||
updated_day.execute_instruction_part2(instr);
|
||||
}
|
||||
updated_day
|
||||
}
|
||||
}
|
||||
|
||||
impl Day5 {
|
||||
|
||||
pub fn create() -> Self {
|
||||
// let lines = util::read_file("input/day5_example.txt");
|
||||
let lines = util::read_file("input/day5.txt");
|
||||
|
||||
// Put the input into the day struct
|
||||
let mut line_split = lines.split(|s| s.is_empty());
|
||||
let stack_lines = line_split.next().unwrap();
|
||||
let instruction_lines = line_split.next().unwrap();
|
||||
|
||||
let stack_id_line = stack_lines.last().unwrap();
|
||||
let stack_id_strs: Vec<&str> = stack_id_line.split_whitespace().collect();
|
||||
let mut stack_id_idxs = Vec::new();
|
||||
let mut last_idx = 0;
|
||||
for stack_id_str in &stack_id_strs {
|
||||
let stack_id_idx = stack_id_line[last_idx..].find(stack_id_str).unwrap();
|
||||
stack_id_idxs.push(last_idx + stack_id_idx);
|
||||
last_idx = stack_id_idx + 1;
|
||||
}
|
||||
let stack_ids: Vec<u8> = stack_id_strs.iter().map(|s| s.parse::<u8>().unwrap()).collect();
|
||||
|
||||
let mut stacks: Vec<Vec<char>> = vec![Vec::with_capacity(stack_lines.len() - 1); stack_id_idxs.len()];
|
||||
for stack_line in stack_lines.iter().rev().skip(1) {
|
||||
stack_id_idxs.iter().enumerate()
|
||||
.for_each(|(i, stack_id_idx)| if let Some(c) = stack_line.chars().nth(stack_id_idx.to_owned()).filter(|c| !c.is_whitespace()) { stacks[i].push(c) })
|
||||
}
|
||||
|
||||
return Day5 {
|
||||
stacks,
|
||||
stack_ids_rev: FxHashMap::from_iter(stack_ids.iter().enumerate().map(|(k, v)| (v.clone(), k))),
|
||||
instructions: instruction_lines.iter().map(|s| StackInstruction::parse(s)).collect()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl DaySolver for Day5 {
|
||||
|
||||
|
||||
fn solve_part1(&mut self) -> String {
|
||||
let updated_day = self.execute_instructions_part1();
|
||||
return updated_day.stacks.iter().filter_map(|s| s.last())
|
||||
.cloned()
|
||||
.collect::<String>()
|
||||
}
|
||||
|
||||
fn solve_part2(&mut self) -> String {
|
||||
let updated_day = self.execute_instructions_part2();
|
||||
return updated_day.stacks.iter().filter_map(|s| s.last())
|
||||
.cloned()
|
||||
.collect::<String>()
|
||||
}
|
||||
}
|
||||
@@ -8,8 +8,8 @@ pub struct DayX {
|
||||
impl DayX {
|
||||
|
||||
pub fn create() -> Self {
|
||||
// let lines = util::read_file("input/dayX_example.txt");
|
||||
let lines = util::read_file("input/dayX.txt");
|
||||
let lines = util::read_file("input/dayX_example.txt");
|
||||
// let lines = util::read_file("input/dayX.txt");
|
||||
|
||||
// Put the input into the day struct
|
||||
return DayX {}
|
||||
|
||||
@@ -3,6 +3,7 @@ use crate::day1::Day1;
|
||||
use crate::day2::Day2;
|
||||
use crate::day3::Day3;
|
||||
use crate::day4::Day4;
|
||||
use crate::day5::Day5;
|
||||
use crate::day_solver::DaySolver;
|
||||
|
||||
mod util;
|
||||
@@ -11,8 +12,9 @@ mod day_solver;
|
||||
mod day2;
|
||||
mod day3;
|
||||
mod day4;
|
||||
mod day5;
|
||||
|
||||
const MAX_DAY: u8 = 4;
|
||||
const MAX_DAY: u8 = 5;
|
||||
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
|
||||
|
||||
fn main() {
|
||||
@@ -80,6 +82,7 @@ fn build_day_solver(day: u8) -> Option<Box<dyn DaySolver>> {
|
||||
2 => Some(Box::new(Day2::create())),
|
||||
3 => Some(Box::new(Day3::create())),
|
||||
4 => Some(Box::new(Day4::create())),
|
||||
5 => Some(Box::new(Day5::create())),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user