-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.ts
73 lines (66 loc) · 2.03 KB
/
day2.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
67
68
69
70
71
72
73
let OpponentMove = {
A: 1, // rock
B: 2, // paper
C: 3, // Scissors
}
let YourMove = {
X: 1, // R
Y: 2, // P
Z: 3, // S
}
async function main() {
const test = 'inputs/day2test.txt'
const input = 'inputs/input2.txt'
async function computePart1(inputFilePath: any): Promise<number> {
const file = await Deno.readTextFile(inputFilePath);
let counter = 0;
file.trim().split("\n").map(line => {
let [om, mm] = line.trim().split(" ")
let omn: number = OpponentMove[om]
let mmn: number = YourMove[mm]
counter += mmn;
if (omn === mmn) {
counter += 3
} else if (omn === mmn - 1 || (omn === 3 && mmn === 1)) { // win
counter += 6
}
})
return counter
}
async function computePart2(inputFilePath: any): Promise<number> {
const readStream = await Deno.readTextFile(inputFilePath);
let counter = 0;
readStream.trim().split("\n").map(line => {
let [om, mm] = line.trim().split(" ")
let omn: number = OpponentMove[om]
let mmn = mm === "X" ? //lose
omn - 1 :
mm === "Y" ? //draw
omn :
omn + 1; // win;
// roll over
mmn = mmn === 0 ? 3 : mmn;
mmn = mmn === 4 ? 1 : mmn;
counter += mmn
if (omn === mmn) {
counter += 3
} else if (omn === mmn - 1 || (omn === 3 && mmn === 1)) { // win
counter += 6
}
})
return counter
}
console.log('Part 1')
console.log('test: ')
console.log(await computePart1(test))
console.log('Expecting 15')
console.log('\nInput:')
console.log(await computePart1(input))
console.log('\n\nPart 2')
console.log('test: ')
console.log(await computePart2(test))
console.log('Expecting 12')
console.log('\nInput:')
console.log(await computePart2(input))
}
main();