-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.js
57 lines (48 loc) · 1.26 KB
/
day3.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
import fs from "fs";
import assert from "assert";
import * as R from "ramda";
const parseInput = (input) => input.split("\n").map((line) => line.split(""));
const testInput = `vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw`;
const charValue = (character) => {
return character.charCodeAt(0) - (character.match(/[A-Z]/) ? 38 : 96);
};
const part1 = (input) => {
const lines = parseInput(input);
return lines.reduce(
(acc, chars) =>
acc +
charValue(
R.intersection(
chars.slice(0, Math.ceil(chars.length / 2)),
chars.slice(Math.ceil(chars.length / 2))
)[0]
),
0
);
};
const part2 = (input) => {
const lines = parseInput(input);
return lines.reduce(
(acc, line, index) =>
acc +
(index % 3 === 0
? charValue(
R.intersection(
R.intersection(line, lines[index + 1]),
lines[index + 2]
)[0]
)
: 0),
0
);
};
assert(part1(testInput) === 157);
assert(part2(testInput) === 70);
const input = fs.readFileSync("inputs/day3.txt", "utf-8");
console.log(`Part 1: ${part1(input)}`);
console.log(`Part 2: ${part2(input)}`);