forked from tpatel/advent-of-code-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04.js
131 lines (115 loc) · 2.98 KB
/
day04.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const fs = require("fs");
const lines = fs
.readFileSync("day04.txt", { encoding: "utf-8" })
.split("\n\n")
.filter((x) => Boolean(x))
.map((x) =>
x
.replace(/[\n ,]+/g, " ")
.trim()
.split(" ")
.map((y) => parseInt(y))
);
let [drawnNumbers, ...cards] = lines;
class Card {
constructor(numbers) {
this.cardSize = 5;
this.numbers = numbers;
this.numberToPosition = new Map();
for (let i = 0; i < this.numbers.length; i++) {
const n = this.numbers[i];
this.numberToPosition.set(n, {
line: Math.floor(i / this.cardSize),
column: i % this.cardSize,
});
}
this.lines = Array(this.cardSize).fill(0);
this.columns = Array(this.cardSize).fill(0);
this.isComplete = false;
this.markedNumbers = new Set();
}
addMarkedNumber(number) {
const position = this.numberToPosition.get(number);
if (!position) {
return;
}
this.markedNumbers.add(number);
this.lines[position.line]++;
this.columns[position.column]++;
if (
this.lines[position.line] === this.cardSize ||
this.columns[position.column] === this.cardSize
) {
this.isComplete = true;
}
}
unmarkedNumbers() {
return this.numbers.filter((n) => !this.markedNumbers.has(n));
}
showCard() {
const array = [];
for (let i = 0; i < this.cardSize; i++) {
array.push(
this.numbers
.slice(i * this.cardSize, i * this.cardSize + this.cardSize)
.map((n) =>
this.markedNumbers.has(n) ? `\x1b[47m\x1b[30m${n}\x1b[0m` : n
)
.join(`\t`)
);
}
console.log(array.join("\n") + "\n");
}
}
function part1(_cards) {
let cards = _cards.map((x) => new Card(x));
let winningCard;
const actuallyDrawn = [];
for (const drawn of drawnNumbers) {
let finished = false;
actuallyDrawn.push(drawn);
for (const card of cards) {
card.addMarkedNumber(drawn);
if (card.isComplete) {
finished = true;
winningCard = card;
break;
}
}
if (finished) {
break;
}
}
const unmarkedNumbers = winningCard.unmarkedNumbers();
console.log(
unmarkedNumbers.reduce((a, b) => a + b, 0) * actuallyDrawn.slice(-1)
);
}
part1(cards);
function part2(_cards) {
let cards = _cards.map((x) => new Card(x));
let lastWinningCard;
let lastWinningNumber;
const actuallyDrawn = [];
for (const drawn of drawnNumbers) {
actuallyDrawn.push(drawn);
let hasIncompleteCards = false;
for (const card of cards) {
if (!card.isComplete) {
hasIncompleteCards = true;
card.addMarkedNumber(drawn);
// card.showCard();
if (card.isComplete) {
lastWinningCard = card;
lastWinningNumber = drawn;
}
}
}
if (!hasIncompleteCards) {
break;
}
}
const unmarkedNumbers = lastWinningCard.unmarkedNumbers();
console.log(unmarkedNumbers.reduce((a, b) => a + b, 0) * lastWinningNumber);
}
part2(cards);