-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.mjs
147 lines (131 loc) · 3.49 KB
/
day02.mjs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { open } from "node:fs/promises"
const ROCK_PLAYER = "X"
const PAPER_PLAYER = "Y"
const SCISSORS_PLAYER = "Z"
const SHALL_LOSE = "X"
const SHALL_DRAW = "Y"
const SHALL_WIN = "Z"
const ROCK_OPPONENT = "A"
const PAPER_OPPONENT = "B"
const SCISSORS_OPPONENT = "C"
const SCORE_ROCK = 1
const SCORE_PAPER = 2
const SCORE_SCISSORS = 3
const LOSS = 0
const DRAW = 3
const WIN = 6
async function parseInput(filename) {
const matches = []
const file = await open(filename)
for await (const line of file.readLines()) {
matches.push(line.split(" "))
}
return matches
}
export async function part1(filename) {
return (await parseInput(filename))
.map(([opponent, player]) => score(opponent, player))
.reduce((sum, x) => sum + x, 0)
}
export async function part2(filename) {
return (await parseInput(filename))
.map(([opponent, result]) => [opponent, findPlayerShape(opponent, result)])
.map(([opponent, player]) => score(opponent, player))
.reduce((sum, x) => sum + x, 0)
}
const findPlayerShape = (opponent, result) => {
switch (opponent) {
case ROCK_OPPONENT:
switch (result) {
case SHALL_DRAW:
return ROCK_PLAYER
case SHALL_WIN:
return PAPER_PLAYER
case SHALL_LOSE:
return SCISSORS_PLAYER
default:
throw Error(`unexpected result "${result}"`)
}
case PAPER_OPPONENT:
switch (result) {
case SHALL_DRAW:
return PAPER_PLAYER
case SHALL_WIN:
return SCISSORS_PLAYER
case SHALL_LOSE:
return ROCK_PLAYER
default:
throw Error(`unexpected result "${result}"`)
}
case SCISSORS_OPPONENT:
switch (result) {
case SHALL_DRAW:
return SCISSORS_PLAYER
case SHALL_WIN:
return ROCK_PLAYER
case SHALL_LOSE:
return PAPER_PLAYER
default:
throw Error(`unexpected result "${result}"`)
}
default:
throw Error(`unexpected shape "${opponent}"`)
}
}
const score = (opponent, player) =>
shape_score(player) + win_score(opponent, player)
const shape_score = (shape) => {
switch (shape) {
case ROCK_PLAYER:
return SCORE_ROCK
case PAPER_PLAYER:
return SCORE_PAPER
case SCISSORS_PLAYER:
return SCORE_SCISSORS
default:
throw Error(`unexpected shape "${shape}"`)
}
}
const win_score = (opponent, player) => {
switch (player) {
case ROCK_PLAYER:
switch (opponent) {
case ROCK_OPPONENT:
return DRAW
case PAPER_OPPONENT:
return LOSS
case SCISSORS_OPPONENT:
return WIN
default:
throw Error(`unexpected shape "${opponent}"`)
}
case PAPER_PLAYER:
switch (opponent) {
case ROCK_OPPONENT:
return WIN
case PAPER_OPPONENT:
return DRAW
case SCISSORS_OPPONENT:
return LOSS
default:
throw Error(`unexpected shape "${opponent}"`)
}
case SCISSORS_PLAYER:
switch (opponent) {
case ROCK_OPPONENT:
return LOSS
case PAPER_OPPONENT:
return WIN
case SCISSORS_OPPONENT:
return DRAW
default:
throw Error(`unexpected shape "${opponent}"`)
}
default:
throw Error(`unexpected shape "${shape}"`)
}
}
const scorePart1 = await part1("./day02.in")
const scorePart2 = await part2("./day02.in")
console.log(`Day 02 part 1: ${scorePart1}`)
console.log(`Day 02 part 2: ${scorePart2}`)