Compare commits

..

2 Commits

Author SHA1 Message Date
MasterofJOKers f083a70ae5 day05: Changes for part 2
I feel like there should be away for me to read from the back and move
things over directly instead of having a temporary Vec for it, but I'm
not sure how this would work with the owner-checking, so I didn't try.
2022-12-18 18:31:23 +01:00
MasterofJOKers 152773a0a9 day05: Implement part 1 2022-12-18 18:26:03 +01:00
3 changed files with 120 additions and 0 deletions

42
day05/Cargo.lock generated Normal file
View File

@ -0,0 +1,42 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
dependencies = [
"memchr",
]
[[package]]
name = "day05"
version = "0.1.0"
dependencies = [
"regex",
]
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "regex"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"

9
day05/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "day05"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1.7.0"

69
day05/src/main.rs Normal file
View File

@ -0,0 +1,69 @@
use std::error;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use regex::Regex;
fn main() -> Result<(), Box<dyn error::Error>> {
let f = File::open("input.txt")?;
let reader = BufReader::new(f);
let mut lines = reader.lines();
let mut items: Vec<Vec<&str>> = Vec::new();
// read the initial state
let mut init_lines: Vec<String> = vec![];
while let Some(Ok(line)) = lines.next() {
if line == "" {
break;
}
init_lines.push(line);
}
init_lines.reverse();
let mut count = 0;
for (i, line) in init_lines.iter().enumerate() {
// init the items with the right amount of Vecs
if i == 0 {
count = line.split_whitespace().count();
for _ in 0..count {
items.push(vec![]);
}
continue;
}
// add the items to the vecs
// let line_iter = line.chars();
for i in 0..count {
let item = &line[(i * 4 + 1)..(i * 4 + 2)];
if item == " " {
continue;
}
items[i].push(&item);
}
}
// read the movements
let re = Regex::new(r"^move (\d+) from (\d+) to (\d+)$")?;
while let Some(Ok(line)) = lines.next() {
// println!("{:?}", re.captures(&line));
// println!("{:?}", items);
let cap: Vec<usize> = re.captures(&line).ok_or("Could not match line")?.iter().skip(1).map(|x| x.unwrap().as_str().parse().unwrap()).collect();
let mut items_to_move = vec![];
for _ in 0..cap[0] {
let item = items[cap[1] - 1].pop().ok_or("Found a broken match")?;
items_to_move.push(item);
}
for item in items_to_move.iter().rev() {
items[cap[2] - 1].push(item);
}
}
// output the result (top of each item-stack i.e. end of the Vec)
// println!("{:?}", items);
println!("{}", items.iter().map(|v| String::from(*v.iter().last().unwrap())).collect::<Vec<_>>().join(""));
Ok(())
}