adventofcode/2022/02/part1.js

61 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-12-02 15:35:31 +00:00
import * as R from "remeda";
import fs from "fs-extra";
2022-12-02 15:59:27 +00:00
import path from "path";
2022-12-02 15:35:31 +00:00
export const sampleInput = `A Y
B X
C Z`;
2022-12-02 15:59:27 +00:00
export const puzzleInput = fs.readFileSync(
path.join(path.dirname(import.meta.url), "input").replace("file:", ""),
"utf8"
);
2022-12-02 15:35:31 +00:00
const playMap = {
2022-12-02 15:59:27 +00:00
X: "A", // rock
Y: "B", // paper
Z: "C", // scissors
};
2022-12-02 15:35:31 +00:00
export const parseInput = R.createPipe(
2022-12-02 15:59:27 +00:00
(text) => text.split("\n"),
R.filter(R.identity), // remove last line
R.map((entry) => entry.split(" "))
);
2022-12-02 15:35:31 +00:00
2022-12-02 15:59:27 +00:00
export const movePositions = ["A", "B", "C"];
2022-12-02 15:35:31 +00:00
const playScore = {
2022-12-02 15:59:27 +00:00
A: 1,
B: 2,
C: 3,
2022-12-02 15:35:31 +00:00
};
const draw = 3;
const lost = 0;
const win = 6;
2022-12-02 15:59:27 +00:00
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;
};
2022-12-02 15:35:31 +00:00
2022-12-02 15:59:27 +00:00
export const mapScores = ([opponent, me]) => [
outcome(opponent, me),
playScore[me],
];
2022-12-02 15:35:31 +00:00
export const solutionPart1 = R.createPipe(
2022-12-02 15:59:27 +00:00
parseInput,
R.map(([a, b]) => [a, playMap[b]]),
R.map(mapScores),
R.map(R.sumBy(R.identity)),
R.sumBy(R.identity)
2022-12-02 15:35:31 +00:00
);