Merge branch '2021-21'
This commit is contained in:
commit
7914f1a60a
30
2021/21/part1.mjs
Normal file
30
2021/21/part1.mjs
Normal file
@ -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;
|
||||
|
||||
}
|
42
2021/21/part2.mjs
Normal file
42
2021/21/part2.mjs
Normal file
@ -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));
|
||||
}
|
2
2021/21/sample
Normal file
2
2021/21/sample
Normal file
@ -0,0 +1,2 @@
|
||||
Player 1 starting position: 4
|
||||
Player 2 starting position: 8
|
20
2021/21/test.mjs
Normal file
20
2021/21/test.mjs
Normal file
@ -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);
|
||||
});
|
Loading…
Reference in New Issue
Block a user