-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.js
107 lines (98 loc) · 2.99 KB
/
day2.js
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
const fs = require('fs');
const allContents = fs.readFileSync('/Users/schliengeranais/Documents/theodo/advent-of-code-2022/day-02/input2.txt', 'utf-8');
const calculateRoundOutcome = (opponent, me) => {
switch (opponent) {
case 'A':
if (me === 'X') return 3;
if (me === 'Y') return 6;
if (me === 'Z') return 0;
case 'B':
if (me === 'X') return 0;
if (me === 'Y') return 3;
if (me === 'Z') return 6;
case 'C':
if (me === 'Z') return 3;
if (me === 'Y') return 0;
if (me === 'X') return 6;
default:
return 0;
}
};
const calculateShapeScore = (shape) => {
switch (shape) {
case 'X':
case 'A':
return 1;
case 'Y':
case 'B':
return 2;
case 'Z':
case 'C':
return 3;
default:
return 0;
}
};
const calculateEndPoints = (end) => {
switch (end) {
case 'X':
return 0;
case 'Y':
return 3;
case 'Z':
return 6;
default:
return 0;
}
};
const findOutShape = (opponent, end) => {
switch (opponent) {
case 'A':
if (end === 0) return 'C';
if (end === 3) return 'A';
if (end === 6) return 'B';
case 'B':
if (end === 0) return 'A';
if (end === 3) return 'B';
if (end === 6) return 'C';
case 'C':
if (end === 0) return 'B';
if (end === 3) return 'C';
if (end === 6) return 'A';
default:
return 'A';
}
}
const part1score = () => {
let totalScore = 0;
allContents.split(/\r?\n/).forEach((line) => {
if (line.length === 0) return;
const [them, me] = line.split(' ');
const outcomeScore = calculateRoundOutcome(them, me);
const shapeScore = calculateShapeScore(me);
const roundScore = outcomeScore + shapeScore;
console.log(them, me)
console.log(shapeScore, '+', outcomeScore, '=', roundScore)
totalScore += roundScore;
});
console.log('if everything goes exactly according to the strategy guide, my score would be:')
console.log(totalScore)
}
const part2score = () => {
let totalScore = 0;
allContents.split(/\r?\n/).forEach((line) => {
if (line.length === 0) return;
const [them, end] = line.split(' ');
const outcomeScore = calculateEndPoints(end);
const myShape = findOutShape(them, outcomeScore)
const shapeScore = calculateShapeScore(myShape);
const roundScore = outcomeScore + shapeScore;
console.log(them, end)
console.log(myShape, shapeScore, '+', outcomeScore, '=', roundScore)
totalScore += roundScore;
});
console.log('if everything goes exactly according to the strategy guide, my score would be:')
console.log(totalScore)
}
// part1score();
// part2score();