From 4388234565d34d1afc6502db34a85f424fec9a91 Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Wed, 7 Dec 2022 00:59:22 +0300 Subject: 2022, day5: part1 --- 2022/day5/example | 9 + 2022/day5/input | 512 ++++++++++++++++++++++++++++++++++++++++++++ 2022/day5/part1/Cargo.toml | 10 + 2022/day5/part1/src/main.rs | 62 ++++++ 4 files changed, 593 insertions(+) create mode 100644 2022/day5/example create mode 100644 2022/day5/input create mode 100644 2022/day5/part1/Cargo.toml create mode 100644 2022/day5/part1/src/main.rs (limited to '2022') diff --git a/2022/day5/example b/2022/day5/example new file mode 100644 index 0000000..42ef47f --- /dev/null +++ b/2022/day5/example @@ -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 diff --git a/2022/day5/input b/2022/day5/input new file mode 100644 index 0000000..76316f1 --- /dev/null +++ b/2022/day5/input @@ -0,0 +1,512 @@ +[J] [F] [M] +[Z] [F] [G] [Q] [F] +[G] [P] [H] [Z] [S] [Q] +[V] [W] [Z] [P] [D] [G] [P] +[T] [D] [S] [Z] [N] [W] [B] [N] +[D] [M] [R] [J] [J] [P] [V] [P] [J] +[B] [R] [C] [T] [C] [V] [C] [B] [P] +[N] [S] [V] [R] [T] [N] [G] [Z] [W] + 1 2 3 4 5 6 7 8 9 + +move 2 from 4 to 6 +move 1 from 9 to 5 +move 3 from 2 to 4 +move 8 from 4 to 7 +move 2 from 9 to 7 +move 3 from 8 to 3 +move 2 from 1 to 2 +move 5 from 7 to 9 +move 1 from 9 to 4 +move 1 from 8 to 3 +move 1 from 3 to 4 +move 2 from 4 to 9 +move 7 from 3 to 5 +move 6 from 1 to 8 +move 11 from 7 to 9 +move 12 from 5 to 3 +move 6 from 6 to 9 +move 3 from 3 to 8 +move 4 from 2 to 7 +move 3 from 5 to 7 +move 1 from 5 to 7 +move 2 from 2 to 5 +move 1 from 5 to 2 +move 5 from 8 to 9 +move 7 from 7 to 2 +move 3 from 8 to 7 +move 1 from 8 to 9 +move 4 from 3 to 6 +move 1 from 5 to 1 +move 9 from 9 to 6 +move 7 from 9 to 6 +move 20 from 6 to 5 +move 12 from 9 to 8 +move 5 from 5 to 1 +move 3 from 7 to 4 +move 6 from 2 to 7 +move 2 from 3 to 1 +move 4 from 3 to 8 +move 1 from 4 to 1 +move 7 from 7 to 5 +move 4 from 8 to 2 +move 3 from 6 to 2 +move 3 from 2 to 9 +move 4 from 1 to 7 +move 2 from 1 to 2 +move 3 from 9 to 5 +move 11 from 8 to 5 +move 1 from 6 to 9 +move 1 from 8 to 5 +move 1 from 1 to 2 +move 24 from 5 to 4 +move 2 from 1 to 6 +move 11 from 5 to 4 +move 2 from 7 to 9 +move 1 from 6 to 2 +move 4 from 2 to 1 +move 28 from 4 to 2 +move 1 from 7 to 8 +move 9 from 2 to 5 +move 2 from 9 to 6 +move 4 from 4 to 2 +move 1 from 7 to 4 +move 3 from 4 to 7 +move 1 from 6 to 9 +move 21 from 2 to 3 +move 3 from 1 to 6 +move 5 from 6 to 2 +move 7 from 2 to 3 +move 1 from 9 to 3 +move 1 from 8 to 4 +move 1 from 7 to 8 +move 3 from 5 to 8 +move 1 from 1 to 7 +move 2 from 7 to 9 +move 2 from 8 to 4 +move 1 from 9 to 2 +move 1 from 8 to 6 +move 11 from 3 to 4 +move 1 from 7 to 8 +move 6 from 5 to 9 +move 2 from 8 to 7 +move 1 from 6 to 5 +move 7 from 3 to 8 +move 9 from 3 to 6 +move 1 from 8 to 3 +move 1 from 7 to 4 +move 2 from 3 to 5 +move 4 from 5 to 7 +move 4 from 6 to 8 +move 2 from 7 to 9 +move 11 from 4 to 2 +move 1 from 4 to 2 +move 6 from 8 to 9 +move 1 from 7 to 1 +move 1 from 3 to 7 +move 3 from 7 to 8 +move 6 from 8 to 9 +move 6 from 4 to 8 +move 18 from 9 to 3 +move 1 from 5 to 8 +move 5 from 6 to 5 +move 6 from 8 to 1 +move 3 from 5 to 4 +move 1 from 9 to 8 +move 3 from 4 to 8 +move 15 from 3 to 6 +move 2 from 5 to 9 +move 3 from 3 to 1 +move 9 from 6 to 4 +move 2 from 1 to 5 +move 2 from 5 to 8 +move 6 from 4 to 2 +move 6 from 1 to 6 +move 3 from 4 to 6 +move 6 from 9 to 1 +move 4 from 2 to 1 +move 7 from 8 to 1 +move 1 from 6 to 7 +move 17 from 1 to 5 +move 1 from 7 to 1 +move 5 from 2 to 1 +move 1 from 8 to 6 +move 11 from 6 to 4 +move 2 from 2 to 3 +move 3 from 1 to 8 +move 7 from 2 to 5 +move 4 from 6 to 7 +move 4 from 1 to 5 +move 15 from 5 to 9 +move 2 from 3 to 7 +move 2 from 8 to 2 +move 1 from 1 to 9 +move 6 from 2 to 6 +move 7 from 5 to 6 +move 5 from 7 to 3 +move 1 from 6 to 1 +move 2 from 3 to 4 +move 1 from 3 to 4 +move 5 from 6 to 4 +move 14 from 9 to 2 +move 1 from 8 to 9 +move 1 from 7 to 8 +move 1 from 9 to 6 +move 2 from 9 to 5 +move 1 from 1 to 2 +move 7 from 6 to 9 +move 1 from 3 to 4 +move 8 from 5 to 2 +move 1 from 6 to 7 +move 1 from 7 to 4 +move 1 from 8 to 4 +move 1 from 3 to 9 +move 7 from 9 to 5 +move 1 from 9 to 1 +move 6 from 5 to 1 +move 8 from 2 to 4 +move 1 from 5 to 6 +move 1 from 6 to 7 +move 1 from 7 to 9 +move 7 from 2 to 9 +move 1 from 9 to 4 +move 3 from 9 to 1 +move 1 from 9 to 6 +move 11 from 2 to 8 +move 9 from 1 to 8 +move 1 from 6 to 4 +move 1 from 1 to 9 +move 12 from 4 to 2 +move 4 from 9 to 3 +move 3 from 4 to 6 +move 9 from 8 to 6 +move 12 from 4 to 9 +move 8 from 6 to 3 +move 8 from 2 to 7 +move 11 from 3 to 4 +move 2 from 2 to 7 +move 2 from 6 to 1 +move 1 from 2 to 3 +move 2 from 6 to 2 +move 3 from 2 to 6 +move 2 from 1 to 6 +move 1 from 6 to 1 +move 1 from 6 to 4 +move 2 from 6 to 3 +move 1 from 6 to 5 +move 4 from 3 to 8 +move 12 from 4 to 5 +move 5 from 9 to 7 +move 3 from 8 to 7 +move 1 from 9 to 1 +move 3 from 8 to 2 +move 13 from 5 to 6 +move 1 from 2 to 9 +move 13 from 6 to 7 +move 7 from 9 to 6 +move 2 from 4 to 6 +move 1 from 8 to 6 +move 1 from 1 to 6 +move 1 from 2 to 9 +move 1 from 2 to 3 +move 12 from 7 to 9 +move 7 from 8 to 4 +move 1 from 1 to 3 +move 2 from 7 to 9 +move 15 from 7 to 4 +move 8 from 6 to 3 +move 1 from 8 to 9 +move 1 from 7 to 2 +move 10 from 3 to 5 +move 6 from 5 to 9 +move 1 from 2 to 8 +move 1 from 5 to 8 +move 2 from 8 to 9 +move 10 from 4 to 9 +move 20 from 9 to 6 +move 1 from 7 to 6 +move 4 from 9 to 3 +move 1 from 5 to 9 +move 4 from 4 to 9 +move 8 from 9 to 7 +move 2 from 5 to 1 +move 7 from 4 to 3 +move 8 from 3 to 2 +move 6 from 9 to 8 +move 1 from 3 to 7 +move 1 from 3 to 1 +move 7 from 7 to 8 +move 13 from 8 to 3 +move 2 from 2 to 8 +move 1 from 8 to 2 +move 1 from 4 to 1 +move 1 from 1 to 8 +move 2 from 8 to 2 +move 24 from 6 to 2 +move 2 from 7 to 8 +move 5 from 3 to 4 +move 25 from 2 to 6 +move 5 from 4 to 9 +move 2 from 8 to 7 +move 2 from 7 to 3 +move 4 from 6 to 2 +move 2 from 6 to 4 +move 9 from 2 to 3 +move 11 from 3 to 7 +move 10 from 7 to 8 +move 1 from 7 to 9 +move 3 from 2 to 4 +move 8 from 8 to 2 +move 1 from 2 to 6 +move 2 from 4 to 1 +move 1 from 8 to 2 +move 1 from 6 to 9 +move 1 from 8 to 3 +move 6 from 9 to 7 +move 2 from 9 to 1 +move 9 from 6 to 8 +move 7 from 2 to 3 +move 7 from 8 to 2 +move 10 from 6 to 8 +move 7 from 1 to 2 +move 9 from 3 to 2 +move 5 from 3 to 8 +move 4 from 7 to 2 +move 2 from 3 to 2 +move 12 from 2 to 3 +move 6 from 4 to 2 +move 1 from 7 to 6 +move 5 from 3 to 5 +move 16 from 8 to 4 +move 12 from 2 to 7 +move 5 from 5 to 7 +move 1 from 8 to 3 +move 1 from 6 to 4 +move 17 from 7 to 4 +move 1 from 7 to 1 +move 1 from 1 to 9 +move 1 from 9 to 5 +move 11 from 4 to 9 +move 10 from 2 to 3 +move 1 from 5 to 4 +move 1 from 9 to 2 +move 2 from 2 to 1 +move 1 from 2 to 3 +move 23 from 4 to 5 +move 7 from 9 to 7 +move 3 from 9 to 1 +move 20 from 5 to 6 +move 3 from 5 to 8 +move 1 from 4 to 1 +move 2 from 8 to 3 +move 4 from 6 to 4 +move 7 from 7 to 2 +move 1 from 8 to 4 +move 19 from 3 to 9 +move 5 from 1 to 7 +move 7 from 2 to 6 +move 3 from 7 to 5 +move 2 from 3 to 4 +move 1 from 5 to 4 +move 1 from 1 to 4 +move 1 from 7 to 6 +move 13 from 6 to 7 +move 6 from 9 to 3 +move 1 from 3 to 5 +move 2 from 3 to 4 +move 2 from 6 to 2 +move 3 from 4 to 3 +move 8 from 9 to 1 +move 2 from 2 to 1 +move 8 from 6 to 7 +move 2 from 9 to 4 +move 20 from 7 to 1 +move 2 from 7 to 5 +move 2 from 5 to 1 +move 8 from 1 to 8 +move 8 from 8 to 6 +move 1 from 6 to 9 +move 8 from 6 to 1 +move 1 from 5 to 3 +move 7 from 3 to 2 +move 1 from 5 to 2 +move 2 from 9 to 7 +move 1 from 5 to 8 +move 18 from 1 to 4 +move 1 from 8 to 9 +move 3 from 2 to 3 +move 2 from 7 to 4 +move 5 from 2 to 4 +move 3 from 3 to 8 +move 8 from 1 to 7 +move 2 from 9 to 2 +move 32 from 4 to 5 +move 1 from 9 to 7 +move 1 from 2 to 1 +move 6 from 1 to 6 +move 1 from 2 to 4 +move 3 from 8 to 1 +move 3 from 6 to 5 +move 1 from 3 to 6 +move 2 from 1 to 9 +move 4 from 4 to 7 +move 31 from 5 to 4 +move 4 from 5 to 6 +move 1 from 6 to 1 +move 7 from 6 to 5 +move 1 from 9 to 4 +move 19 from 4 to 2 +move 1 from 5 to 9 +move 5 from 5 to 6 +move 3 from 4 to 2 +move 2 from 7 to 1 +move 4 from 7 to 8 +move 3 from 8 to 6 +move 2 from 6 to 7 +move 6 from 7 to 8 +move 3 from 1 to 5 +move 4 from 5 to 9 +move 15 from 2 to 1 +move 4 from 6 to 4 +move 2 from 6 to 3 +move 1 from 3 to 7 +move 4 from 1 to 2 +move 1 from 3 to 4 +move 2 from 7 to 4 +move 5 from 9 to 3 +move 2 from 7 to 3 +move 16 from 4 to 8 +move 8 from 8 to 5 +move 2 from 1 to 5 +move 1 from 9 to 6 +move 1 from 6 to 5 +move 7 from 5 to 9 +move 3 from 1 to 8 +move 1 from 8 to 4 +move 8 from 2 to 7 +move 3 from 1 to 3 +move 1 from 3 to 9 +move 2 from 4 to 2 +move 7 from 8 to 5 +move 7 from 9 to 1 +move 6 from 3 to 5 +move 6 from 7 to 4 +move 3 from 4 to 1 +move 3 from 2 to 5 +move 1 from 7 to 8 +move 1 from 7 to 5 +move 1 from 9 to 8 +move 2 from 2 to 4 +move 15 from 1 to 6 +move 8 from 5 to 9 +move 3 from 3 to 4 +move 4 from 4 to 3 +move 1 from 9 to 7 +move 6 from 9 to 4 +move 1 from 9 to 2 +move 6 from 4 to 9 +move 2 from 4 to 6 +move 5 from 6 to 9 +move 1 from 3 to 1 +move 8 from 6 to 8 +move 12 from 5 to 3 +move 1 from 5 to 3 +move 1 from 3 to 8 +move 4 from 6 to 1 +move 11 from 3 to 8 +move 1 from 2 to 1 +move 23 from 8 to 2 +move 3 from 1 to 2 +move 1 from 1 to 9 +move 2 from 2 to 3 +move 6 from 3 to 6 +move 1 from 7 to 6 +move 1 from 4 to 7 +move 1 from 4 to 3 +move 1 from 7 to 3 +move 4 from 8 to 4 +move 2 from 1 to 8 +move 3 from 8 to 1 +move 4 from 6 to 2 +move 7 from 9 to 1 +move 1 from 9 to 6 +move 2 from 2 to 3 +move 3 from 9 to 4 +move 1 from 9 to 3 +move 10 from 2 to 8 +move 16 from 2 to 5 +move 2 from 3 to 6 +move 6 from 1 to 8 +move 1 from 1 to 5 +move 8 from 8 to 5 +move 11 from 5 to 9 +move 2 from 1 to 8 +move 1 from 1 to 8 +move 4 from 4 to 6 +move 3 from 3 to 9 +move 14 from 9 to 3 +move 15 from 8 to 5 +move 9 from 5 to 4 +move 7 from 6 to 1 +move 1 from 6 to 3 +move 4 from 4 to 7 +move 2 from 6 to 2 +move 4 from 7 to 4 +move 4 from 1 to 4 +move 10 from 4 to 3 +move 14 from 3 to 6 +move 5 from 4 to 1 +move 6 from 5 to 7 +move 1 from 2 to 6 +move 3 from 7 to 2 +move 2 from 2 to 3 +move 3 from 7 to 8 +move 2 from 8 to 2 +move 2 from 2 to 7 +move 6 from 6 to 2 +move 1 from 8 to 7 +move 8 from 2 to 7 +move 1 from 4 to 1 +move 5 from 5 to 3 +move 3 from 3 to 2 +move 5 from 1 to 3 +move 7 from 5 to 8 +move 6 from 6 to 3 +move 1 from 5 to 9 +move 10 from 7 to 9 +move 26 from 3 to 4 +move 1 from 5 to 1 +move 6 from 8 to 2 +move 9 from 2 to 9 +move 1 from 7 to 5 +move 1 from 8 to 5 +move 2 from 6 to 2 +move 20 from 9 to 6 +move 1 from 1 to 6 +move 1 from 4 to 2 +move 1 from 5 to 8 +move 1 from 5 to 7 +move 3 from 1 to 3 +move 1 from 3 to 6 +move 12 from 4 to 8 +move 11 from 4 to 5 +move 1 from 7 to 5 +move 1 from 2 to 8 +move 1 from 1 to 8 +move 2 from 2 to 5 +move 8 from 6 to 2 +move 5 from 6 to 4 +move 2 from 5 to 3 +move 12 from 8 to 4 +move 5 from 2 to 6 +move 3 from 8 to 1 +move 11 from 6 to 8 +move 10 from 4 to 6 +move 5 from 4 to 6 +move 12 from 6 to 5 +move 22 from 5 to 6 +move 3 from 6 to 5 +move 3 from 8 to 5 +move 1 from 3 to 8 +move 4 from 8 to 1 +move 6 from 1 to 7 +move 5 from 6 to 9 diff --git a/2022/day5/part1/Cargo.toml b/2022/day5/part1/Cargo.toml new file mode 100644 index 0000000..a479178 --- /dev/null +++ b/2022/day5/part1/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "part1" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.10.5" +regex = "1.7.0" diff --git a/2022/day5/part1/src/main.rs b/2022/day5/part1/src/main.rs new file mode 100644 index 0000000..f64fcce --- /dev/null +++ b/2022/day5/part1/src/main.rs @@ -0,0 +1,62 @@ +use itertools::Itertools; +use regex::Regex; +use std::collections::LinkedList; + +fn main() { + let input: &str = include_str!("../../input"); + + let width_finder = Regex::new(r"(?m)^\s1.*(\d)\s$").unwrap(); + let mut width = 0; + + for cap in width_finder.captures_iter(input) { + width = str::parse(&cap[1]).unwrap(); + } + + let mut crate_builder: Vec> = Vec::new(); + + for _ in 0..width { + crate_builder.push(LinkedList::new()); + } + + for line in input.lines() { + let mut idx = 0; + if line.contains("1") { + break; + } + + for part in &line.chars().chunks(4) { + let crt: String = part.collect(); + if !crt.contains("[") { + idx += 1; + } else { + let crate_ = crt.chars().nth(1).unwrap(); + crate_builder[idx].push_back(crate_); + idx += 1; + } + } + } + + let move_parser = Regex::new(r"^move\s(\d+)\sfrom\s(\d)\sto\s(\d)").unwrap(); + + for mv in input.lines().skip_while(|l| !l.contains("move")) { + for cap in move_parser.captures_iter(mv) { + let how_many = str::parse(&cap[1]).unwrap(); + let from: usize = str::parse(&cap[2]).unwrap(); + let to: usize = str::parse(&cap[3]).unwrap(); + + for _ in 0..how_many { + if let Some(box_) = crate_builder[from - 1].pop_front() { + crate_builder[to - 1].push_front(box_); + } + } + } + } + + println!( + "{}", + crate_builder + .iter() + .filter_map(|s| s.front()) + .collect::() + ); +} -- cgit v1.2.3-70-g09d2