-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02_1.js
53 lines (48 loc) · 1.09 KB
/
day02_1.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
console.log("---");
import { readLines } from "https://deno.land/[email protected]/io/bufio.ts";
import { range } from "https://deno.land/x/[email protected]/range.mjs";
import { slidingWindows } from "https://deno.land/[email protected]/collections/mod.ts";
import count from "https://deno.land/x/[email protected]/src/collection/count.ts";
const sMap = {
A: 1,
B: 2,
C: 3,
};
const mMap = {
X: "A",
Y: "B",
Z: "C",
};
const wMap = {
A: "C",
B: "A",
C: "B",
};
const lMap = {
A: "B",
B: "C",
C: "A",
};
function play(line) {
const [pl1, _pl2] = line.split(" ");
// const pl2 = mMap[_pl2];
let pl2;
if (_pl2 === "X") pl2 = wMap[pl1];
if (_pl2 === "Y") pl2 = pl1;
if (_pl2 === "Z") pl2 = lMap[pl1];
let res = 0;
if (pl1 === pl2) res = 3 + sMap[pl2];
else if (wMap[pl2] === pl1) res = 6 + sMap[pl2];
else res = sMap[pl2];
console.log({ line, pl1, pl2, res });
return res;
}
const all = (await Deno.readTextFile("./resources/input02.txt"))
.trim()
.split("\n");
const res = all.map((l) => play(l));
console.log(res);
console.log(
{ res },
res.reduce((acc, i) => acc + i, 0)
);