-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.ts
78 lines (63 loc) · 2.31 KB
/
day4.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
import { assertEquals } from "https://deno.land/std@0.208.0/assert/mod.ts";
import { loadTestData, loadData } from "./utils.ts";
const part1test = 13;
const part2test = 30;
interface Card {
winningNumbers: Set<number>,
yourNumbers: Set<number>,
id: number,
}
const checkWins = (card: Card): number[] => {
const winnings = new Set(
Array.from(card.winningNumbers).filter(x => card.yourNumbers.has(x))
).size;
return [...Array(winnings).keys()].map(num => num + card.id + 1);
}
const part1 = (data: string) => {
let sum = 0;
const lines = data.split("\n").map(line => line.split(": ")[1]) // we don't need the card number
const numbers = lines.map(
line => line.split("|").map(
numList => numList.trim().split(" ").map(
num => parseInt(num.trim())).filter(
num => !isNaN(num))).map(
numList => new Set(numList)));
const winningNumbers = numbers.map(
(item: Set<number>[]) => new Set(
Array.from(item[0]).filter(x => item[1].has(x))
));
const scores = winningNumbers.map(wins =>
Math.floor(Math.pow(2, wins.size - 1))
);
for (const score of scores) {
sum += score;
}
return sum;
};
const part2 = (data: string) => {
const lines = data.split("\n").map(line => line.split(": ")[1]) // we don't need the card number
const numbers = lines.map(
line => line.split("|").map(
numList => numList.trim().split(" ").map(
num => parseInt(num.trim())).filter(
num => !isNaN(num))).map(
numList => new Set(numList)));
let id = 0;
const cards: Card[] = numbers.map(([winningNumbers, yourNumbers]) => ({
winningNumbers, yourNumbers,
id: id++
}));
const winningsMap = cards.map(card => checkWins(card).map(id => cards[id]))
let count = cards.length;
let hand = cards;
while (hand.length > 0) {
const winnings = hand.flatMap(card => winningsMap[card.id]);
count += winnings.length;
hand = winnings;
}
return count;
};
assertEquals(part1(await loadTestData(4)), part1test)
console.log(part1(await loadData(4)))
assertEquals(part2(await loadTestData(4)), part2test)
console.log(part2(await loadData(4)))