64 lines
1.2 KiB
JavaScript
64 lines
1.2 KiB
JavaScript
import * as R from "remeda";
|
|
import { test, expect } from "vitest";
|
|
import fs from "fs-extra";
|
|
import path from "path";
|
|
|
|
import { spy } from "../01/main.js";
|
|
|
|
export const sampleInput = `A Y
|
|
B X
|
|
C Z`;
|
|
|
|
export const puzzleInput = fs.readFileSync(
|
|
path.join(path.dirname(import.meta.url), "input").replace("file:", ""),
|
|
"utf8"
|
|
);
|
|
|
|
const playMap = {
|
|
X: "A", // rock
|
|
Y: "B", // paper
|
|
Z: "C", // scissors
|
|
};
|
|
|
|
export const parseInput = R.createPipe(
|
|
(text) => text.split("\n"),
|
|
R.filter(R.identity), // remove last line
|
|
R.map((entry) => entry.split(" "))
|
|
);
|
|
|
|
export const movePositions = ["A", "B", "C"];
|
|
|
|
const playScore = {
|
|
A: 1,
|
|
B: 2,
|
|
C: 3,
|
|
};
|
|
|
|
const draw = 3;
|
|
const lost = 0;
|
|
const win = 6;
|
|
|
|
const outcome = (opponent, me) => {
|
|
if (opponent === me) return draw;
|
|
if (opponent === "A") {
|
|
return me === "C" ? lost : win;
|
|
}
|
|
if (opponent === "B") {
|
|
return me === "A" ? lost : win;
|
|
}
|
|
return me === "B" ? lost : win;
|
|
};
|
|
|
|
export const mapScores = ([opponent, me]) => [
|
|
outcome(opponent, me),
|
|
playScore[me],
|
|
];
|
|
|
|
export const solutionPart1 = R.createPipe(
|
|
parseInput,
|
|
R.map(([a, b]) => [a, playMap[b]]),
|
|
R.map(mapScores),
|
|
R.map(R.sumBy(R.identity)),
|
|
R.sumBy(R.identity)
|
|
);
|