Update to Day Four: Try using the parjs library to parse the input, instead of Regex

This commit is contained in:
Lewis Dale committed 2023-12-04 21:54:46 +00:00
1 parent 95c8195e7a
commit 9806b225d1
3 files changed
+49 -7

No files matched your search

+20 -6
View File
@@ -1,6 +1,18 @@
import fs from "fs";
import {Engine} from "./day_three";
import {forEach} from "lodash";
import {whitespace, int, string} from "parjs";
import {between, manySepBy, then} from "parjs/combinators";
const cardIdParser = int()
.pipe(between(whitespace()))
.pipe(between(string('Card'), string(': ')));
const numbersParser = int()
.pipe(between(whitespace()))
.pipe(manySepBy(whitespace()));
const winningNumberParser = numbersParser
.pipe(between(cardIdParser, string('|')))
.pipe(then(numbersParser));
export class Scratchcard {
private readonly numbers: number[];
@@ -8,11 +20,13 @@ export class Scratchcard {
private children: Scratchcard[] = [];
constructor(input: string) {
const [_, withoutId] = input.split(':');
const [winning, ourNumbers] = withoutId.split('|');
const [
winningNumbers,
numbers
] = winningNumberParser.parse(input).value;
this.winningNumbers = this.parseNumberList(winning);
this.numbers = this.parseNumberList(ourNumbers);
this.winningNumbers = winningNumbers;
this.numbers = numbers;
}
private parseNumberList(input: string): number[] {