From 6aaf365868c220bbf0af9279d913772bb461fcf7 Mon Sep 17 00:00:00 2001 From: Maciej Jur Date: Mon, 5 Dec 2022 11:18:10 +0100 Subject: [PATCH] 2022 rust day 5 parsing --- 2022/rust/src/solutions/day05.rs | 70 ++++++++++++++++++++++++++++---- 2022/rust/src/solutions/day06.rs | 46 +++++++++++++++++++++ 2022/rust/src/solutions/mod.rs | 1 + 3 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 2022/rust/src/solutions/day06.rs diff --git a/2022/rust/src/solutions/day05.rs b/2022/rust/src/solutions/day05.rs index e06744b..e772f23 100644 --- a/2022/rust/src/solutions/day05.rs +++ b/2022/rust/src/solutions/day05.rs @@ -1,5 +1,7 @@ +use regex::Regex; use crate::utils; +type Data = (Vec>, Vec<(i32, i32, i32)>); pub fn run() -> () { let data = parse_data(utils::read_lines(utils::Source::Scratch)); @@ -10,17 +12,58 @@ pub fn run() -> () { } -fn solve1(data: &()) -> i32 { +fn solve1(data: &Data) -> i32 { 1 } -fn solve2(data: &()) -> i32 { +fn solve2(data: &Data) -> i32 { 2 } -fn parse_data(data: Vec) -> () { - () +fn parse_data(data: Vec) -> Data { + let re = Regex::new("( {3}|[\\[\\w\\]]{3})").unwrap(); + let iter = data.iter(); + let mut boxes = iter + .map_while(|s| { + let cap = re.find_iter(s) + .map(|x| x.as_str().trim()) + .enumerate() + .filter_map(|(idx, str)| match str.len() { + 0 => None, + _ => Some((idx, str.chars().nth(1).unwrap())) + }) + .collect::>(); + if cap.is_empty() { None } else { Some(cap) } + }) + .collect::>() + .into_iter() + .rev(); + let stacks = boxes.next() + .map(|bottom| { + let stacks = bottom.into_iter() + .map(|(_, c)| vec![c]) + .collect::>(); + boxes.fold(stacks, |mut acc, next| { + next.into_iter().for_each(|(i, c)| + acc[i].push(c) + ); + acc + }) + }) + .unwrap(); + + let re = Regex::new("^move (\\d+) from (\\d+) to (\\d+)$").unwrap(); + let actions = data.iter() + .filter_map(|str| re.captures(str)) + .map(|cap| ( + cap.get(1).unwrap().as_str().parse().unwrap(), + cap.get(2).unwrap().as_str().parse().unwrap(), + cap.get(3).unwrap().as_str().parse().unwrap(), + )) + .collect(); + + (stacks, actions) } @@ -28,19 +71,30 @@ fn parse_data(data: Vec) -> () { mod tests { use super::*; - fn data() -> Vec<&'static str> { - vec![""] + fn data() -> Vec { + vec![ + " [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", + ] + .into_iter().map(String::from).collect() } #[test] fn part1() { - let data = parse_data(data().into_iter().map(String::from).collect()); + let data = parse_data(data()); assert_eq!(1, solve1(&data)); } #[test] fn part2() { - let data = parse_data(data().into_iter().map(String::from).collect()); + let data = parse_data(data()); assert_eq!(2, solve2(&data)); } } diff --git a/2022/rust/src/solutions/day06.rs b/2022/rust/src/solutions/day06.rs new file mode 100644 index 0000000..e06744b --- /dev/null +++ b/2022/rust/src/solutions/day06.rs @@ -0,0 +1,46 @@ +use crate::utils; + + +pub fn run() -> () { + let data = parse_data(utils::read_lines(utils::Source::Scratch)); + + println!("Day 5"); + println!("Part 1: {}", solve1(&data)); + println!("Part 2: {}", solve2(&data)); +} + + +fn solve1(data: &()) -> i32 { + 1 +} + +fn solve2(data: &()) -> i32 { + 2 +} + + +fn parse_data(data: Vec) -> () { + () +} + + +#[cfg(test)] +mod tests { + use super::*; + + fn data() -> Vec<&'static str> { + vec![""] + } + + #[test] + fn part1() { + let data = parse_data(data().into_iter().map(String::from).collect()); + assert_eq!(1, solve1(&data)); + } + + #[test] + fn part2() { + let data = parse_data(data().into_iter().map(String::from).collect()); + assert_eq!(2, solve2(&data)); + } +} diff --git a/2022/rust/src/solutions/mod.rs b/2022/rust/src/solutions/mod.rs index f290640..d945a75 100644 --- a/2022/rust/src/solutions/mod.rs +++ b/2022/rust/src/solutions/mod.rs @@ -3,3 +3,4 @@ pub mod day02; pub mod day03; pub mod day04; pub mod day05; +pub mod day06;