-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.ts
65 lines (49 loc) · 1.47 KB
/
3.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
import { readFile } from 'fs/promises'
const input = (await readFile('input.txt')).toString()
const LETTERS = 'abcdefghijklmnopqrstuvwxyz'
const splitAt = (index: number, text: string) => [
text.slice(0, index),
text.slice(index),
]
const getPriority = (char: string) => {
if (char === char.toLowerCase()) return LETTERS.indexOf(char) + 1
return LETTERS.indexOf(char.toLowerCase()) + 27
}
const GROUP_SIZE = 3
let totalPriority = 0
const sacks = input.split('\n')
for (let i = 0; i < sacks.length; i += GROUP_SIZE) {
const group = sacks.slice(i, i + GROUP_SIZE)
const letters: string[] = []
for (const aChar of group[0]) {
for (const bChar of group[1]) {
for (const cChar of group[2]) {
if (
aChar === bChar &&
aChar === cChar &&
bChar === cChar &&
!letters.includes(aChar)
) {
totalPriority += getPriority(aChar)
letters.push(aChar)
}
}
}
}
}
console.log(totalPriority)
// part 1
// for (const sack of input.split('\n')) {
// const middleIndex = sack.length / 2
// const [first, second] = splitAt(middleIndex, sack)
// const sharedLetters: string[] = []
// for (const firstChar of first) {
// for (const secondChar of second) {
// if (firstChar === secondChar && !sharedLetters.includes(firstChar)) {
// totalPriority += getPriority(firstChar)
// sharedLetters.push(firstChar)
// }
// }
// }
// }
// console.log(totalPriority)