-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.ts
60 lines (49 loc) · 1.81 KB
/
part1.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
import { readFileSync } from 'fs';
function getInput(): string[] {
const input = readFileSync('./input.txt', 'utf8');
const rows: string[] = input.split('\r\n');
return rows;
}
function rawToCubes(raw: string) {
const colors: string[][] = raw.split(',').map((value) => value.trim().split(' '));
const cubes: { [key:string] : number } = colors.reduce((acc: { [key:string] : number } , color) => {
const [num, colorName] : string[] = color;
acc[colorName] = parseInt(num);
return acc;
}, {});
const orderedColors = ['red', 'green', 'blue']
const orderedCubes : { [key: string]: number } = {
red: 0,
green: 0,
blue: 0,
};
orderedColors.map((color) => {
if(cubes.hasOwnProperty(color)){
orderedCubes[color] = cubes[color];
}
});
return orderedCubes;
}
let array_of_games: number[] = [];
const bag_limit: { red: number, green: number, blue: number } = { red: 12, green: 13, blue: 14 };
function isInBagLimit(cubes: { [key: string]: number }): boolean {
const { red, green, blue } = cubes;
return red <= bag_limit.red && green <= bag_limit.green && blue <= bag_limit.blue;
}
getInput().map((value) => {
let array_of_cubes: { [key: string]: number }[] = [];
let array_of_results: boolean[] = [];
const gameId: string = value.split(':')[0].split(' ')[1];
const setsOfCubes: string[] = value.split(':')[1].split(';').map((value) => value.trim());
setsOfCubes.map((setOfCubes) => {
const cubes = rawToCubes(setOfCubes);
array_of_cubes.push(cubes);
})
array_of_cubes.map((cubes) => {
array_of_results.push(isInBagLimit(cubes));
})
if(array_of_results.indexOf(false) === -1){
array_of_games.push(parseInt(gameId));
}
})
console.log(array_of_games.reduce((acc, value) => acc + value, 0))