From 24feb1c12724dd2c3bcb55f81dd161ed0a256db0 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Mon, 26 Dec 2022 17:09:04 -0800 Subject: [PATCH] starting on day13, parser is halfway done --- 2022-aoc/Cargo.toml | 1 + 2022-aoc/src/d13.rs | 118 ++++++++++++++++++++++++++++++++++++++++++++ 2022-aoc/src/lib.rs | 1 + 3 files changed, 120 insertions(+) create mode 100644 2022-aoc/src/d13.rs diff --git a/2022-aoc/Cargo.toml b/2022-aoc/Cargo.toml index ea7401a..3677a93 100644 --- a/2022-aoc/Cargo.toml +++ b/2022-aoc/Cargo.toml @@ -12,3 +12,4 @@ lazy_static = "1.4.0" regex = "1.7" ndarray = "0.15" petgraph = "0.6" +lyn = "0.1.0" diff --git a/2022-aoc/src/d13.rs b/2022-aoc/src/d13.rs new file mode 100644 index 0000000..0d9f436 --- /dev/null +++ b/2022-aoc/src/d13.rs @@ -0,0 +1,118 @@ +use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; +use lyn::*; + +/* + ::= ? + ::= ( )* + ::= | * + ::= "," | ", " + ::= "[" + ::= "]" + ::= [0-9]+ +*/ + +#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)] +struct Packet { + elements: Vec, +} + +#[derive(Debug, PartialEq, Eq, Clone)] +enum Element { + Plain(u32), + List(Vec), +} + +impl PartialOrd for Element { + fn partial_cmp(&self, other: &Self) -> Option { + use Element::*; + match (self, other) { + (Plain(v1), Plain(v2)) => Some(v1.cmp(v2)), + (List(v1), List(v2)) => v1.partial_cmp(v2), + (Plain(_), List(_)) => List(vec![self.clone()]).partial_cmp(other), + (List(_), Plain(_)) => self.partial_cmp(&List(vec![other.clone()])), + } + } +} + +impl Ord for Element { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.partial_cmp(other).unwrap() + } +} + +#[aoc_generator(day13)] +fn parse_input(input: &str) -> Vec { + let mut out = Vec::new(); + for line in input.lines() { + let mut scanner = Scanner::new(line); + let packet = parse_packet(&mut scanner); + out.push(packet); + } + + out +} + +fn parse_packet(scanner: &mut Scanner) -> Packet { + // we can pop the first character off, it's the opening brace + scanner.pop(); + let elements = parse_list(scanner); + // pop off the last character, the final closing brace + assert!(scanner.pop().is_some()); + assert!(scanner.is_done()); + Packet { elements } +} + +fn parse_list(scanner: &mut Scanner) -> Vec { + todo!() +} + +#[aoc_run(day13, part1)] +fn part1(input: &[Packet]) -> u32 { + 0 +} + +#[aoc_run(day13, part2)] +fn part2(input: &[Packet]) -> u32 { + 0 +} + +#[cfg(test)] +mod test { + use super::*; + const INPUT: &str = "[1,1,3,1,1] +[1,1,5,1,1] + +[[1],[2,3,4]] +[[1],4] + +[9] +[[8,7,6]] + +[[4,4],4,4] +[[4,4],4,4,4] + +[7,7,7,7] +[7,7,7] + +[] +[3] + +[[[]]] +[[]] + +[1,[2,[3,[4,[5,6,7]]]],8,9] +[1,[2,[3,[4,[5,6,0]]]],8,9]"; + + #[test] + fn part1_test() { + let v = parse_input(INPUT); + + assert_eq!(part1(&v), 1); + } + + #[test] + fn part2_test() { + let v = parse_input(INPUT); + assert_eq!(part2(&v), 1); + } +} diff --git a/2022-aoc/src/lib.rs b/2022-aoc/src/lib.rs index 769a7ff..7ad04ef 100644 --- a/2022-aoc/src/lib.rs +++ b/2022-aoc/src/lib.rs @@ -12,5 +12,6 @@ mod d9; mod d10; mod d11; mod d12; +mod d13; aoc_lib! { year = 2022 }