-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.js
79 lines (72 loc) · 1.99 KB
/
day02.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
import { read, readEx } from './util.js';
import Vec from './util/Vec.js';
await import('./util/bad-but-great.js');
await readEx();
await read();
let data;
// 10:03
const partOne = () => {
let ids = 0;
for (const line of data.lines()) {
const [left, right] = line.split(': ');
const game_id = +left.match(/\d+/)[0];
const sets = right.split('; ').map(s => s.split(', '));
const cubes = {
red: 0,
green: 0,
blue: 0,
};
for (let set of sets) {
for (const count of set) {
const n = +count.split` `[0];
const c = count.split` `[1];
cubes[c] = Math.max(cubes[c], n);
}
}
console.log({ game_id, sets });
console.log(cubes);
if (cubes.red <= 12 && cubes.green <= 13 && cubes.blue <= 14) {
console.log(game_id);
ids += game_id;
}
}
console.log({ids});
};
// 11:54
const partTwo = () => {
let ids = 0;
for (const line of data.lines()) {
const [left, right] = line.split(': ');
const game_id = +left.match(/\d+/)[0];
const sets = right.split('; ').map(s => s.split(', '));
const cubes = {
red: 0,
green: 0,
blue: 0,
};
for (let set of sets) {
for (const count of set) {
const n = +count.split` `[0];
const c = count.split` `[1];
cubes[c] = Math.max(cubes[c], n);
}
}
console.log({ game_id, sets });
console.log(cubes);
let p = cubes.red * cubes.blue * cubes.green;
console.log(p);
ids += p;
}
console.log({ids});
};
if (process.argv[2]) {
console.log('--- --- Running Sample Data --- ---');
data = await readEx(); // Sample Data
partOne();
partTwo();
} else {
console.log('--- --- Running Real Data --- ---');
data = await read(); // Real Data
partOne();
partTwo();
}