From bd3952e9bee012468b6138f61bdf8c11d04ad951 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Mon, 4 Dec 2023 12:19:52 -0500 Subject: [PATCH] part 2 --- 2023/04/benchmark.js | 57 +++++++++++++++++++++------------------ 2023/04/benchmark.test.js | 17 ++++++------ 2023/04/part1.js | 42 +++++++++++++++-------------- 2023/04/part2.js | 32 +++++++++++++++++++--- 2023/04/solutions.yml | 2 +- 2023/04/test.js | 38 +++++++++++++------------- 6 files changed, 110 insertions(+), 78 deletions(-) diff --git a/2023/04/benchmark.js b/2023/04/benchmark.js index 5d7fe0c..69b227e 100644 --- a/2023/04/benchmark.js +++ b/2023/04/benchmark.js @@ -1,33 +1,38 @@ -import fs from 'fs-extra'; -import { parse } from 'yaml'; -import { Bench } from 'tinybench'; -import assert from 'assert'; +import fs from "fs-extra"; +import { parse } from "yaml"; +import { Bench } from "tinybench"; +import assert from "assert"; -import { solution_1 } from './part1.js'; -import { solution_2 } from './part2.js'; +import { solution_1 } from "./part1.js"; +import { solution_2 } from "./part2.js"; -const input = fs.readFileSync('./input',{ encoding: 'utf8'}); -const solutions = parse( fs.readFileSync('./solutions.yml',{encoding:'utf8'})); +const input = fs.readFileSync("./input", { encoding: "utf8" }); +const solutions = parse( + fs.readFileSync("./solutions.yml", { encoding: "utf8" }) +); -const bench = new Bench({time: 10000}); +const bench = new Bench({ time: 10000 }); -bench.add( '1', () => { - assert.equal(solution_1(input),solutions[1]) -}) -.add( '2', () => { - assert.equal(solution_2(input),solutions[2]) -}); +bench + .add("1", () => { + assert.equal(solution_1(input), solutions[1]); + }) + .add("2", () => { + assert.equal(solution_2(input), solutions[2]); + }); await bench.run(); -bench.tasks.forEach( ({result,name}) => { - console.log(JSON.stringify({ - day : 1, - year : 2023, - language: 'javascript', - part: name, - timestamp: new Date().toISOString(), - time: result.mean / 1000, - persec: 1000 / result.mean, - })); -}) +bench.tasks.forEach(({ result, name }) => { + console.log( + JSON.stringify({ + day: 1, + year: 2023, + language: "javascript", + part: name, + timestamp: new Date().toISOString(), + time: result.mean / 1000, + persec: 1000 / result.mean, + }) + ); +}); diff --git a/2023/04/benchmark.test.js b/2023/04/benchmark.test.js index 4ec0bda..5fb0b6a 100644 --- a/2023/04/benchmark.test.js +++ b/2023/04/benchmark.test.js @@ -1,13 +1,12 @@ -import { bench, expect } from 'vitest'; -import { readFileSync } from 'fs-extra'; -import { parse } from 'yaml'; +import { bench, expect } from "vitest"; +import { readFileSync } from "fs-extra"; +import { parse } from "yaml"; -import { solution_1 } from './part1.js'; +import { solution_1 } from "./part1.js"; -const input = readFileSync('./input',{ encoding: 'utf8'}); -const solutions = parse( readFileSync('./solutions.yml',{encoding:'utf8'})); +const input = readFileSync("./input", { encoding: "utf8" }); +const solutions = parse(readFileSync("./solutions.yml", { encoding: "utf8" })); -bench( 'part 1', () => { - expect(solution_1(input)).toBe(solutions[1]) +bench("part 1", () => { + expect(solution_1(input)).toBe(solutions[1]); }); - diff --git a/2023/04/part1.js b/2023/04/part1.js index 9b97e40..5439f5b 100644 --- a/2023/04/part1.js +++ b/2023/04/part1.js @@ -1,26 +1,28 @@ -import * as R from 'remeda'; +import * as R from "remeda"; export const slice_and_dice = R.createPipe( - (l) => l.replace(/Card \d+:/,'').split('|').map( - segment => segment.split(' ').filter(R.identity) - ), - ([winning,mine]) => { - let seen = {}; - winning.forEach( n => seen[n] = true ); - let got = mine.filter( n => seen[n] ).length; - return got; - }, - (n) => n == 0 ? 0 : Math.pow(2,n-1) + (l) => + l + .replace(/Card \d+:/, "") + .split("|") + .map((segment) => segment.split(" ").filter(R.identity)), + ([winning, mine]) => { + let seen = {}; + winning.forEach((n) => (seen[n] = true)); + let got = mine.filter((n) => seen[n]).length; + return got; + }, + (n) => (n == 0 ? 0 : Math.pow(2, n - 1)) ); +export const get_lines = R.createPipe( (i) => i.split("\n"), +R.compact); + export function solution_1(input) { - return R.pipe( - input, - i => i.split("\n"), - R.compact, - R.map( - slice_and_dice - ), - R.sumBy(R.identity) - ) + return R.pipe( + input, + get_lines, + R.map(slice_and_dice), + R.sumBy(R.identity) + ); } diff --git a/2023/04/part2.js b/2023/04/part2.js index 0eb2b95..914fc35 100644 --- a/2023/04/part2.js +++ b/2023/04/part2.js @@ -1,5 +1,31 @@ -import * as R from 'remeda'; +import * as R from "remeda"; -import { } from './part1.js'; +import { get_lines } from "./part1.js"; -export function solution_2(input){} +export function solution_2(input) { + const cards = get_lines(input); + + const cards_won = cards.map(()=>1); + let total_won = 0; + + while( cards.length ) { + // console.log(cards_won); + let card = cards.shift(); + const [ winning, mine ] = card + .replace(/Card \d+:/, "") + .split("|") + .map((segment) => segment.split(" ").filter(R.identity)); + + let seen = {}; + winning.forEach((n) => (seen[n] = true)); + let got = mine.filter((n) => seen[n]).length; + + R.range(1,got+1).forEach( i => { + cards_won[i] = (cards_won[i] ?? 0 ) +cards_won[0]; + }); + + total_won += cards_won.shift()??0; + } + + return total_won; +} diff --git a/2023/04/solutions.yml b/2023/04/solutions.yml index b3702b2..23a78b7 100644 --- a/2023/04/solutions.yml +++ b/2023/04/solutions.yml @@ -1,2 +1,2 @@ 1: 23678 -2: TODO +2: 15455663 diff --git a/2023/04/test.js b/2023/04/test.js index 5500b5d..27df077 100644 --- a/2023/04/test.js +++ b/2023/04/test.js @@ -1,23 +1,23 @@ -import { test, expect } from 'vitest'; -import { readFileSync } from 'fs-extra'; -import { parse } from 'yaml'; +import { test, expect } from "vitest"; +import { readFileSync } from "fs-extra"; +import { parse } from "yaml"; -import { solution_1, slice_and_dice } from './part1.js'; -import { solution_2 } from './part2.js'; +import { solution_1, slice_and_dice } from "./part1.js"; +import { solution_2 } from "./part2.js"; -const input = readFileSync('./input',{ encoding: 'utf8'}); -const example = readFileSync('./example',{ encoding: 'utf8'}); -const solutions = parse( readFileSync('./solutions.yml',{encoding:'utf8'})); +const input = readFileSync("./input", { encoding: "utf8" }); +const example = readFileSync("./example", { encoding: "utf8" }); +const solutions = parse(readFileSync("./solutions.yml", { encoding: "utf8" })); -test( 'part 1', () => { +test("part 1", () => { + expect( + slice_and_dice("Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53") + ).toBe(8); - expect( slice_and_dice( -"Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53" -)).toBe(8); - - expect( solution_1(example)).toBe(13); - expect( solution_1(input)).toBe(solutions['1']); -}) -test( 'part 2', () => { - expect( solution_2(input)).toBe(solutions['2']); -}) + expect(solution_1(example)).toBe(13); + expect(solution_1(input)).toBe(solutions["1"]); +}); +test("part 2", () => { + expect(solution_2(example)).toBe(30); + expect(solution_2(input)).toBe(solutions["2"]); +});