-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
91 lines (78 loc) · 1.89 KB
/
index.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
87
88
89
90
91
import run from "aocrunner";
const parseInput = (rawInput: string) => rawInput;
const bag = {
red: 12,
green: 13,
blue: 14,
};
const parseRound = (round: string) => {
const [number, color] = round.split(" ");
return {
number: parseInt(number),
color,
};
};
const part1 = (rawInput: string) => {
const input = parseInput(rawInput);
const lines = input.split("\n");
return (
lines
.map((line) => {
const [id, game] = line.split(": ");
// split game into rounds
const rounds = game
.split(", ")
.map((r) => r.split("; "))
.flat()
.map(parseRound);
// filter rounds by color
const redRounds = rounds.filter((r) => r.color === "red");
const greenRounds = rounds.filter((r) => r.color === "green");
const blueRounds = rounds.filter((r) => r.color === "blue");
// check if any of the rounds are invalid
const valid = !(
// invalid if any round has more than the bag
(
redRounds.some((r) => r.number > bag.red) ||
greenRounds.some((r) => r.number > bag.green) ||
blueRounds.some((r) => r.number > bag.blue)
)
);
return {
id,
valid,
};
})
.filter((g) => g.valid) // filter to keep valid games
.map((g) => g.id) // get the id
.map((id) => parseInt(id.split(" ")[1])) // get the number
// sum all numbers
.reduce((acc, id) => acc + id, 0)
);
};
const part2 = (rawInput: string) => {
const input = parseInput(rawInput);
return;
};
run({
part1: {
tests: [
// {
// input: ``,
// expected: "",
// },
],
solution: part1,
},
part2: {
tests: [
// {
// input: ``,
// expected: "",
// },
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});