-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.ts
86 lines (78 loc) · 2.56 KB
/
solver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { PuzzleSolver } from "../types/puzzle-solver.ts";
type ScratchCard = {
id: number;
winningNumbers: number[];
numbers: number[];
};
export default class Solver23D4 implements PuzzleSolver {
private cards: ScratchCard[] = [];
private winCountCache = new Map<number, number>();
parseInput(input: string[]) {
this.cards = input
.filter((line) => !!line)
.map((line) => {
const { groups } =
line.match(
/Card (?<id>[\s\d]+): (?<winningNumbers>[\d\s]+)\| (?<numbers>[\d\s]+)/
) ?? {};
return {
id: Number(groups?.id.trim()),
winningNumbers:
groups?.winningNumbers
.split(" ")
.filter((n) => !!n && n !== " ")
.map(Number) ?? [],
numbers:
groups?.numbers
.split(" ")
.filter((n) => !!n && n !== " ")
.map(Number) ?? [],
};
});
}
solvePart1() {
let sum = 0;
for (let i = 0; i < this.cards.length; i++) {
sum += this.getCardPoints(this.cards[i]);
}
return sum;
}
solvePart2() {
let sum = 0;
for (let i = 0; i < this.cards.length; i++) {
sum += this.getNumberOfCards(i);
}
return sum;
}
private getNumberOfCards(cardIndex: number): number {
if (cardIndex >= this.cards.length) {
return 0;
}
const winCount = this.getWinCount(this.cards[cardIndex]);
let numberOfCards = 0;
for (let i = cardIndex + 1; i <= cardIndex + winCount; i++) {
numberOfCards += this.getNumberOfCards(i);
}
return numberOfCards + 1;
}
private getCardPoints(card: ScratchCard): number {
const winCount = this.getWinCount(card);
if (winCount === 0) {
return 0;
}
return 2 ** (winCount - 1);
}
public getWinCount(card: ScratchCard): number {
if (this.winCountCache.has(card.id)) {
return this.winCountCache.get(card.id) ?? 0;
}
const winCount = card.numbers.reduce((acc, number) => {
if (card.winningNumbers.includes(number)) {
acc++;
}
return acc;
}, 0);
this.winCountCache.set(card.id, winCount);
return winCount;
}
}