Move Day 5 to its own crate
This commit is contained in:
parent
bb4dd6fb41
commit
b905d7f7fd
4 changed files with 27 additions and 12 deletions
7
2022/day5/Cargo.lock
generated
Normal file
7
2022/day5/Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day5"
|
||||||
|
version = "0.1.0"
|
8
2022/day5/Cargo.toml
Normal file
8
2022/day5/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "day5"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -1,7 +1,6 @@
|
||||||
|
use std::env;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io::Result;
|
use std::fs;
|
||||||
|
|
||||||
use crate::file::line_reader_for_file;
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
enum State {
|
enum State {
|
||||||
|
@ -111,19 +110,22 @@ impl TryFrom<&str> for Instruction {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main(filename: &str) -> Result<()> {
|
fn main() {
|
||||||
let mut line_reader = line_reader_for_file(filename)?.peekable();
|
let args: Vec<String> = env::args().collect();
|
||||||
|
|
||||||
let first_line = line_reader.peek().unwrap().as_ref().unwrap();
|
let filename = args.get(1).expect("Missing filename argument");
|
||||||
|
|
||||||
|
let file_contents = fs::read_to_string(&filename).expect("Unable to read file");
|
||||||
|
let mut lines = file_contents.lines().peekable();
|
||||||
|
|
||||||
|
let first_line = lines.peek().unwrap();
|
||||||
let number_of_stacks = (first_line.len() as f32 / 4.0).ceil() as usize;
|
let number_of_stacks = (first_line.len() as f32 / 4.0).ceil() as usize;
|
||||||
let mut part1_stacks = Stacks(vec![vec![]; number_of_stacks]);
|
let mut part1_stacks = Stacks(vec![vec![]; number_of_stacks]);
|
||||||
let mut part2_stacks = Stacks(vec![vec![]; number_of_stacks]);
|
let mut part2_stacks = Stacks(vec![vec![]; number_of_stacks]);
|
||||||
|
|
||||||
let mut state = State::StartingState;
|
let mut state = State::StartingState;
|
||||||
|
|
||||||
for line in line_reader {
|
for line in lines {
|
||||||
let line = line?;
|
|
||||||
|
|
||||||
if line.is_empty() {
|
if line.is_empty() {
|
||||||
assert!(state == State::StartingState);
|
assert!(state == State::StartingState);
|
||||||
state = State::Instructions;
|
state = State::Instructions;
|
||||||
|
@ -146,7 +148,7 @@ pub fn main(filename: &str) -> Result<()> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
State::Instructions => {
|
State::Instructions => {
|
||||||
let instruction = Instruction::try_from(line.as_str()).unwrap();
|
let instruction = Instruction::try_from(line).unwrap();
|
||||||
let _ = part1_stacks.part1_perform(&instruction);
|
let _ = part1_stacks.part1_perform(&instruction);
|
||||||
let _ = part2_stacks.part2_perform(&instruction);
|
let _ = part2_stacks.part2_perform(&instruction);
|
||||||
}
|
}
|
||||||
|
@ -163,6 +165,4 @@ pub fn main(filename: &str) -> Result<()> {
|
||||||
"Part 2: tops of stacks: {}",
|
"Part 2: tops of stacks: {}",
|
||||||
part2_stacks.tops().collect::<Vec<&str>>().join("")
|
part2_stacks.tops().collect::<Vec<&str>>().join("")
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue