-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday02.mjs
71 lines (64 loc) · 1.95 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
import {
readInput,
sum,
splitWhitespace,
splitLines,
removeEmpty,
pipe,
map,
add,
reduce,
} from './utils.mjs'
const input = await readInput('02')
const parsedInput = pipe(splitLines, removeEmpty, map(splitWhitespace))(input)
const scores = {
win: 6,
loss: 0,
draw: 3,
rock: 1,
paper: 2,
scissors: 3,
}
const player1 = {
A: 'rock',
B: 'paper',
C: 'scissors',
}
const player2 = {
X: 'rock',
Y: 'paper',
Z: 'scissors',
}
const outcome = {
X: 'loss',
Y: 'draw',
Z: 'win',
}
export const part1 = pipe(
map(([p1, p2]) => [player1[p1], player2[p2]]),
map(([p1, p2]) => {
if (p1 === p2) return scores.draw + scores[p2]
if (p1 === 'rock' && p2 === 'paper') return scores.win + scores.paper
if (p1 === 'rock' && p2 === 'scissors') return scores.loss + scores.scissors
if (p1 === 'paper' && p2 === 'rock') return scores.loss + scores.rock
if (p1 === 'paper' && p2 === 'scissors') return scores.win + scores.scissors
if (p1 === 'scissors' && p2 === 'rock') return scores.win + scores.rock
if (p1 === 'scissors' && p2 === 'paper') return scores.loss + scores.paper
}),
reduce(add, 0)
)(parsedInput)
export const part2 = pipe(
map(([p1, p2]) => [player1[p1], outcome[p2]]),
map(([p1, p2]) => {
if (p1 === 'rock' && p2 === 'loss') return scores.loss + scores.scissors
if (p1 === 'rock' && p2 === 'draw') return scores.draw + scores.rock
if (p1 === 'rock' && p2 === 'win') return scores.win + scores.paper
if (p1 === 'paper' && p2 === 'loss') return scores.loss + scores.rock
if (p1 === 'paper' && p2 === 'draw') return scores.draw + scores.paper
if (p1 === 'paper' && p2 === 'win') return scores.win + scores.scissors
if (p1 === 'scissors' && p2 === 'loss') return scores.loss + scores.paper
if (p1 === 'scissors' && p2 === 'draw') return scores.draw + scores.scissors
if (p1 === 'scissors' && p2 === 'win') return scores.win + scores.rock
}),
reduce(add, 0)
)(parsedInput)