Skip to content

Commit

Permalink
move to 2022 folder
Browse files Browse the repository at this point in the history
  • Loading branch information
MellKam committed Dec 6, 2022
1 parent d7b4f14 commit ed648a0
Show file tree
Hide file tree
Showing 22 changed files with 84 additions and 83 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion day_1/rust/Cargo.toml → 2022/day_1/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "day_1_rust"
name = "rust_2022_01"
version = "0.1.0"
edition = "2021"

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion day_2/rust/Cargo.toml → 2022/day_2/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "day_2_rust"
name = "rust_2022_02"
version = "0.1.0"
edition = "2021"

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion day_3/rust/Cargo.toml → 2022/day_3/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "day_3_rust"
name = "rust_2022_03"
version = "0.1.0"
edition = "2021"

Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion day_4/rust/Cargo.toml → 2022/day_4/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "day_4_rust"
name = "rust_2022_04"
version = "0.1.0"
edition = "2021"

Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions 2022/day_5/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "rust_2022_05"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub struct CratesLineIterator<'a> {
}

impl<'a> CratesLineIterator<'a> {
fn skip_by(&mut self, n: usize) -> Result<(), usize> {
fn advance(&mut self, n: usize) -> Result<(), usize> {
for i in 0..n {
self.iter.next().ok_or(i)?;
}
Expand All @@ -25,27 +25,23 @@ impl Iterator for CratesLineIterator<'_> {
type Item = (usize, char);

fn next(&mut self) -> Option<Self::Item> {
if let Err(_) = self.skip_by(1) {
if let Err(_) = self.advance(1) {
return None;
}
let optional_char = self.iter.next();
let current_char = self.iter.next()?;

if let Err(size) = self.skip_by(2) {
if let Err(size) = self.advance(2) {
if size != 1 {
return None;
}
}

return match optional_char {
Some(' ') => {
self.stack_index += 1;
return self.next();
}
Some(c) => {
self.stack_index += 1;
Some((self.stack_index as usize, c))
}
None => return None,
};
self.stack_index += 1;

if current_char == ' ' {
return self.next();
}

return Some((self.stack_index as usize, current_char));
}
}
28 changes: 28 additions & 0 deletions 2022/day_5/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
mod crates_iter;
mod procedure;
mod supply_stacks;

use procedure::Procuedure;
use supply_stacks::SupplyStacks;

fn main() {
let data = include_str!("../../input.txt");

let (string_stacks, procedures_str) = data.split_once("\n\n").unwrap();

let mut stacks = SupplyStacks::new(string_stacks);

let procedures = procedures_str
.lines()
.map(|line| Procuedure::from(line))
.collect::<Vec<Procuedure>>();

for proc in procedures {
stacks.apply_procedure(&proc, false);
}

let highest_vec = stacks.get_highest_elements();
let highest_str = highest_vec.iter().collect::<String>();

println!("{highest_str}");
}
18 changes: 18 additions & 0 deletions 2022/day_5/rust/src/procedure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[derive(Debug)]
pub struct Procuedure {
pub move_from: usize,
pub move_to: usize,
pub crates_count: u32,
}

impl From<&str> for Procuedure {
fn from(string: &str) -> Self {
let items = string.split(" ").collect::<Vec<&str>>();

return Procuedure {
crates_count: items[1].parse::<u32>().unwrap(),
move_from: items[3].parse::<usize>().unwrap(),
move_to: items[5].parse::<usize>().unwrap(),
};
}
}
48 changes: 3 additions & 45 deletions day_5/rust/src/main.rs → 2022/day_5/rust/src/supply_stacks.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
mod crates_iter;

use crates_iter::CratesLineIterator;

#[derive(Debug)]
struct Procuedure {
pub move_from: usize,
pub move_to: usize,
pub crates_count: u32,
}
use crate::{crates_iter::CratesLineIterator, procedure::Procuedure};

#[derive(Debug)]
struct SupplyStacks {
pub struct SupplyStacks {
stacks: Vec<Vec<char>>,
}

Expand Down Expand Up @@ -39,7 +30,7 @@ impl SupplyStacks {
}
}

pub fn apply_procedure(&mut self, procedure: Procuedure, save_order: bool) {
pub fn apply_procedure(&mut self, procedure: &Procuedure, save_order: bool) {
if procedure.move_from == procedure.move_to {
return;
}
Expand Down Expand Up @@ -76,36 +67,3 @@ impl SupplyStacks {
return result;
}
}

fn main() {
let data = include_str!("../../input.txt");

let (string_stacks, procedures_str) = match data.split_once("\n\n") {
Some((a, b)) => (a, b),
_ => panic!(),
};

let mut stacks = SupplyStacks::new(string_stacks);

let procedures = procedures_str
.lines()
.map(|line| {
let items = line.split(" ").collect::<Vec<&str>>();

return Procuedure {
crates_count: items[1].parse::<u32>().unwrap(),
move_from: items[3].parse::<usize>().unwrap(),
move_to: items[5].parse::<usize>().unwrap(),
};
})
.collect::<Vec<Procuedure>>();

for proc in procedures {
stacks.apply_procedure(proc, true);
}

let highest_vec = stacks.get_highest_elements();
let highest_str = highest_vec.iter().collect::<String>();

println!("{highest_str}");
}
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[workspace]
resolver = "2"
members = [
"day_1/rust",
"day_2/rust",
"day_3/rust",
"day_4/rust",
"day_5/rust"
]
"2022/day_1/rust",
"2022/day_2/rust",
"2022/day_3/rust",
"2022/day_4/rust",
"2022/day_5/rust"
]
exclude = [ "2022" ]
8 changes: 0 additions & 8 deletions day_5/rust/Cargo.toml

This file was deleted.

0 comments on commit ed648a0

Please sign in to comment.