Solved day 5

This commit is contained in:
gabe 2022-12-05 01:48:20 -06:00
parent c92c8843e9
commit 5030dfa2a4
11 changed files with 786 additions and 14 deletions

View file

@ -30,6 +30,10 @@ path="src/day04/solve.rs"
name="day05" name="day05"
path="src/day05/solve.rs" path="src/day05/solve.rs"
[[bin]]
name="day06"
path="src/day06/solve.rs"
[dependencies] [dependencies]
regex="1" regex="1"
once_cell = "1.16.0" once_cell = "1.16.0"

View file

@ -0,0 +1,514 @@
[S] [C] [Z]
[F] [J] [P] [T] [N]
[G] [H] [G] [Q] [G] [D]
[V] [V] [D] [G] [F] [D] [V]
[R] [B] [F] [N] [N] [Q] [L] [S]
[J] [M] [M] [P] [H] [V] [B] [B] [D]
[L] [P] [H] [D] [L] [F] [D] [J] [L]
[D] [T] [V] [M] [J] [N] [F] [M] [G]
1 2 3 4 5 6 7 8 9
move 3 from 4 to 6
move 1 from 5 to 8
move 3 from 7 to 3
move 4 from 5 to 7
move 1 from 7 to 8
move 3 from 9 to 4
move 2 from 8 to 2
move 4 from 4 to 5
move 2 from 5 to 1
move 2 from 5 to 6
move 7 from 8 to 1
move 9 from 3 to 9
move 11 from 6 to 5
move 2 from 6 to 7
move 12 from 1 to 4
move 10 from 2 to 9
move 2 from 3 to 9
move 1 from 7 to 5
move 4 from 7 to 6
move 2 from 6 to 1
move 5 from 1 to 6
move 10 from 9 to 1
move 9 from 9 to 8
move 13 from 4 to 3
move 7 from 6 to 2
move 2 from 8 to 5
move 9 from 3 to 9
move 8 from 9 to 8
move 4 from 8 to 4
move 1 from 7 to 5
move 3 from 9 to 1
move 7 from 2 to 1
move 1 from 3 to 1
move 1 from 3 to 6
move 1 from 6 to 1
move 2 from 3 to 6
move 5 from 4 to 1
move 1 from 6 to 1
move 3 from 8 to 7
move 8 from 8 to 4
move 3 from 5 to 4
move 1 from 6 to 7
move 1 from 5 to 8
move 4 from 5 to 2
move 7 from 5 to 8
move 3 from 2 to 7
move 7 from 4 to 8
move 11 from 8 to 4
move 15 from 4 to 1
move 25 from 1 to 6
move 4 from 8 to 7
move 1 from 2 to 4
move 11 from 6 to 4
move 12 from 6 to 3
move 1 from 1 to 9
move 1 from 9 to 8
move 16 from 1 to 3
move 1 from 8 to 7
move 12 from 4 to 6
move 9 from 6 to 5
move 3 from 6 to 5
move 6 from 7 to 5
move 3 from 3 to 5
move 2 from 6 to 3
move 11 from 5 to 8
move 2 from 8 to 3
move 2 from 1 to 4
move 7 from 3 to 1
move 2 from 4 to 6
move 2 from 6 to 2
move 5 from 7 to 3
move 1 from 1 to 6
move 1 from 1 to 8
move 2 from 2 to 5
move 1 from 7 to 4
move 1 from 1 to 2
move 10 from 3 to 5
move 11 from 3 to 6
move 1 from 4 to 9
move 1 from 9 to 4
move 1 from 4 to 2
move 2 from 5 to 9
move 2 from 2 to 8
move 2 from 1 to 6
move 2 from 1 to 2
move 2 from 3 to 6
move 3 from 8 to 1
move 3 from 1 to 4
move 7 from 8 to 3
move 2 from 9 to 5
move 2 from 4 to 9
move 7 from 5 to 6
move 2 from 8 to 6
move 1 from 4 to 8
move 2 from 2 to 4
move 21 from 6 to 3
move 10 from 5 to 7
move 7 from 7 to 6
move 1 from 9 to 3
move 1 from 4 to 9
move 1 from 9 to 4
move 1 from 8 to 4
move 8 from 6 to 4
move 1 from 4 to 5
move 1 from 5 to 8
move 4 from 3 to 6
move 1 from 8 to 2
move 1 from 4 to 2
move 2 from 7 to 3
move 2 from 2 to 7
move 22 from 3 to 5
move 4 from 6 to 2
move 2 from 6 to 9
move 7 from 3 to 9
move 6 from 9 to 1
move 18 from 5 to 3
move 2 from 5 to 4
move 20 from 3 to 5
move 3 from 7 to 3
move 5 from 1 to 2
move 11 from 5 to 7
move 1 from 1 to 7
move 3 from 9 to 3
move 16 from 5 to 8
move 7 from 8 to 7
move 1 from 9 to 2
move 8 from 2 to 3
move 2 from 2 to 4
move 3 from 3 to 1
move 9 from 3 to 8
move 1 from 6 to 3
move 9 from 7 to 3
move 3 from 1 to 8
move 1 from 7 to 9
move 1 from 9 to 4
move 1 from 7 to 5
move 10 from 4 to 5
move 2 from 4 to 2
move 19 from 8 to 5
move 1 from 8 to 3
move 4 from 3 to 5
move 2 from 4 to 8
move 4 from 7 to 8
move 4 from 3 to 9
move 4 from 7 to 6
move 2 from 2 to 5
move 2 from 3 to 2
move 6 from 8 to 7
move 1 from 8 to 4
move 2 from 6 to 4
move 3 from 4 to 8
move 3 from 9 to 2
move 4 from 7 to 8
move 28 from 5 to 8
move 16 from 8 to 4
move 11 from 8 to 4
move 3 from 3 to 4
move 7 from 5 to 8
move 13 from 8 to 7
move 1 from 5 to 6
move 1 from 6 to 7
move 1 from 9 to 2
move 2 from 6 to 2
move 12 from 4 to 9
move 4 from 4 to 1
move 2 from 9 to 8
move 4 from 8 to 3
move 3 from 4 to 5
move 4 from 4 to 1
move 4 from 4 to 7
move 3 from 7 to 9
move 5 from 9 to 7
move 7 from 2 to 3
move 1 from 5 to 7
move 8 from 1 to 5
move 1 from 2 to 4
move 11 from 3 to 1
move 10 from 5 to 3
move 3 from 9 to 1
move 3 from 9 to 6
move 5 from 1 to 6
move 7 from 6 to 9
move 8 from 9 to 7
move 9 from 3 to 4
move 1 from 6 to 9
move 8 from 7 to 1
move 9 from 4 to 2
move 2 from 1 to 6
move 3 from 2 to 6
move 4 from 4 to 6
move 2 from 9 to 8
move 2 from 1 to 2
move 1 from 3 to 8
move 2 from 8 to 4
move 1 from 6 to 8
move 11 from 1 to 6
move 1 from 1 to 5
move 3 from 2 to 9
move 2 from 9 to 3
move 1 from 1 to 7
move 2 from 4 to 9
move 4 from 2 to 9
move 2 from 8 to 5
move 10 from 6 to 1
move 2 from 5 to 6
move 5 from 9 to 8
move 5 from 8 to 7
move 1 from 2 to 1
move 7 from 1 to 2
move 2 from 9 to 4
move 1 from 3 to 5
move 15 from 7 to 2
move 8 from 6 to 3
move 2 from 4 to 3
move 2 from 6 to 4
move 4 from 7 to 1
move 4 from 7 to 5
move 1 from 6 to 4
move 3 from 1 to 7
move 5 from 7 to 6
move 4 from 7 to 5
move 18 from 2 to 4
move 5 from 6 to 4
move 4 from 1 to 2
move 8 from 3 to 8
move 2 from 8 to 4
move 2 from 3 to 7
move 1 from 5 to 7
move 3 from 8 to 4
move 2 from 7 to 2
move 1 from 3 to 8
move 9 from 2 to 6
move 2 from 8 to 6
move 1 from 7 to 3
move 1 from 3 to 5
move 3 from 6 to 8
move 1 from 8 to 5
move 1 from 5 to 9
move 1 from 1 to 2
move 5 from 4 to 6
move 10 from 6 to 2
move 5 from 2 to 6
move 5 from 6 to 4
move 1 from 6 to 3
move 6 from 4 to 6
move 3 from 2 to 6
move 2 from 2 to 3
move 11 from 4 to 6
move 1 from 9 to 5
move 4 from 6 to 7
move 1 from 4 to 3
move 12 from 4 to 3
move 1 from 8 to 6
move 9 from 5 to 7
move 1 from 5 to 2
move 1 from 8 to 5
move 1 from 4 to 9
move 9 from 7 to 9
move 1 from 3 to 4
move 2 from 3 to 6
move 2 from 5 to 6
move 2 from 8 to 5
move 11 from 3 to 4
move 2 from 3 to 1
move 1 from 2 to 3
move 1 from 3 to 8
move 3 from 7 to 9
move 5 from 4 to 2
move 2 from 5 to 8
move 6 from 4 to 2
move 1 from 1 to 3
move 12 from 9 to 1
move 6 from 1 to 6
move 1 from 8 to 4
move 1 from 8 to 3
move 5 from 2 to 7
move 2 from 3 to 9
move 5 from 7 to 1
move 1 from 7 to 5
move 2 from 9 to 1
move 14 from 1 to 7
move 2 from 4 to 7
move 7 from 2 to 4
move 1 from 2 to 1
move 1 from 1 to 3
move 1 from 5 to 4
move 1 from 9 to 6
move 16 from 6 to 5
move 2 from 5 to 4
move 12 from 6 to 8
move 10 from 4 to 8
move 9 from 7 to 3
move 4 from 7 to 6
move 11 from 5 to 8
move 2 from 5 to 2
move 14 from 8 to 9
move 1 from 5 to 1
move 3 from 9 to 4
move 2 from 2 to 1
move 7 from 8 to 3
move 6 from 3 to 5
move 8 from 9 to 8
move 1 from 6 to 1
move 1 from 4 to 2
move 4 from 3 to 8
move 1 from 7 to 2
move 3 from 1 to 5
move 2 from 5 to 7
move 3 from 9 to 2
move 1 from 1 to 8
move 5 from 5 to 4
move 2 from 7 to 8
move 4 from 2 to 5
move 1 from 2 to 4
move 2 from 7 to 8
move 4 from 6 to 2
move 6 from 5 to 3
move 1 from 6 to 5
move 1 from 5 to 3
move 1 from 3 to 8
move 8 from 8 to 3
move 9 from 8 to 5
move 9 from 8 to 2
move 2 from 8 to 9
move 2 from 3 to 8
move 5 from 5 to 8
move 1 from 3 to 7
move 2 from 9 to 5
move 7 from 2 to 4
move 14 from 4 to 6
move 2 from 2 to 7
move 1 from 7 to 3
move 1 from 7 to 9
move 3 from 5 to 2
move 1 from 7 to 1
move 3 from 2 to 4
move 7 from 8 to 2
move 3 from 6 to 1
move 17 from 3 to 1
move 2 from 8 to 3
move 6 from 2 to 7
move 2 from 7 to 9
move 3 from 6 to 8
move 2 from 8 to 6
move 4 from 2 to 1
move 3 from 4 to 7
move 1 from 8 to 7
move 1 from 8 to 9
move 1 from 4 to 2
move 3 from 5 to 7
move 2 from 3 to 1
move 2 from 3 to 5
move 5 from 7 to 4
move 5 from 7 to 3
move 1 from 4 to 8
move 3 from 3 to 1
move 6 from 1 to 3
move 1 from 7 to 5
move 2 from 9 to 2
move 3 from 5 to 8
move 1 from 8 to 1
move 8 from 3 to 5
move 1 from 4 to 9
move 3 from 6 to 5
move 3 from 6 to 3
move 2 from 3 to 7
move 1 from 4 to 7
move 3 from 6 to 4
move 2 from 7 to 2
move 1 from 7 to 8
move 2 from 5 to 4
move 1 from 6 to 1
move 7 from 4 to 7
move 7 from 5 to 2
move 10 from 2 to 3
move 3 from 2 to 6
move 3 from 8 to 1
move 1 from 8 to 7
move 2 from 6 to 3
move 1 from 6 to 9
move 4 from 7 to 5
move 16 from 1 to 5
move 1 from 9 to 7
move 3 from 7 to 6
move 11 from 5 to 6
move 2 from 7 to 9
move 12 from 6 to 4
move 2 from 6 to 9
move 6 from 3 to 2
move 1 from 5 to 7
move 5 from 9 to 5
move 1 from 9 to 6
move 4 from 3 to 7
move 1 from 4 to 2
move 7 from 2 to 5
move 3 from 5 to 2
move 6 from 5 to 6
move 3 from 2 to 6
move 9 from 6 to 8
move 5 from 5 to 9
move 5 from 7 to 1
move 4 from 1 to 9
move 2 from 9 to 4
move 1 from 6 to 7
move 9 from 4 to 1
move 7 from 5 to 9
move 18 from 1 to 3
move 9 from 9 to 5
move 8 from 8 to 2
move 1 from 2 to 5
move 4 from 2 to 3
move 4 from 9 to 6
move 1 from 4 to 8
move 2 from 5 to 7
move 2 from 9 to 2
move 10 from 3 to 9
move 5 from 5 to 9
move 1 from 7 to 2
move 2 from 8 to 7
move 2 from 3 to 5
move 2 from 9 to 1
move 2 from 7 to 3
move 1 from 2 to 1
move 5 from 5 to 8
move 1 from 2 to 1
move 15 from 3 to 6
move 1 from 7 to 6
move 10 from 6 to 5
move 1 from 7 to 8
move 4 from 1 to 6
move 1 from 8 to 3
move 2 from 1 to 5
move 3 from 8 to 1
move 1 from 4 to 6
move 1 from 4 to 2
move 4 from 9 to 7
move 6 from 5 to 7
move 3 from 1 to 9
move 10 from 6 to 8
move 2 from 1 to 3
move 8 from 7 to 9
move 1 from 9 to 6
move 2 from 7 to 9
move 3 from 3 to 5
move 1 from 2 to 6
move 2 from 6 to 5
move 5 from 9 to 4
move 4 from 8 to 2
move 1 from 1 to 3
move 4 from 5 to 9
move 3 from 6 to 1
move 2 from 1 to 5
move 3 from 5 to 2
move 8 from 8 to 3
move 11 from 9 to 4
move 13 from 4 to 8
move 2 from 9 to 2
move 2 from 3 to 1
move 1 from 4 to 1
move 1 from 3 to 8
move 2 from 6 to 9
move 7 from 8 to 1
move 3 from 2 to 5
move 7 from 2 to 5
move 3 from 4 to 6
move 4 from 9 to 2
move 2 from 3 to 5
move 9 from 5 to 6
move 5 from 2 to 7
move 2 from 9 to 2
move 2 from 9 to 7
move 12 from 6 to 8
move 5 from 5 to 7
move 1 from 9 to 8
move 3 from 1 to 6
move 5 from 5 to 8
move 6 from 1 to 9
move 2 from 1 to 5
move 1 from 6 to 9
move 5 from 9 to 7
move 2 from 5 to 8
move 11 from 7 to 6
move 20 from 8 to 1
move 2 from 9 to 8
move 4 from 7 to 6
move 6 from 8 to 3
move 13 from 6 to 9
move 4 from 3 to 2
move 4 from 6 to 3
move 2 from 3 to 6
move 5 from 9 to 8
move 2 from 7 to 1
move 2 from 6 to 9
move 6 from 8 to 3
move 6 from 3 to 6
move 5 from 2 to 9
move 22 from 1 to 3
move 3 from 2 to 1
move 5 from 9 to 3
move 1 from 1 to 6
move 3 from 6 to 2
move 1 from 2 to 4
move 33 from 3 to 5
move 1 from 8 to 7

View file

@ -1,7 +1,15 @@
use crate::utilities::*; use crate::utilities::*;
pub fn part1() -> usize { pub fn part1(input: &(WorkArea, Vec<Move>)) -> Vec<char> {
unimplemented!() let (mut work_area, moves) = input.to_owned();
for r#move in moves {
work_area.apply_move_cratemover9000(&r#move)
}
work_area
.get_stacks()
.iter()
.map(|stack| stack.last().unwrap().to_owned())
.collect()
} }
#[cfg(test)] #[cfg(test)]
@ -10,6 +18,31 @@ mod tests {
#[test] #[test]
fn test_part1() { fn test_part1() {
assert_eq!(part1(), 0); let input = (
WorkArea::new(vec![vec!['Z', 'N'], vec!['M', 'C', 'D'], vec!['P']]),
vec![
Move {
to: 1,
from: 2,
number: 1,
},
Move {
to: 3,
from: 1,
number: 3,
},
Move {
to: 1,
from: 2,
number: 2,
},
Move {
to: 2,
from: 1,
number: 1,
},
],
);
assert_eq!(part1(&input), vec!['C', 'M', 'Z']);
} }
} }

View file

@ -1,7 +1,15 @@
use crate::utilities::*; use crate::utilities::*;
pub fn part2() -> usize { pub fn part2(input: &(WorkArea, Vec<Move>)) -> Vec<char> {
unimplemented!() let (mut work_area, moves) = input.to_owned();
for r#move in moves {
work_area.apply_move_cratemover9001(&r#move)
}
work_area
.get_stacks()
.iter()
.map(|stack| stack.last().unwrap().to_owned())
.collect()
} }
#[cfg(test)] #[cfg(test)]
@ -10,6 +18,31 @@ mod tests {
#[test] #[test]
fn test_part2() { fn test_part2() {
assert_eq!(part2(), 0); let input = (
WorkArea::new(vec![vec!['Z', 'N'], vec!['M', 'C', 'D'], vec!['P']]),
vec![
Move {
to: 1,
from: 2,
number: 1,
},
Move {
to: 3,
from: 1,
number: 3,
},
Move {
to: 1,
from: 2,
number: 2,
},
Move {
to: 2,
from: 1,
number: 1,
},
],
);
assert_eq!(part2(&input), vec!['M', 'C', 'D']);
} }
} }

View file

@ -7,8 +7,8 @@ fn main() {
let _structured_input = utilities::parse(_input); let _structured_input = utilities::parse(_input);
println!("Part One"); println!("Part One");
println!("Result: {}", part1::part1()); println!("Result: {:?}", part1::part1(&_structured_input));
println!("Part Two"); println!("Part Two");
println!("Result: {}", part2::part2()); println!("Result: {:?}", part2::part2(&_structured_input));
} }

View file

@ -1,16 +1,144 @@
pub fn parse(input: &str) -> usize { use once_cell::sync::Lazy;
unimplemented!() use regex::Regex;
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Move {
pub to: usize,
pub from: usize,
pub number: u8,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct WorkArea {
stacks: Vec<Vec<char>>,
}
impl WorkArea {
pub fn new(stacks: Vec<Vec<char>>) -> Self {
Self { stacks }
}
pub fn apply_move_cratemover9000(&mut self, action: &Move) {
for _ in 0..action.number {
let cargo = self.stacks.get_mut(action.from - 1).unwrap().pop().unwrap();
self.stacks.get_mut(action.to - 1).unwrap().push(cargo);
}
}
pub fn apply_move_cratemover9001(&mut self, action: &Move) {
let mut crane_holder: Vec<char> = Vec::new();
for _ in 0..action.number {
let cargo = self.stacks.get_mut(action.from - 1).unwrap().pop().unwrap();
crane_holder.push(cargo);
}
for cargo in crane_holder.iter().rev() {
self.stacks.get_mut(action.to - 1).unwrap().push(*cargo);
}
}
pub fn get_stacks(&self) -> &Vec<Vec<char>> {
&self.stacks
}
}
pub fn parse(input: &str) -> (WorkArea, Vec<Move>) {
let mut input = input.split("\n\n");
let work_area = parse_work_area(input.next().unwrap());
let moves = parse_moves(input.next().unwrap());
(work_area, moves)
}
pub fn parse_work_area(input: &str) -> WorkArea {
//decode those bottom index numbers
let index_row = input.lines().rev().next().unwrap();
//some ascii math and array math to get the second to last char and convert it into a number.
let index_max: usize = (index_row.as_bytes()[index_row.len() - 2] - b'0') as usize;
//initalize the work area:
let mut work_area: Vec<Vec<char>> = Vec::new();
for _ in 0..index_max {
work_area.push(Vec::new())
}
//now parse the rest
for line in input.lines().rev() {
for (y, cargo_crate) in line.as_bytes().chunks(4).enumerate() {
let cargo = cargo_crate[1] as char;
//easiest way to filter out that last line is just to filter out digits.
if cargo != ' ' && !cargo.is_ascii_digit() {
work_area.get_mut(y).unwrap().push(cargo)
}
}
}
WorkArea::new(work_area)
}
pub fn parse_moves(input: &str) -> Vec<Move> {
static PARSE_MOVES_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^move (\d+) from (\d+) to (\d+)$").unwrap());
input
.lines()
.map(|line| {
let cap = PARSE_MOVES_REGEX.captures(line).unwrap();
Move {
to: cap.get(3).unwrap().as_str().parse().unwrap(),
from: cap.get(2).unwrap().as_str().parse().unwrap(),
number: cap.get(1).unwrap().as_str().parse().unwrap(),
}
})
.collect()
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
#[test]
fn test_parse_work_area() {
let input = " [D]
[N] [C]
[Z] [M] [P]
1 2 3 ";
assert_eq!(
parse_work_area(input),
WorkArea::new(vec![vec!['Z', 'N'], vec!['M', 'C', 'D'], vec!['P'],],)
)
}
#[test] #[test]
fn test_parse() { fn test_parse() {
let input = let input = " [D]
"test" [N] [C]
; [Z] [M] [P]
assert_eq!(parse(input), 0); 1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2";
assert_eq!(
parse(input),
(
WorkArea::new(vec![vec!['Z', 'N'], vec!['M', 'C', 'D'], vec!['P'],],),
vec![
Move {
to: 1,
from: 2,
number: 1
},
Move {
to: 3,
from: 1,
number: 3
},
Move {
to: 1,
from: 2,
number: 2
},
Move {
to: 2,
from: 1,
number: 1
},
]
)
);
} }
} }

0
src/day06/input.txt Normal file
View file

15
src/day06/part1.rs Normal file
View file

@ -0,0 +1,15 @@
use crate::utilities::*;
pub fn part1() -> usize {
unimplemented!()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() {
assert_eq!(part1(), 0);
}
}

15
src/day06/part2.rs Normal file
View file

@ -0,0 +1,15 @@
use crate::utilities::*;
pub fn part2() -> usize {
unimplemented!()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part2() {
assert_eq!(part2(), 0);
}
}

14
src/day06/solve.rs Normal file
View file

@ -0,0 +1,14 @@
mod part1;
mod part2;
mod utilities;
fn main() {
let _input = include_str!("./input.txt");
let _structured_input = utilities::parse(_input);
println!("Part One");
println!("Result: {}", part1::part1());
println!("Part Two");
println!("Result: {}", part2::part2());
}

16
src/day06/utilities.rs Normal file
View file

@ -0,0 +1,16 @@
pub fn parse(input: &str) -> usize {
unimplemented!()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse() {
let input =
"test"
;
assert_eq!(parse(input), 0);
}
}