part 2
This commit is contained in:
parent
a87512fbf2
commit
8703a2c454
@ -1,58 +1,63 @@
|
||||
import * as R from "remeda";
|
||||
import { test, expect } from "vitest";
|
||||
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
|
||||
B X
|
||||
C Z`;
|
||||
|
||||
export const puzzleInput =
|
||||
fs.readFileSync( path.join( path.dirname(import.meta.url), "input").replace('file:',''), "utf8");
|
||||
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
|
||||
}
|
||||
X: "A", // rock
|
||||
Y: "B", // paper
|
||||
Z: "C", // scissors
|
||||
};
|
||||
|
||||
export const parseInput = R.createPipe(
|
||||
text => text.split("\n"),
|
||||
R.map( entry => entry.split(' ') ),
|
||||
R.map( ([a,b]) => [a,playMap[b]] ),
|
||||
)
|
||||
(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,
|
||||
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;
|
||||
}
|
||||
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 mapScores = ([opponent, me]) => [
|
||||
outcome(opponent, me),
|
||||
playScore[me],
|
||||
];
|
||||
|
||||
export const solutionPart1 = R.createPipe(
|
||||
parseInput,
|
||||
R.filter( ([a]) => a ), // remove last line
|
||||
R.map( mapScores ),
|
||||
R.map( R.sumBy(R.identity) ),
|
||||
R.sumBy(R.identity)
|
||||
parseInput,
|
||||
R.map(([a, b]) => [a, playMap[b]]),
|
||||
R.map(mapScores),
|
||||
R.map(R.sumBy(R.identity)),
|
||||
R.sumBy(R.identity)
|
||||
);
|
||||
|
||||
|
25
2022/02/part2.js
Normal file
25
2022/02/part2.js
Normal 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)
|
||||
);
|
@ -1,28 +1,41 @@
|
||||
import { test, expect } from "vitest";
|
||||
import { test, expect, describe } from "vitest";
|
||||
|
||||
import { expectSolution } from '../01/main.js';
|
||||
import { parseInput, sampleInput, puzzleInput, mapScores, solutionPart1 } from './part1.js';
|
||||
import { expectSolution } from "../01/main.js";
|
||||
import {
|
||||
parseInput,
|
||||
sampleInput,
|
||||
puzzleInput,
|
||||
mapScores,
|
||||
solutionPart1,
|
||||
} from "./part1.js";
|
||||
import { solutionPart2 } from "./part2.js";
|
||||
|
||||
test( "input parsing", () => {
|
||||
expect(parseInput(sampleInput)).toEqual([['A','B'],['B','A'],['C','C']]);
|
||||
describe("part 1", () => {
|
||||
test("input parsing", () => {
|
||||
expect(parseInput(sampleInput)).toEqual([
|
||||
["A", "Y"],
|
||||
["B", "X"],
|
||||
["C", "Z"],
|
||||
]);
|
||||
});
|
||||
|
||||
test("mapScore", () => {
|
||||
expect(mapScores(["A", "B"])).toEqual([6, 2]);
|
||||
expect(mapScores(["B", "A"])).toEqual([0, 1]);
|
||||
expect(mapScores(["C", "C"])).toEqual([3, 3]);
|
||||
});
|
||||
|
||||
test("part 1, sample", () => {
|
||||
expect(solutionPart1(sampleInput)).toEqual(15);
|
||||
});
|
||||
|
||||
test("solution", () => {
|
||||
expectSolution(solutionPart1(puzzleInput)).toEqual(12458);
|
||||
});
|
||||
});
|
||||
|
||||
test( "mapScore", () => {
|
||||
expect( mapScores(['A','B']) ).toEqual([6,2]);
|
||||
expect( mapScores(['B','A']) ).toEqual([0,1]);
|
||||
expect( mapScores(['C','C']) ).toEqual([3,3]);
|
||||
});
|
||||
|
||||
test( "part 1, sample", () => {
|
||||
expect(
|
||||
solutionPart1(sampleInput)
|
||||
).toEqual(15)
|
||||
|
||||
});
|
||||
|
||||
test("part 1", () => {
|
||||
expectSolution(solutionPart1(puzzleInput)).toEqual(12458);
|
||||
});
|
||||
|
||||
test.todo("part 2", () => {
|
||||
describe("part 2", () => {
|
||||
test("part 2", () => {
|
||||
expectSolution(solutionPart2(puzzleInput)).toEqual(12683);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user