3 changed files with 2586 additions and 0 deletions
@ -0,0 +1,58 @@
@@ -0,0 +1,58 @@
|
||||
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.map( entry => entry.split(' ') ), |
||||
R.map( ([a,b]) => [a,playMap[b]] ), |
||||
) |
||||
|
||||
|
||||
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.filter( ([a]) => a ), // remove last line
|
||||
R.map( mapScores ), |
||||
R.map( R.sumBy(R.identity) ), |
||||
R.sumBy(R.identity) |
||||
); |
||||
|
@ -0,0 +1,28 @@
@@ -0,0 +1,28 @@
|
||||
import { test, expect } from "vitest"; |
||||
|
||||
import { expectSolution } from '../01/main.js'; |
||||
import { parseInput, sampleInput, puzzleInput, mapScores, solutionPart1 } from './part1.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]); |
||||
}); |
||||
|
||||
test( "part 1, sample", () => { |
||||
expect( |
||||
solutionPart1(sampleInput) |
||||
).toEqual(15) |
||||
|
||||
}); |
||||
|
||||
test("part 1", () => { |
||||
expectSolution(solutionPart1(puzzleInput)).toEqual(12458); |
||||
}); |
||||
|
||||
test.todo("part 2", () => { |
||||
}); |
Loading…
Reference in new issue