diff --git a/day05/Cargo.lock b/day05/Cargo.lock new file mode 100644 index 0000000..a626a3b --- /dev/null +++ b/day05/Cargo.lock @@ -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" diff --git a/day05/Cargo.toml b/day05/Cargo.toml new file mode 100644 index 0000000..260644c --- /dev/null +++ b/day05/Cargo.toml @@ -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" diff --git a/day05/src/main.rs b/day05/src/main.rs new file mode 100644 index 0000000..466eb24 --- /dev/null +++ b/day05/src/main.rs @@ -0,0 +1,64 @@ +use std::error; +use std::fs::File; +use std::io::BufRead; +use std::io::BufReader; + +use regex::Regex; + +fn main() -> Result<(), Box> { + let f = File::open("input.txt")?; + let reader = BufReader::new(f); + + let mut lines = reader.lines(); + + let mut items: Vec> = Vec::new(); + + // read the initial state + let mut init_lines: Vec = 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 = re.captures(&line).ok_or("Could not match line")?.iter().skip(1).map(|x| x.unwrap().as_str().parse().unwrap()).collect(); + for _ in 0..cap[0] { + let item = items[cap[1] - 1].pop().ok_or("Found a broken match")?; + 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::>().join("")); + Ok(()) +}