-
Notifications
You must be signed in to change notification settings - Fork 0
/
2301.ts
66 lines (63 loc) · 1.66 KB
/
2301.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
let day = 1
import { config } from "https://deno.land/x/dotenv/mod.ts"
config()
let session = Deno.env.get("AOC_SESSION")
let get_stuff = (day): Promise<string> => {
let url: string = `https://adventofcode.com/2023/day/${day}/input`
console.log(session)
let headers = new Headers({ 'Cookie': `session=${session}` })
return fetch(url, { headers })
.then((resp) => resp.text())
.catch((err) => { throw err })
}
get_stuff(day).then((infile) => {
let lines: string[] = infile.split('\n')
lines.pop()
let A: string[] = []
let r1 = 0
let r2 = 0
let D: Record<string, int> = {
"one": 1, "two": 2, "three": 3, "four": 4, "five": 5,
"six": 6, "seven": 7, "eight": 8, "nine": 9,
"1": 1, "2": 2, "3": 3, "4": 4, "5": 5,
"6": 6, "7": 7, "8": 8, "9": 9
}
for (let s of lines) {
A.push(s)
let a: Array<[number, string]> = []
let i = -1
let L: string
let R: string
while (++i < s.length) {
if ( ! isNaN(parseInt(s[i]))) {
L = s[i]
break
}
}
i = s.length
while (--i > -1) {
if ( ! isNaN(parseInt(s[i]))) {
R = s[i]
break
}
}
r1 += parseInt(L + R)
for (let [key, value] of Object.entries(D)) {
let l = s.indexOf(key)
let r = s.lastIndexOf(key)
if (l !== -1) {
a.push([l, key])
}
if (r !== -1) {
a.push([r, key])
}
}
a.sort((x, y) => x[0] - y[0])
let ll: string = D[a[0][1]]!.toString()
let rr: string = D[a[a.length - 1][1]]!.toString()
let ss: string = ll + rr
r2 += parseInt(ss)
}
console.log("part 1:", r1)
console.log("part 2:", r2)
}).catch((err) => { throw err })