3 changed files with 95 additions and 52 deletions
@ -0,0 +1,25 @@
@@ -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 @@
@@ -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']]); |
||||
}); |
||||
|
||||
test( "mapScore", () => { |
||||
expect( mapScores(['A','B']) ).toEqual([6,2]); |
||||
expect( mapScores(['B','A']) ).toEqual([0,1]); |
||||
expect( mapScores(['C','C']) ).toEqual([3,3]); |
||||
}); |
||||
describe("part 1", () => { |
||||
test("input parsing", () => { |
||||
expect(parseInput(sampleInput)).toEqual([ |
||||
["A", "Y"], |
||||
["B", "X"], |
||||
["C", "Z"], |
||||
]); |
||||
}); |
||||
|
||||
test( "part 1, sample", () => { |
||||
expect( |
||||
solutionPart1(sampleInput) |
||||
).toEqual(15) |
||||
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("solution", () => { |
||||
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