summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock33
-rw-r--r--Cargo.toml3
-rw-r--r--input/day5.txt513
-rw-r--r--input/day5_ex.txt9
-rw-r--r--src/day5.rs65
-rw-r--r--src/main.rs3
6 files changed, 624 insertions, 2 deletions
diff --git a/Cargo.lock b/Cargo.lock
index a1c40dc..7499e33 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3,13 +3,46 @@
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 = "aoc2022"
version = "0.1.0"
dependencies = [
+ "regex",
"simple-error",
]
[[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"
+
+[[package]]
name = "simple-error"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 5db6e4a..ec6e992 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-simple-error = "0.2.3" \ No newline at end of file
+simple-error = "0.2.3"
+regex = "1.7.0" \ No newline at end of file
diff --git a/input/day5.txt b/input/day5.txt
new file mode 100644
index 0000000..23e3650
--- /dev/null
+++ b/input/day5.txt
@@ -0,0 +1,513 @@
+[T] [P] [J]
+[F] [S] [T] [R] [B]
+[V] [M] [H] [S] [F] [R]
+[Z] [P] [Q] [B] [S] [W] [P]
+[C] [Q] [R] [D] [Z] [N] [H] [Q]
+[W] [B] [T] [F] [L] [T] [M] [F] [T]
+[S] [R] [Z] [V] [G] [R] [Q] [N] [Z]
+[Q] [Q] [B] [D] [J] [W] [H] [R] [J]
+ 1 2 3 4 5 6 7 8 9
+
+move 3 from 8 to 2
+move 3 from 1 to 5
+move 3 from 1 to 4
+move 2 from 7 to 4
+move 3 from 7 to 4
+move 8 from 5 to 7
+move 2 from 1 to 8
+move 7 from 3 to 2
+move 1 from 5 to 2
+move 1 from 6 to 7
+move 2 from 5 to 9
+move 1 from 9 to 1
+move 3 from 9 to 6
+move 5 from 6 to 2
+move 10 from 7 to 2
+move 3 from 8 to 9
+move 7 from 9 to 2
+move 1 from 1 to 2
+move 1 from 9 to 6
+move 1 from 4 to 1
+move 1 from 8 to 2
+move 11 from 4 to 2
+move 1 from 7 to 9
+move 1 from 4 to 6
+move 1 from 9 to 7
+move 1 from 1 to 3
+move 1 from 7 to 5
+move 1 from 4 to 9
+move 1 from 5 to 2
+move 1 from 3 to 8
+move 1 from 6 to 9
+move 1 from 8 to 6
+move 11 from 2 to 1
+move 1 from 6 to 8
+move 7 from 2 to 1
+move 14 from 2 to 7
+move 1 from 6 to 3
+move 1 from 8 to 2
+move 1 from 3 to 9
+move 7 from 7 to 1
+move 1 from 6 to 5
+move 5 from 7 to 6
+move 4 from 2 to 8
+move 3 from 6 to 7
+move 3 from 7 to 8
+move 9 from 1 to 3
+move 8 from 3 to 7
+move 1 from 3 to 1
+move 2 from 2 to 3
+move 1 from 6 to 7
+move 2 from 1 to 7
+move 7 from 1 to 6
+move 1 from 3 to 5
+move 2 from 5 to 3
+move 7 from 6 to 3
+move 9 from 7 to 5
+move 1 from 9 to 1
+move 4 from 8 to 5
+move 7 from 1 to 5
+move 4 from 7 to 2
+move 1 from 7 to 8
+move 1 from 6 to 4
+move 10 from 5 to 3
+move 8 from 5 to 1
+move 2 from 8 to 3
+move 2 from 8 to 9
+move 8 from 2 to 7
+move 4 from 9 to 8
+move 13 from 3 to 7
+move 1 from 5 to 3
+move 6 from 3 to 9
+move 10 from 1 to 9
+move 1 from 3 to 4
+move 6 from 9 to 7
+move 1 from 5 to 8
+move 14 from 7 to 6
+move 14 from 6 to 1
+move 13 from 1 to 8
+move 1 from 1 to 2
+move 9 from 8 to 9
+move 6 from 8 to 5
+move 2 from 4 to 6
+move 1 from 8 to 1
+move 2 from 2 to 1
+move 2 from 8 to 6
+move 3 from 1 to 2
+move 3 from 3 to 9
+move 16 from 9 to 1
+move 3 from 2 to 4
+move 3 from 7 to 2
+move 6 from 5 to 4
+move 5 from 7 to 3
+move 4 from 6 to 1
+move 10 from 2 to 9
+move 13 from 9 to 1
+move 5 from 7 to 2
+move 2 from 4 to 6
+move 1 from 9 to 1
+move 2 from 9 to 5
+move 2 from 6 to 8
+move 2 from 5 to 3
+move 1 from 8 to 3
+move 31 from 1 to 7
+move 2 from 1 to 5
+move 12 from 7 to 3
+move 11 from 3 to 2
+move 1 from 8 to 4
+move 6 from 4 to 5
+move 1 from 3 to 4
+move 8 from 3 to 2
+move 5 from 5 to 6
+move 2 from 6 to 7
+move 4 from 7 to 3
+move 1 from 6 to 9
+move 13 from 7 to 6
+move 13 from 2 to 3
+move 1 from 4 to 8
+move 10 from 2 to 3
+move 3 from 7 to 3
+move 2 from 2 to 1
+move 1 from 8 to 2
+move 2 from 4 to 7
+move 1 from 9 to 2
+move 3 from 7 to 3
+move 1 from 5 to 1
+move 2 from 5 to 2
+move 15 from 6 to 7
+move 4 from 1 to 9
+move 22 from 3 to 9
+move 7 from 3 to 9
+move 4 from 3 to 8
+move 4 from 9 to 4
+move 3 from 2 to 4
+move 5 from 7 to 1
+move 7 from 4 to 7
+move 2 from 8 to 4
+move 1 from 4 to 8
+move 3 from 1 to 5
+move 2 from 1 to 4
+move 1 from 2 to 9
+move 2 from 5 to 7
+move 1 from 5 to 9
+move 3 from 8 to 6
+move 8 from 7 to 1
+move 6 from 7 to 1
+move 10 from 1 to 9
+move 3 from 6 to 2
+move 2 from 1 to 3
+move 2 from 3 to 6
+move 3 from 7 to 4
+move 2 from 7 to 1
+move 1 from 2 to 5
+move 13 from 9 to 5
+move 12 from 9 to 3
+move 6 from 5 to 3
+move 2 from 9 to 1
+move 11 from 9 to 3
+move 1 from 4 to 6
+move 2 from 5 to 3
+move 1 from 1 to 8
+move 24 from 3 to 5
+move 2 from 9 to 3
+move 2 from 2 to 4
+move 1 from 9 to 2
+move 2 from 6 to 8
+move 5 from 3 to 5
+move 2 from 8 to 9
+move 1 from 9 to 8
+move 4 from 1 to 4
+move 1 from 9 to 4
+move 1 from 8 to 4
+move 1 from 8 to 4
+move 7 from 4 to 5
+move 1 from 1 to 8
+move 1 from 6 to 5
+move 35 from 5 to 4
+move 18 from 4 to 3
+move 6 from 4 to 3
+move 8 from 5 to 8
+move 8 from 8 to 1
+move 2 from 4 to 9
+move 23 from 3 to 1
+move 1 from 8 to 5
+move 1 from 9 to 1
+move 1 from 5 to 1
+move 1 from 9 to 4
+move 11 from 1 to 2
+move 16 from 4 to 5
+move 3 from 3 to 5
+move 9 from 2 to 5
+move 1 from 4 to 1
+move 2 from 2 to 6
+move 1 from 2 to 9
+move 1 from 6 to 2
+move 1 from 3 to 5
+move 1 from 3 to 9
+move 1 from 2 to 9
+move 23 from 1 to 5
+move 1 from 6 to 9
+move 1 from 9 to 8
+move 27 from 5 to 1
+move 1 from 9 to 3
+move 18 from 5 to 8
+move 6 from 5 to 7
+move 1 from 5 to 6
+move 1 from 9 to 8
+move 12 from 8 to 3
+move 1 from 1 to 4
+move 6 from 7 to 8
+move 1 from 6 to 3
+move 1 from 4 to 2
+move 2 from 1 to 8
+move 1 from 2 to 9
+move 8 from 3 to 2
+move 2 from 9 to 7
+move 5 from 2 to 7
+move 7 from 7 to 2
+move 2 from 8 to 2
+move 3 from 1 to 9
+move 5 from 1 to 2
+move 3 from 9 to 8
+move 3 from 8 to 7
+move 5 from 2 to 5
+move 2 from 7 to 6
+move 12 from 8 to 9
+move 12 from 1 to 4
+move 9 from 9 to 3
+move 4 from 5 to 8
+move 12 from 3 to 8
+move 1 from 7 to 9
+move 3 from 9 to 2
+move 1 from 4 to 7
+move 3 from 1 to 7
+move 7 from 4 to 6
+move 3 from 6 to 2
+move 2 from 7 to 9
+move 18 from 8 to 1
+move 2 from 4 to 7
+move 1 from 2 to 8
+move 1 from 8 to 2
+move 10 from 2 to 3
+move 3 from 9 to 8
+move 2 from 6 to 7
+move 13 from 3 to 1
+move 2 from 8 to 9
+move 28 from 1 to 8
+move 1 from 5 to 2
+move 1 from 4 to 3
+move 4 from 7 to 6
+move 5 from 6 to 7
+move 7 from 2 to 6
+move 1 from 9 to 6
+move 2 from 2 to 4
+move 1 from 9 to 1
+move 4 from 1 to 2
+move 3 from 2 to 5
+move 3 from 4 to 9
+move 3 from 5 to 7
+move 1 from 1 to 4
+move 6 from 7 to 6
+move 1 from 2 to 6
+move 1 from 4 to 1
+move 1 from 1 to 8
+move 3 from 9 to 4
+move 18 from 6 to 3
+move 4 from 3 to 6
+move 1 from 7 to 9
+move 1 from 6 to 9
+move 2 from 3 to 6
+move 1 from 9 to 6
+move 1 from 9 to 2
+move 6 from 6 to 8
+move 3 from 4 to 7
+move 2 from 7 to 2
+move 35 from 8 to 7
+move 3 from 3 to 1
+move 26 from 7 to 2
+move 10 from 3 to 9
+move 6 from 9 to 4
+move 3 from 1 to 2
+move 1 from 4 to 3
+move 4 from 4 to 1
+move 1 from 3 to 6
+move 1 from 8 to 3
+move 1 from 6 to 2
+move 1 from 3 to 2
+move 13 from 7 to 3
+move 3 from 1 to 4
+move 4 from 3 to 1
+move 3 from 1 to 9
+move 2 from 1 to 9
+move 10 from 2 to 9
+move 19 from 2 to 9
+move 6 from 3 to 9
+move 2 from 3 to 4
+move 2 from 2 to 6
+move 17 from 9 to 8
+move 1 from 2 to 8
+move 2 from 9 to 3
+move 2 from 6 to 7
+move 8 from 9 to 3
+move 5 from 4 to 5
+move 14 from 9 to 4
+move 1 from 2 to 3
+move 1 from 7 to 2
+move 2 from 9 to 3
+move 1 from 2 to 7
+move 5 from 5 to 1
+move 1 from 2 to 1
+move 1 from 3 to 1
+move 1 from 9 to 7
+move 3 from 7 to 2
+move 3 from 3 to 7
+move 1 from 2 to 4
+move 1 from 3 to 8
+move 1 from 2 to 4
+move 4 from 3 to 4
+move 16 from 8 to 9
+move 3 from 1 to 4
+move 21 from 4 to 6
+move 1 from 7 to 2
+move 1 from 8 to 2
+move 1 from 1 to 3
+move 6 from 6 to 7
+move 3 from 1 to 9
+move 3 from 7 to 3
+move 1 from 4 to 6
+move 1 from 4 to 7
+move 2 from 2 to 6
+move 1 from 8 to 6
+move 13 from 6 to 7
+move 1 from 2 to 3
+move 15 from 9 to 8
+move 6 from 6 to 3
+move 13 from 8 to 3
+move 4 from 9 to 4
+move 5 from 4 to 8
+move 19 from 3 to 9
+move 3 from 3 to 1
+move 5 from 8 to 9
+move 17 from 9 to 7
+move 1 from 1 to 8
+move 4 from 9 to 6
+move 3 from 3 to 8
+move 1 from 1 to 2
+move 3 from 3 to 1
+move 36 from 7 to 6
+move 1 from 1 to 2
+move 7 from 8 to 2
+move 24 from 6 to 5
+move 2 from 6 to 7
+move 1 from 3 to 2
+move 4 from 6 to 8
+move 19 from 5 to 1
+move 8 from 6 to 4
+move 7 from 2 to 5
+move 3 from 2 to 8
+move 15 from 1 to 6
+move 2 from 9 to 5
+move 2 from 7 to 8
+move 3 from 4 to 1
+move 4 from 5 to 6
+move 1 from 9 to 7
+move 1 from 8 to 3
+move 3 from 6 to 1
+move 2 from 4 to 7
+move 13 from 1 to 8
+move 1 from 3 to 7
+move 1 from 4 to 5
+move 19 from 8 to 6
+move 1 from 7 to 3
+move 8 from 5 to 8
+move 1 from 6 to 8
+move 3 from 5 to 9
+move 1 from 6 to 4
+move 3 from 4 to 7
+move 1 from 3 to 9
+move 4 from 7 to 9
+move 20 from 6 to 3
+move 1 from 8 to 4
+move 2 from 9 to 4
+move 2 from 9 to 2
+move 2 from 9 to 3
+move 13 from 6 to 9
+move 9 from 9 to 8
+move 2 from 6 to 3
+move 8 from 8 to 2
+move 2 from 7 to 3
+move 5 from 9 to 3
+move 12 from 3 to 5
+move 1 from 4 to 7
+move 8 from 2 to 4
+move 8 from 4 to 7
+move 2 from 2 to 6
+move 2 from 8 to 9
+move 2 from 6 to 8
+move 2 from 9 to 6
+move 2 from 6 to 9
+move 2 from 4 to 8
+move 2 from 9 to 2
+move 6 from 3 to 1
+move 2 from 2 to 9
+move 3 from 9 to 3
+move 8 from 7 to 2
+move 6 from 1 to 2
+move 8 from 3 to 8
+move 1 from 7 to 3
+move 5 from 3 to 8
+move 6 from 2 to 7
+move 3 from 7 to 6
+move 2 from 7 to 9
+move 1 from 7 to 8
+move 8 from 5 to 7
+move 7 from 2 to 1
+move 7 from 1 to 6
+move 7 from 7 to 9
+move 1 from 7 to 6
+move 2 from 3 to 9
+move 2 from 8 to 5
+move 25 from 8 to 5
+move 5 from 5 to 1
+move 1 from 6 to 4
+move 17 from 5 to 4
+move 5 from 5 to 4
+move 23 from 4 to 7
+move 2 from 5 to 2
+move 4 from 6 to 3
+move 6 from 3 to 7
+move 1 from 5 to 2
+move 1 from 1 to 7
+move 2 from 2 to 8
+move 2 from 2 to 9
+move 1 from 5 to 7
+move 4 from 1 to 6
+move 2 from 8 to 3
+move 2 from 9 to 4
+move 1 from 4 to 8
+move 7 from 9 to 1
+move 2 from 3 to 5
+move 28 from 7 to 4
+move 4 from 6 to 2
+move 2 from 6 to 2
+move 3 from 7 to 4
+move 2 from 5 to 6
+move 4 from 2 to 6
+move 9 from 6 to 5
+move 4 from 1 to 7
+move 1 from 6 to 2
+move 3 from 2 to 3
+move 1 from 8 to 6
+move 1 from 7 to 4
+move 2 from 3 to 4
+move 1 from 7 to 4
+move 2 from 1 to 6
+move 1 from 7 to 9
+move 1 from 7 to 9
+move 1 from 6 to 2
+move 7 from 5 to 8
+move 1 from 3 to 9
+move 1 from 5 to 2
+move 7 from 8 to 7
+move 4 from 4 to 8
+move 2 from 8 to 4
+move 2 from 2 to 7
+move 1 from 1 to 7
+move 1 from 5 to 6
+move 32 from 4 to 7
+move 2 from 6 to 5
+move 2 from 8 to 2
+move 1 from 2 to 1
+move 2 from 5 to 4
+move 1 from 2 to 5
+move 1 from 1 to 4
+move 4 from 4 to 3
+move 1 from 6 to 4
+move 1 from 5 to 4
+move 5 from 9 to 1
+move 4 from 3 to 5
+move 3 from 1 to 6
+move 2 from 9 to 5
+move 2 from 1 to 3
+move 15 from 7 to 1
+move 5 from 5 to 3
+move 1 from 5 to 2
+move 3 from 4 to 5
+move 2 from 5 to 9
+move 3 from 3 to 6
+move 3 from 3 to 4
+move 1 from 3 to 8
+move 1 from 9 to 3
+move 2 from 4 to 9
+move 1 from 5 to 3
+move 2 from 9 to 6
+move 1 from 8 to 1
+move 1 from 3 to 2
+move 1 from 4 to 9
+move 2 from 9 to 3
+move 9 from 1 to 3
+move 5 from 3 to 4
+move 2 from 1 to 3
+move 4 from 1 to 5
+move 1 from 2 to 8
+move 3 from 4 to 9 \ No newline at end of file
diff --git a/input/day5_ex.txt b/input/day5_ex.txt
new file mode 100644
index 0000000..e98aba4
--- /dev/null
+++ b/input/day5_ex.txt
@@ -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 \ No newline at end of file
diff --git a/src/day5.rs b/src/day5.rs
new file mode 100644
index 0000000..806ddf2
--- /dev/null
+++ b/src/day5.rs
@@ -0,0 +1,65 @@
+use std::{collections::HashMap};
+use regex::Regex;
+
+pub fn run(input: String) {
+ let lines: Vec<&str> = input.lines().collect();
+ let mut lix: usize = 0;
+ let mut ship: HashMap<usize, Vec<char>> = HashMap::new();
+ for line in lines.iter() {
+ lix += 1;
+ let chrs: Vec<char> = line.chars().collect();
+ // Line with just container numbers after the containers themselves
+ if chrs[1] == '1' {
+ break;
+ }
+ for (ix, container) in chrs.chunks(4).enumerate() {
+ let stack = ship.entry(ix+1).or_insert(Vec::new());// key starts at 1, of course
+ let payload = container[1];
+ if payload.is_alphabetic() {
+ stack.insert(0, payload) // last one we found is always the bottom of the stack
+ }
+ }
+ }
+ println!("ship: {:?}", ship);
+ lix += 1;
+
+
+ let re = Regex::new(r"^move (\d+) from (\d+) to (\d+)$").unwrap();
+ assert!(re.is_match("move 1 from 2 to 4"));
+ for line in lines[lix..].iter() {
+ // parse the instruction...
+ // "move 1 from 2 to 1"
+
+ let mm = re.captures(line).expect("instruction didn't match regex???");
+ let n: usize = mm.get(1).unwrap().as_str().parse().unwrap();
+ let ifrom: usize = mm.get(2).unwrap().as_str().parse().unwrap();
+ let ito: usize = mm.get(3).unwrap().as_str().parse().unwrap();
+ println!("moving {} from {} to {}", n, ifrom, ito);
+ // for _ in 0..n {
+ // let stackfrom = ship.get_mut(&ifrom).expect("couldn't find stack to pop from!");
+ // let ch = stackfrom.pop().expect("failed to pop from stack! maybe it was empty?");
+ // let stackto = ship.get_mut(&ito).expect("couldn't find stack to push to!");
+ // stackto.push(ch);
+ // }
+ let stackfrom = ship.get_mut(&ifrom).expect("couldn't find stack to pop from!");
+ let mut chunk: Vec<char> = Vec::new();
+ for _ in 0..n {
+ let ch = stackfrom.pop().expect("failed to pop from stack! maybe it was empty?");
+ chunk.push(ch);
+ }
+ let stackto = ship.get_mut(&ito).expect("couldn't find stack to push to!");
+ for _ in 0..n {
+ let ch = chunk.pop().unwrap();
+ stackto.push(ch);
+ }
+ }
+
+ print!("day 5: ");
+ for i in 1..(ship.keys().len()+1) {
+ let topch = ship.get(&i).unwrap().last().unwrap();
+ print!("{}", topch);
+ }
+ print!("\n");
+
+
+} \ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
index c257999..12aa4b2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,12 +3,13 @@ use std::fs;
mod day1;
mod day2;
mod day3;
-
mod day4;
+mod day5;
fn main() {
day1::run(fs::read_to_string("input/day1.txt").expect("Failed to read input file!"));
day2::run(fs::read_to_string("input/day2.txt").expect("Failed to read input file!"));
day3::run(fs::read_to_string("input/day3.txt").expect("Failed to read input file!"));
day4::run(fs::read_to_string("input/day4.txt").expect("Failed to read input file!"));
+ day5::run(fs::read_to_string("input/day5.txt").expect("Failed to read input file!"));
}