starting on day13, parser is halfway done

This commit is contained in:
Joe Ardent 2022-12-26 17:09:04 -08:00
parent 0423e7a8f3
commit 24feb1c127
3 changed files with 120 additions and 0 deletions

View file

@ -12,3 +12,4 @@ lazy_static = "1.4.0"
regex = "1.7"
ndarray = "0.15"
petgraph = "0.6"
lyn = "0.1.0"

118
2022-aoc/src/d13.rs Normal file
View file

@ -0,0 +1,118 @@
use aoc_runner_derive::{aoc as aoc_run, aoc_generator};
use lyn::*;
/*
<packet> ::= <leftbrace> <list>? <rightbrace>
<list> ::= <single> (<comma> <single>)*
<single> ::= <number> | <leftbrace> <list>* <rightbrace>
<comma> ::= "," | ", "
<leftbrace> ::= "["
<rightbrace> ::= "]"
<number> ::= [0-9]+
*/
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
struct Packet {
elements: Vec<Element>,
}
#[derive(Debug, PartialEq, Eq, Clone)]
enum Element {
Plain(u32),
List(Vec<Element>),
}
impl PartialOrd for Element {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
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<Packet> {
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<Element> {
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);
}
}

View file

@ -12,5 +12,6 @@ mod d9;
mod d10;
mod d11;
mod d12;
mod d13;
aoc_lib! { year = 2022 }