aoc-2023/day_5/parser.ts

25 lines
604 B
TypeScript
Raw Permalink Normal View History

2024-12-09 22:41:02 +09:00
import nearly from "npm:nearley";
import "./grammar.js";
// deno-lint-ignore no-explicit-any
const grammar = (window as unknown as any)["grammar"];
const compiled_grammar = nearly.Grammar.fromCompiled(grammar);
export type MappingData = {
dst: number,
src: number,
range: number
}
export type MapType = { from: string, to: string, data: MappingData[] }
export type InputType = {
seeds: number[],
map: MapType[]
}
export function parseInput(input: string): InputType {
const parser = new nearly.Parser(compiled_grammar);
parser.feed(input);
return parser.results[0];
}