Update to Day Four: Try using the parjs library to parse the input, instead of Regex
This commit is contained in:
parent
95c8195e7a
commit
9806b225d1
|
@ -18,6 +18,7 @@
|
|||
"typescript": "^5.3.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"lodash": "^4.17.21"
|
||||
"lodash": "^4.17.21",
|
||||
"parjs": "^0.16.1"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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[] {
|
||||
|
|
27
yarn.lock
27
yarn.lock
|
@ -901,6 +901,13 @@ chalk@^4.0.0:
|
|||
ansi-styles "^4.1.0"
|
||||
supports-color "^7.1.0"
|
||||
|
||||
char-info@^0.3.2:
|
||||
version "0.3.2"
|
||||
resolved "https://registry.yarnpkg.com/char-info/-/char-info-0.3.2.tgz#d4c4d034245c864d1ab17152cd31746b3bd4f0d0"
|
||||
integrity sha512-6P6KW8crZx+N857wZalB4FreR+PhheSLmAk22c8zbQsFhsDxZP1aTDfmOjrzddgp1IBOl53b0Z8kDQrwh4B//A==
|
||||
dependencies:
|
||||
node-interval-tree "^1.3.3"
|
||||
|
||||
char-regex@^1.0.2:
|
||||
version "1.0.2"
|
||||
resolved "https://registry.yarnpkg.com/char-regex/-/char-regex-1.0.2.tgz#d744358226217f981ed58f479b1d6bcc29545dcf"
|
||||
|
@ -1819,6 +1826,13 @@ node-int64@^0.4.0:
|
|||
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
|
||||
integrity sha512-O5lz91xSOeoXP6DulyHfllpq+Eg00MWitZIbtPfoSEvqIHdl5gfcY6hYzDWnj0qD5tz52PI08u9qUvSVeUBeHw==
|
||||
|
||||
node-interval-tree@^1.3.3:
|
||||
version "1.3.3"
|
||||
resolved "https://registry.yarnpkg.com/node-interval-tree/-/node-interval-tree-1.3.3.tgz#15ffb904cde08270214acace8dc7653e89ae32b7"
|
||||
integrity sha512-K9vk96HdTK5fEipJwxSvIIqwTqr4e3HRJeJrNxBSeVMNSC/JWARRaX7etOLOuTmrRMeOI/K5TCJu3aWIwZiNTw==
|
||||
dependencies:
|
||||
shallowequal "^1.0.2"
|
||||
|
||||
node-releases@^2.0.13:
|
||||
version "2.0.14"
|
||||
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.14.tgz#2ffb053bceb8b2be8495ece1ab6ce600c4461b0b"
|
||||
|
@ -1876,6 +1890,14 @@ p-try@^2.0.0:
|
|||
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6"
|
||||
integrity sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==
|
||||
|
||||
parjs@^0.16.1:
|
||||
version "0.16.1"
|
||||
resolved "https://registry.yarnpkg.com/parjs/-/parjs-0.16.1.tgz#cce6c1d5cdfc90e2ff8234125af2e3410081d7f2"
|
||||
integrity sha512-/r+iT+Erol+S33m2NcmbDigCeazTkNKZLBzNJFclqrTOMqQUr1IpM8bL7hc7Ai7GrLpbESLlgP6NYoPfO1HFAA==
|
||||
dependencies:
|
||||
char-info "^0.3.2"
|
||||
lodash "^4.17.21"
|
||||
|
||||
parse-json@^5.2.0:
|
||||
version "5.2.0"
|
||||
resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-5.2.0.tgz#c76fc66dee54231c962b22bcc8a72cf2f99753cd"
|
||||
|
@ -1998,6 +2020,11 @@ semver@^7.5.3, semver@^7.5.4:
|
|||
dependencies:
|
||||
lru-cache "^6.0.0"
|
||||
|
||||
shallowequal@^1.0.2:
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/shallowequal/-/shallowequal-1.1.0.tgz#188d521de95b9087404fd4dcb68b13df0ae4e7f8"
|
||||
integrity sha512-y0m1JoUZSlPAjXVtPPW70aZWfIL/dSP7AFkRnniLCrK/8MDKog3TySTBmckD+RObVxH0v4Tox67+F14PdED2oQ==
|
||||
|
||||
shebang-command@^2.0.0:
|
||||
version "2.0.0"
|
||||
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-2.0.0.tgz#ccd0af4f8835fbdc265b82461aaf0c36663f34ea"
|
||||
|
|
Loading…
Reference in New Issue