summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2022-12-05 22:46:27 +0000
committerMartin Ashby <martin@ashbysoft.com>2022-12-05 22:46:27 +0000
commit4fcbe72a62529745a85011552ae1a6659d770f93 (patch)
tree2bc394ad69d605702440cb0eb8cdcf910536efee /src
parent11a3e07268362bc1ee61b9d3f5fa6db6e12ac707 (diff)
downloadaoc2022-4fcbe72a62529745a85011552ae1a6659d770f93.tar.gz
aoc2022-4fcbe72a62529745a85011552ae1a6659d770f93.tar.bz2
aoc2022-4fcbe72a62529745a85011552ae1a6659d770f93.tar.xz
aoc2022-4fcbe72a62529745a85011552ae1a6659d770f93.zip
day5
Not super happy with this, fell back to iterative programming pretty heavily (: Also a ton of .unwrap(), but this is an artifical problem not a real world
Diffstat (limited to 'src')
-rw-r--r--src/day5.rs65
-rw-r--r--src/main.rs3
2 files changed, 67 insertions, 1 deletions
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!"));
}