From 99e244c054cfae9826b535dc07f1364396734c95 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sat, 10 Dec 2022 18:25:50 -0500 Subject: [PATCH] part 2 --- 2022/10/part1.js | 65 ++++++++++++++++++++++++------------------------ 2022/10/part2.js | 36 +++++++++++++++++++++------ 2022/10/test.js | 60 +++++++++++++++++++++++++++----------------- 3 files changed, 98 insertions(+), 63 deletions(-) diff --git a/2022/10/part1.js b/2022/10/part1.js index 531b5a6..efa45db 100644 --- a/2022/10/part1.js +++ b/2022/10/part1.js @@ -15,49 +15,48 @@ export const sample = readInput(import.meta.url, "sample"); export const sample2 = readInput(import.meta.url, "sample.2"); export const comsys = () => { - let cycle = 1; - let register = 1; - const scheduled = []; + let cycle = 1; + let register = 1; + const scheduled = []; - return (instr) => { - if( instr && (instr[0] === 'addx') ) { - scheduled.push(0,instr[1]); - } else { - scheduled.push(0); - } - cycle++; - register += scheduled.shift() ?? 0; - return { cycle, register }; + return (instr) => { + if (instr && instr[0] === "addx") { + scheduled.push(0, instr[1]); + } else if (instr) { + scheduled.push(0); } - -} + cycle++; + register += scheduled.shift() ?? 0; + return { cycle, register }; + }; +}; export const gatherStrength = () => { - let strength = 0; - return ({cycle, signal, register}) => { - if( (cycle - 20) % 40 === 0 ) { - strength += signal; - } - return { strength, cycle, signal, register }; + let strength = 0; + return ({ cycle, signal, register }) => { + if ((cycle - 20) % 40 === 0) { + strength += signal; } -} + return { strength, cycle, signal, register }; + }; +}; export const addSignal = (input) => ({ - ...input, - signal: input.cycle*input.register + ...input, + signal: input.cycle * input.register, }); - export default (instructions) => { - const sys = comsys(); + const sys = comsys(); - return R.pipe( - R.range(0,220), - R.map( () => sys(instructions.shift()) ), - R.map( addSignal ), - R.map( gatherStrength() ), - R.last, - R.prop('strength') - ) + instructions = [...instructions]; + return R.pipe( + R.range(0, 220), + R.map(() => sys(instructions.shift())), + R.map(addSignal), + R.map(gatherStrength()), + R.last, + R.prop("strength") + ); }; diff --git a/2022/10/part2.js b/2022/10/part2.js index 673e07f..1eb393c 100644 --- a/2022/10/part2.js +++ b/2022/10/part2.js @@ -1,13 +1,35 @@ import * as R from "remeda"; -import { comsys } from './part1.js'; +export const comsys = () => { + let cycle = 0; + let register = 1; + const scheduled = []; + + return (instr) => { + if (instr && instr[0] === "addx") { + scheduled.push(0, instr[1]); + } else if (instr) { + scheduled.push(0); + } + cycle++; + register += scheduled.shift() ?? 0; + return { cycle, register }; + }; +}; + +const toPixel = (entry, i) => { + if (entry.register >= i - 1 && entry.register <= i + 1) return "#"; + else return "."; +}; export default (instructions) => { - const sys = comsys(); - - return R.pipe( - R.range(0,3), - R.map( () => sys(instructions.shift()) ), - ) + const sys = comsys(); + instructions = [...instructions]; + return R.pipe( + [sys(), ...R.range(0, 239).map(() => sys(instructions.shift()))], + R.chunk(40), + R.map((line) => line.map(toPixel).join("") + "\n"), + (lines) => lines.join("") + ); }; diff --git a/2022/10/test.js b/2022/10/test.js index c0cac25..3910954 100644 --- a/2022/10/test.js +++ b/2022/10/test.js @@ -2,41 +2,55 @@ import { test, expect, describe } from "vitest"; import * as R from "remeda"; import { expectSolution } from "../01/main.js"; -import part1, { comsys, sample, sample2, puzzleInput, addSignal } from "./part1.js"; +import part1, { + comsys, + sample, + sample2, + puzzleInput, + addSignal, +} from "./part1.js"; import part2 from "./part2.js"; describe("part 1", () => { - test( "comsys", () => { - let sys = comsys(); - const result = sample.map( sys).map(R.prop('register')); + test("comsys", () => { + let sys = comsys(); + const result = sample.map(sys).map(R.prop("register")); - expect(result).toEqual([1,1,4]); - - sys = comsys(); - expect(sample.map( sys ).map(R.prop('cycle')) - - ).toEqual([2,3,4] ) - - }); - test( "signal strength", () => { - expect(addSignal({cycle: 3, register: 2})).toMatchObject({signal: 6}); - - }); - test( "sample2", () => { + expect(result).toEqual([1, 1, 4]); + sys = comsys(); + expect(sample.map(sys).map(R.prop("cycle"))).toEqual([2, 3, 4]); + }); + test("signal strength", () => { + expect(addSignal({ cycle: 3, register: 2 })).toMatchObject({ signal: 6 }); + }); + test("sample2", () => { expect(part1(sample2)).toEqual(13140); - - }); + }); test("solution", () => { expectSolution(part1(puzzleInput)).toEqual(15020); }); }); -describe.only("part 2", () => { +describe("part 2", () => { test("sample", () => { - expectSolution(part2(sample2)).toEqual("TODO"); + expect(part2(sample2)).toEqual(`##..##..##..##..##..##..##..##..##..##.. +###...###...###...###...###...###...###. +####....####....####....####....####.... +#####.....#####.....#####.....#####..... +######......######......######......#### +#######.......#######.......#######..... +`); }); - test.todo("solution", () => { - expectSolution(part2(puzzleInput)).toEqual("TODO"); + test("solution", () => { + expectSolution(part2(puzzleInput)).toEqual( + `####.####.#..#..##..#....###...##..###.. +#....#....#..#.#..#.#....#..#.#..#.#..#. +###..###..#..#.#....#....#..#.#..#.#..#. +#....#....#..#.#.##.#....###..####.###.. +#....#....#..#.#..#.#....#....#..#.#.... +####.#.....##...###.####.#....#..#.#.... +` + ); }); });