-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
48 lines (36 loc) · 1.27 KB
/
main.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
import { join } from 'node:path'
import { existsSync, readFileSync, writeFileSync } from 'fs'
const dayNumber = process.argv[2]
const sessionToken = process.env.AOC_SESSION_TOKEN
if (!sessionToken) {
throw new Error('Token must be set')
}
console.log(`Running day ${dayNumber}`)
const getData = async (day) => {
const cacheFile = join('.cache', `day-${day}.txt`)
if (existsSync(cacheFile)) {
return readFileSync(cacheFile, 'utf-8')
} else {
console.log('Fetching from Advent Of Code')
const response = await fetch(`https://adventofcode.com/2022/day/${day}/input`, {
headers: {
Cookie: `session=${sessionToken}`,
'User-Agent': 'https://github.com/alexmuller/advent-of-code-2022'
}
})
if (!response.ok) {
throw new Error('Bad response from Advent Of Code')
}
const fileContents = await response.text()
writeFileSync(cacheFile, fileContents)
return fileContents
}
}
const data = (await getData(dayNumber)).split('\n');
if (data[data.length - 1] === '') {
data.pop()
}
const dayCode = await import(`./day-${('00'+dayNumber).slice(-2)}/main.js`)
console.log(dayCode.partOne(data))
console.log(dayCode.partTwo(data))
export {}