Add Day 5 solution

This commit is contained in:
Brandon Scott 2022-12-11 21:39:51 -06:00
parent b2b5248771
commit e278a3d28c
4 changed files with 597 additions and 0 deletions

View File

@ -0,0 +1,9 @@
[D]
[N] [C]
[Z] [M] [P]
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

View File

@ -0,0 +1,512 @@
[Q] [B] [H]
[F] [W] [D] [Q] [S]
[D] [C] [N] [S] [G] [F]
[R] [D] [L] [C] [N] [Q] [R]
[V] [W] [L] [M] [P] [S] [M] [M]
[J] [B] [F] [P] [B] [B] [P] [F] [F]
[B] [V] [G] [J] [N] [D] [B] [L] [V]
[D] [P] [R] [W] [H] [R] [Z] [W] [S]
1 2 3 4 5 6 7 8 9
move 1 from 4 to 1
move 2 from 4 to 8
move 5 from 9 to 6
move 1 from 1 to 3
move 5 from 8 to 3
move 1 from 1 to 5
move 4 from 3 to 6
move 14 from 6 to 2
move 5 from 4 to 5
move 7 from 7 to 2
move 24 from 2 to 3
move 13 from 3 to 2
move 1 from 7 to 9
move 1 from 9 to 5
move 7 from 2 to 6
move 3 from 1 to 7
move 3 from 6 to 3
move 2 from 7 to 1
move 1 from 7 to 5
move 2 from 2 to 6
move 2 from 1 to 4
move 9 from 5 to 1
move 1 from 6 to 3
move 4 from 5 to 4
move 1 from 2 to 7
move 4 from 6 to 2
move 7 from 2 to 3
move 2 from 2 to 6
move 2 from 2 to 3
move 2 from 5 to 4
move 1 from 7 to 3
move 4 from 6 to 7
move 19 from 3 to 6
move 3 from 7 to 4
move 1 from 7 to 8
move 1 from 8 to 1
move 2 from 1 to 3
move 10 from 3 to 2
move 3 from 3 to 8
move 1 from 3 to 9
move 1 from 9 to 6
move 11 from 6 to 8
move 2 from 3 to 8
move 6 from 4 to 3
move 3 from 4 to 1
move 7 from 2 to 8
move 1 from 3 to 6
move 6 from 8 to 5
move 1 from 4 to 6
move 9 from 6 to 9
move 6 from 3 to 8
move 1 from 3 to 5
move 10 from 1 to 3
move 11 from 8 to 7
move 1 from 3 to 5
move 1 from 1 to 8
move 5 from 9 to 2
move 1 from 6 to 3
move 5 from 3 to 6
move 1 from 3 to 5
move 4 from 6 to 4
move 1 from 5 to 9
move 6 from 2 to 4
move 2 from 2 to 9
move 5 from 5 to 1
move 2 from 1 to 7
move 10 from 8 to 3
move 1 from 8 to 6
move 3 from 6 to 3
move 6 from 4 to 2
move 8 from 3 to 8
move 3 from 4 to 8
move 4 from 2 to 1
move 3 from 5 to 3
move 4 from 7 to 6
move 2 from 9 to 3
move 1 from 2 to 9
move 1 from 2 to 3
move 2 from 4 to 8
move 1 from 7 to 9
move 5 from 7 to 8
move 2 from 7 to 3
move 14 from 3 to 2
move 3 from 9 to 5
move 1 from 3 to 1
move 1 from 7 to 4
move 3 from 9 to 8
move 7 from 8 to 9
move 7 from 2 to 5
move 2 from 3 to 7
move 2 from 7 to 6
move 16 from 8 to 9
move 4 from 6 to 5
move 1 from 2 to 5
move 21 from 9 to 5
move 3 from 9 to 3
move 6 from 1 to 4
move 1 from 1 to 9
move 1 from 1 to 4
move 2 from 6 to 3
move 3 from 4 to 6
move 3 from 4 to 8
move 1 from 9 to 4
move 2 from 4 to 6
move 4 from 3 to 6
move 1 from 3 to 4
move 1 from 4 to 9
move 1 from 9 to 8
move 1 from 8 to 6
move 6 from 2 to 1
move 2 from 8 to 4
move 6 from 1 to 8
move 23 from 5 to 9
move 1 from 4 to 7
move 1 from 7 to 1
move 22 from 9 to 7
move 4 from 8 to 7
move 1 from 5 to 2
move 1 from 1 to 9
move 2 from 8 to 4
move 6 from 6 to 3
move 2 from 9 to 5
move 18 from 7 to 4
move 18 from 4 to 5
move 1 from 2 to 7
move 1 from 8 to 4
move 6 from 7 to 2
move 5 from 4 to 5
move 1 from 3 to 1
move 1 from 7 to 2
move 4 from 3 to 4
move 1 from 3 to 4
move 1 from 1 to 7
move 1 from 5 to 8
move 3 from 4 to 3
move 3 from 3 to 8
move 2 from 8 to 3
move 2 from 4 to 8
move 2 from 7 to 5
move 1 from 7 to 9
move 2 from 3 to 1
move 1 from 9 to 7
move 4 from 2 to 3
move 1 from 8 to 9
move 2 from 1 to 8
move 2 from 2 to 4
move 1 from 9 to 1
move 4 from 6 to 8
move 1 from 2 to 7
move 1 from 4 to 7
move 4 from 8 to 2
move 1 from 4 to 3
move 1 from 1 to 9
move 4 from 8 to 1
move 2 from 2 to 1
move 3 from 3 to 9
move 2 from 7 to 1
move 32 from 5 to 1
move 1 from 8 to 7
move 6 from 5 to 1
move 2 from 7 to 6
move 1 from 9 to 5
move 1 from 3 to 2
move 1 from 5 to 9
move 2 from 6 to 1
move 1 from 3 to 7
move 1 from 9 to 8
move 36 from 1 to 4
move 1 from 8 to 9
move 5 from 4 to 9
move 6 from 9 to 3
move 2 from 2 to 9
move 3 from 1 to 9
move 1 from 3 to 2
move 30 from 4 to 8
move 1 from 7 to 5
move 1 from 3 to 5
move 3 from 3 to 4
move 2 from 8 to 5
move 3 from 9 to 8
move 3 from 9 to 3
move 19 from 8 to 6
move 2 from 3 to 5
move 3 from 4 to 3
move 1 from 4 to 7
move 8 from 1 to 8
move 1 from 3 to 2
move 1 from 7 to 6
move 4 from 5 to 3
move 1 from 1 to 7
move 2 from 5 to 4
move 1 from 9 to 4
move 12 from 6 to 2
move 1 from 7 to 8
move 6 from 2 to 9
move 3 from 6 to 7
move 2 from 7 to 5
move 6 from 2 to 3
move 8 from 3 to 5
move 5 from 6 to 8
move 5 from 3 to 6
move 1 from 9 to 4
move 1 from 9 to 8
move 5 from 5 to 9
move 3 from 4 to 6
move 1 from 4 to 9
move 1 from 7 to 5
move 1 from 3 to 5
move 8 from 9 to 2
move 3 from 9 to 6
move 27 from 8 to 2
move 10 from 6 to 9
move 1 from 6 to 4
move 1 from 4 to 9
move 2 from 5 to 6
move 5 from 5 to 3
move 2 from 6 to 9
move 5 from 3 to 2
move 12 from 9 to 3
move 5 from 3 to 1
move 3 from 1 to 5
move 1 from 9 to 8
move 1 from 5 to 2
move 1 from 2 to 1
move 1 from 1 to 6
move 1 from 5 to 3
move 34 from 2 to 4
move 8 from 3 to 9
move 1 from 6 to 1
move 1 from 8 to 5
move 4 from 2 to 8
move 3 from 8 to 7
move 1 from 7 to 2
move 7 from 9 to 8
move 1 from 9 to 6
move 2 from 5 to 1
move 1 from 6 to 9
move 1 from 9 to 5
move 2 from 2 to 5
move 5 from 8 to 6
move 2 from 8 to 5
move 1 from 1 to 3
move 12 from 4 to 6
move 2 from 7 to 1
move 4 from 1 to 6
move 3 from 2 to 3
move 1 from 8 to 5
move 1 from 2 to 6
move 1 from 1 to 9
move 1 from 9 to 5
move 16 from 4 to 1
move 4 from 3 to 1
move 8 from 1 to 8
move 1 from 4 to 1
move 6 from 5 to 8
move 1 from 5 to 7
move 12 from 6 to 9
move 7 from 1 to 5
move 2 from 1 to 7
move 1 from 7 to 1
move 9 from 9 to 6
move 15 from 6 to 2
move 2 from 9 to 7
move 4 from 4 to 5
move 2 from 2 to 9
move 3 from 7 to 5
move 2 from 1 to 3
move 1 from 7 to 1
move 10 from 2 to 3
move 6 from 8 to 6
move 3 from 9 to 2
move 14 from 5 to 6
move 1 from 8 to 4
move 5 from 8 to 2
move 2 from 2 to 3
move 24 from 6 to 1
move 3 from 1 to 2
move 9 from 2 to 9
move 1 from 4 to 3
move 1 from 4 to 2
move 1 from 8 to 4
move 23 from 1 to 4
move 3 from 2 to 4
move 2 from 1 to 2
move 1 from 8 to 4
move 3 from 3 to 5
move 3 from 3 to 4
move 3 from 5 to 8
move 3 from 2 to 7
move 2 from 3 to 8
move 15 from 4 to 3
move 2 from 4 to 1
move 19 from 3 to 9
move 1 from 7 to 2
move 1 from 2 to 5
move 1 from 5 to 4
move 1 from 7 to 6
move 1 from 7 to 4
move 3 from 8 to 3
move 1 from 8 to 4
move 5 from 3 to 8
move 1 from 3 to 6
move 22 from 9 to 2
move 17 from 2 to 6
move 3 from 9 to 3
move 9 from 4 to 9
move 6 from 4 to 9
move 5 from 2 to 6
move 1 from 4 to 2
move 1 from 4 to 9
move 1 from 1 to 6
move 19 from 9 to 2
move 4 from 8 to 7
move 1 from 1 to 5
move 1 from 5 to 3
move 1 from 8 to 1
move 1 from 8 to 2
move 4 from 3 to 7
move 12 from 6 to 1
move 3 from 7 to 3
move 7 from 2 to 7
move 9 from 2 to 6
move 4 from 2 to 6
move 13 from 1 to 4
move 8 from 6 to 4
move 16 from 4 to 8
move 12 from 7 to 6
move 3 from 8 to 3
move 1 from 1 to 2
move 4 from 3 to 8
move 5 from 8 to 9
move 27 from 6 to 8
move 2 from 3 to 7
move 2 from 2 to 8
move 2 from 7 to 5
move 1 from 5 to 9
move 1 from 5 to 1
move 1 from 6 to 9
move 2 from 6 to 2
move 2 from 2 to 6
move 2 from 9 to 2
move 3 from 4 to 3
move 1 from 1 to 9
move 5 from 9 to 8
move 1 from 9 to 5
move 2 from 2 to 6
move 2 from 4 to 6
move 1 from 3 to 7
move 1 from 5 to 6
move 1 from 6 to 7
move 6 from 6 to 8
move 2 from 7 to 5
move 2 from 3 to 2
move 34 from 8 to 1
move 1 from 5 to 6
move 1 from 5 to 3
move 1 from 6 to 1
move 32 from 1 to 8
move 23 from 8 to 4
move 1 from 2 to 1
move 24 from 8 to 4
move 1 from 3 to 6
move 47 from 4 to 6
move 2 from 6 to 1
move 3 from 1 to 5
move 1 from 2 to 1
move 3 from 5 to 7
move 21 from 6 to 2
move 3 from 7 to 8
move 2 from 1 to 6
move 8 from 6 to 4
move 4 from 8 to 9
move 3 from 2 to 8
move 4 from 4 to 2
move 2 from 2 to 5
move 4 from 9 to 8
move 2 from 1 to 5
move 11 from 6 to 1
move 14 from 2 to 6
move 2 from 4 to 3
move 1 from 2 to 9
move 3 from 2 to 9
move 20 from 6 to 5
move 2 from 4 to 2
move 4 from 9 to 1
move 8 from 8 to 9
move 1 from 6 to 9
move 14 from 5 to 2
move 10 from 2 to 7
move 7 from 9 to 6
move 1 from 6 to 8
move 6 from 2 to 6
move 1 from 2 to 5
move 1 from 3 to 5
move 9 from 6 to 3
move 1 from 5 to 2
move 9 from 7 to 3
move 12 from 3 to 2
move 9 from 5 to 9
move 1 from 8 to 6
move 3 from 3 to 5
move 1 from 7 to 6
move 14 from 2 to 6
move 3 from 9 to 7
move 6 from 1 to 2
move 5 from 1 to 8
move 10 from 6 to 9
move 4 from 5 to 6
move 3 from 2 to 4
move 9 from 9 to 7
move 1 from 8 to 7
move 3 from 9 to 6
move 3 from 3 to 7
move 1 from 5 to 1
move 15 from 7 to 1
move 2 from 8 to 5
move 2 from 5 to 4
move 1 from 7 to 4
move 1 from 3 to 1
move 15 from 6 to 7
move 2 from 4 to 9
move 3 from 4 to 7
move 18 from 1 to 6
move 1 from 8 to 9
move 6 from 9 to 7
move 3 from 6 to 8
move 1 from 1 to 2
move 2 from 9 to 5
move 2 from 2 to 9
move 16 from 6 to 3
move 15 from 3 to 7
move 2 from 8 to 4
move 1 from 3 to 7
move 3 from 4 to 9
move 2 from 1 to 9
move 26 from 7 to 4
move 1 from 2 to 1
move 7 from 9 to 8
move 1 from 2 to 5
move 2 from 5 to 2
move 8 from 7 to 5
move 1 from 7 to 3
move 1 from 3 to 9
move 2 from 2 to 7
move 1 from 6 to 4
move 4 from 8 to 9
move 1 from 1 to 3
move 1 from 5 to 6
move 2 from 5 to 7
move 17 from 4 to 9
move 6 from 4 to 9
move 1 from 3 to 4
move 6 from 7 to 9
move 3 from 5 to 6
move 2 from 7 to 9
move 4 from 8 to 9
move 4 from 6 to 4
move 8 from 4 to 6
move 1 from 8 to 4
move 3 from 5 to 2
move 2 from 4 to 3
move 1 from 7 to 9
move 2 from 3 to 5
move 4 from 6 to 9
move 1 from 6 to 1
move 36 from 9 to 4
move 2 from 5 to 3
move 3 from 2 to 1
move 3 from 1 to 4
move 14 from 4 to 1
move 1 from 8 to 5
move 4 from 1 to 3
move 5 from 9 to 5
move 2 from 5 to 8
move 1 from 8 to 9
move 4 from 9 to 6
move 3 from 5 to 8
move 1 from 5 to 6
move 2 from 1 to 6
move 2 from 9 to 7
move 6 from 6 to 4
move 1 from 1 to 3
move 29 from 4 to 6
move 7 from 3 to 4
move 1 from 8 to 9
move 3 from 1 to 6
move 4 from 1 to 4
move 1 from 8 to 4
move 4 from 4 to 3
move 15 from 6 to 8
move 9 from 4 to 9
move 1 from 7 to 9
move 8 from 8 to 3
move 3 from 6 to 7
move 1 from 1 to 2
move 4 from 7 to 6
move 7 from 8 to 5
move 1 from 8 to 4
move 2 from 5 to 7
move 1 from 2 to 4
move 5 from 6 to 1
move 4 from 3 to 2

74
rust/src/day5.rs Normal file
View File

@ -0,0 +1,74 @@
use std::collections::VecDeque;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::str;
pub fn run() {
let file = File::open("resources/day5input.txt").expect("Input file not found.");
let reader = BufReader::new(file);
let mut sorted_crates_part1: Vec<VecDeque<String>> = Vec::new();
let mut sorted_crates_part2: Vec<VecDeque<String>> = Vec::new();
let mut read_state: bool = true;
for line in reader.lines() {
let unwrapped_line = line.unwrap();
if unwrapped_line.len() == 0 {
sorted_crates_part1 = sorted_crates_part2.clone();
read_state = false;
continue;
}
if read_state {
let chunks = unwrapped_line
.as_bytes()
.chunks(4)
.map(str::from_utf8)
.collect::<Result<Vec<&str>, _>>()
.unwrap();
for n in 0..chunks.len() {
if chunks.len() > sorted_crates_part2.len() {
sorted_crates_part2.push(VecDeque::new());
}
if chunks[n].contains("[") {
sorted_crates_part2[n].push_back(chunks[n].to_string());
}
}
} else {
if unwrapped_line.contains("move") {
let args = unwrapped_line.split(" ").collect::<Vec<&str>>();
let num_crates_move = args[1].parse::<usize>().unwrap();
let source_col = args[3].parse::<usize>().unwrap();
let dest_col = args[5].parse::<usize>().unwrap();
for _ in 0..num_crates_move {
let current_crate = sorted_crates_part1[source_col - 1].pop_front().unwrap();
sorted_crates_part1[dest_col - 1].push_front(current_crate);
}
let mut crates: Vec<String> = sorted_crates_part2[source_col - 1]
.drain(0..num_crates_move)
.collect();
crates.reverse();
for c in crates {
sorted_crates_part2[dest_col - 1].push_front(c);
}
}
}
}
let answer1:Vec<String> = sorted_crates_part1
.iter()
.map(|x| x.clone().pop_front().unwrap().trim().replace("[","").replace("]",""))
.collect();
println!("Day 5 Answer 1: {:?}", answer1.join(""));
let answer2:Vec<String> = sorted_crates_part2
.iter()
.map(|x| x.clone().pop_front().unwrap().trim().replace("[","").replace("]",""))
.collect();
println!("Day 5 Answer 2: {:?}", answer2.join(""));
}

View File

@ -4,6 +4,7 @@ mod day1;
mod day2;
mod day3;
mod day4;
mod day5;
fn main() {
let args: Vec<String> = env::args().collect();
@ -15,6 +16,7 @@ fn main() {
"day2" => day2::run(),
"day3" => day3::run(),
"day4" => day4::run(),
"day5" => day5::run(),
_ => eprintln!("error: invalid day")
}
},