This commit is contained in:
Yanick Champoux 2022-12-02 10:59:27 -05:00
parent a87512fbf2
commit 8703a2c454
3 changed files with 98 additions and 55 deletions

View File

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

25
2022/02/part2.js Normal file
View File

@ -0,0 +1,25 @@
import * as R from "remeda";
import { movePositions, mapScores, playMap, parseInput } from "./part1.js";
function calculateMove(opponent, verdict) {
const incr = {
Y: 0,
X: 2,
Z: 1,
};
const index =
(movePositions.findIndex((x) => x === opponent) + incr[verdict]) %
movePositions.length;
return movePositions[index];
}
export const solutionPart2 = R.createPipe(
parseInput,
R.map(([a, b]) => [a, calculateMove(a, b)]),
R.map(mapScores),
R.map(R.sumBy(R.identity)),
R.sumBy(R.identity)
);

View File

@ -1,28 +1,41 @@
import { test, expect } from "vitest"; import { test, expect, describe } from "vitest";
import { expectSolution } from '../01/main.js'; import { expectSolution } from "../01/main.js";
import { parseInput, sampleInput, puzzleInput, mapScores, solutionPart1 } from './part1.js'; import {
parseInput,
sampleInput,
puzzleInput,
mapScores,
solutionPart1,
} from "./part1.js";
import { solutionPart2 } from "./part2.js";
test( "input parsing", () => { describe("part 1", () => {
expect(parseInput(sampleInput)).toEqual([['A','B'],['B','A'],['C','C']]); test("input parsing", () => {
}); expect(parseInput(sampleInput)).toEqual([
["A", "Y"],
["B", "X"],
["C", "Z"],
]);
});
test( "mapScore", () => { test("mapScore", () => {
expect( mapScores(['A','B']) ).toEqual([6,2]); expect(mapScores(["A", "B"])).toEqual([6, 2]);
expect( mapScores(['B','A']) ).toEqual([0,1]); expect(mapScores(["B", "A"])).toEqual([0, 1]);
expect( mapScores(['C','C']) ).toEqual([3,3]); expect(mapScores(["C", "C"])).toEqual([3, 3]);
}); });
test( "part 1, sample", () => { test("part 1, sample", () => {
expect( expect(solutionPart1(sampleInput)).toEqual(15);
solutionPart1(sampleInput) });
).toEqual(15)
}); test("solution", () => {
test("part 1", () => {
expectSolution(solutionPart1(puzzleInput)).toEqual(12458); expectSolution(solutionPart1(puzzleInput)).toEqual(12458);
});
}); });
test.todo("part 2", () => { describe("part 2", () => {
test("part 2", () => {
expectSolution(solutionPart2(puzzleInput)).toEqual(12683);
});
}); });