From 17047ac035b8775936fc26559976ed4d82418bff Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Tue, 21 Dec 2021 15:03:45 -0500 Subject: [PATCH] 2021-21 --- 2021/21/part1.mjs | 30 ++++++++++++++++++++++++++++++ 2021/21/part2.mjs | 42 ++++++++++++++++++++++++++++++++++++++++++ 2021/21/sample | 2 ++ 2021/21/test.mjs | 20 ++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 2021/21/part1.mjs create mode 100644 2021/21/part2.mjs create mode 100644 2021/21/sample create mode 100644 2021/21/test.mjs diff --git a/2021/21/part1.mjs b/2021/21/part1.mjs new file mode 100644 index 0000000..f6cdfc2 --- /dev/null +++ b/2021/21/part1.mjs @@ -0,0 +1,30 @@ +import fs from "fs-extra"; +import fp from "lodash/fp.js"; +import _ from "lodash"; + +export const processInput = (input) => fs.readFile(input,'utf8').then( + content => content.split("\n").filter(x=>x).map( + line => parseInt(line.split(": ")[1])-1 + ) +); + +export function solution(positions) { + let scores = [0,0]; + let dice = 0; + + MAIN: while( Math.max(...scores) < 1000 ) { + for( const player of [0,1] ) { + let add = 0; + _.range(3).forEach( () => { + dice++; + add+= dice; + } ); + positions[player] = (positions[player] + add) % 10; + scores[player] += 1 + positions[player] + if(scores[player]>=1000) break MAIN; + } + } + + return Math.min(...scores) * dice; + +} diff --git a/2021/21/part2.mjs b/2021/21/part2.mjs new file mode 100644 index 0000000..aa8f9fa --- /dev/null +++ b/2021/21/part2.mjs @@ -0,0 +1,42 @@ +import fs from "fs-extra"; +import fp from "lodash/fp.js"; +import _ from "lodash"; + +import * as p1 from "./part1.mjs"; + +const cartesianProduct = (...rest) => + rest.reduce( + (a, b) => a.flatMap((x) => b.map((y) => x.concat([y]))), + [[]] + ); + +const dimensions = cartesianProduct( + _.range(1, 4), + _.range(1, 4), + _.range(1, 4) +).map(fp.sum); + +function resolveGame(p1, p2, s1, s2, f) { + let wins = 0; + let loses = 0; + + dimensions.forEach((points) => { + const newPosition = (p1 + points) % 10; + const newScore = newPosition + 1 + s1; + if (newScore >= 21) { + wins++; + return; + } + const next = f(p2, newPosition, s2, newScore, f); + wins += next[1]; + loses += next[0]; + }); + + return [wins, loses]; +} + +export function solution(positions) { + const res = _.memoize(resolveGame, (...args) => args.join("-")); + + return Math.max(...res(...positions, 0, 0, res)); +} diff --git a/2021/21/sample b/2021/21/sample new file mode 100644 index 0000000..3f69194 --- /dev/null +++ b/2021/21/sample @@ -0,0 +1,2 @@ +Player 1 starting position: 4 +Player 2 starting position: 8 diff --git a/2021/21/test.mjs b/2021/21/test.mjs new file mode 100644 index 0000000..4cf8e08 --- /dev/null +++ b/2021/21/test.mjs @@ -0,0 +1,20 @@ +// https://adventofcode.com/2021/day/21 + +import tap from "tap"; +import fs from "fs-extra"; + +import * as p1 from "./part1.mjs"; +import * as p2 from "./part2.mjs"; + +const sample = p1.processInput('sample'); +const input = p1.processInput('input'); + +tap.test("part1", async (t) => { + t.equal(p1.solution(await sample),739785 ); + t.equal(p1.solution(await input), 916083); +}); + +tap.test("part2", async (t) => { + t.equal(p2.solution(await p1.processInput('sample')), 444356092776315); + t.equal(p2.solution(await p1.processInput('input')), 49982165861983); +});