This commit is contained in:
Yanick Champoux 2022-12-10 18:25:50 -05:00
parent 31fab2a508
commit 99e244c054
3 changed files with 98 additions and 63 deletions

View File

@ -20,17 +20,16 @@ export const comsys = () => {
const scheduled = []; const scheduled = [];
return (instr) => { return (instr) => {
if( instr && (instr[0] === 'addx') ) { if (instr && instr[0] === "addx") {
scheduled.push(0, instr[1]); scheduled.push(0, instr[1]);
} else { } else if (instr) {
scheduled.push(0); scheduled.push(0);
} }
cycle++; cycle++;
register += scheduled.shift() ?? 0; register += scheduled.shift() ?? 0;
return { cycle, register }; return { cycle, register };
} };
};
}
export const gatherStrength = () => { export const gatherStrength = () => {
let strength = 0; let strength = 0;
@ -39,25 +38,25 @@ export const gatherStrength = () => {
strength += signal; strength += signal;
} }
return { strength, cycle, signal, register }; return { strength, cycle, signal, register };
} };
} };
export const addSignal = (input) => ({ export const addSignal = (input) => ({
...input, ...input,
signal: input.cycle*input.register signal: input.cycle * input.register,
}); });
export default (instructions) => { export default (instructions) => {
const sys = comsys(); const sys = comsys();
instructions = [...instructions];
return R.pipe( return R.pipe(
R.range(0, 220), R.range(0, 220),
R.map(() => sys(instructions.shift())), R.map(() => sys(instructions.shift())),
R.map(addSignal), R.map(addSignal),
R.map(gatherStrength()), R.map(gatherStrength()),
R.last, R.last,
R.prop('strength') R.prop("strength")
) );
}; };

View File

@ -1,13 +1,35 @@
import * as R from "remeda"; 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) => { export default (instructions) => {
const sys = comsys(); const sys = comsys();
instructions = [...instructions];
return R.pipe( return R.pipe(
R.range(0,3), [sys(), ...R.range(0, 239).map(() => sys(instructions.shift()))],
R.map( () => sys(instructions.shift()) ), R.chunk(40),
) R.map((line) => line.map(toPixel).join("") + "\n"),
(lines) => lines.join("")
);
}; };

View File

@ -2,41 +2,55 @@ import { test, expect, describe } from "vitest";
import * as R from "remeda"; import * as R from "remeda";
import { expectSolution } from "../01/main.js"; 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"; import part2 from "./part2.js";
describe("part 1", () => { describe("part 1", () => {
test("comsys", () => { test("comsys", () => {
let sys = comsys(); let sys = comsys();
const result = sample.map( sys).map(R.prop('register')); const result = sample.map(sys).map(R.prop("register"));
expect(result).toEqual([1, 1, 4]); expect(result).toEqual([1, 1, 4]);
sys = comsys(); sys = comsys();
expect(sample.map( sys ).map(R.prop('cycle')) expect(sample.map(sys).map(R.prop("cycle"))).toEqual([2, 3, 4]);
).toEqual([2,3,4] )
}); });
test("signal strength", () => { test("signal strength", () => {
expect(addSignal({ cycle: 3, register: 2 })).toMatchObject({ signal: 6 }); expect(addSignal({ cycle: 3, register: 2 })).toMatchObject({ signal: 6 });
}); });
test("sample2", () => { test("sample2", () => {
expect(part1(sample2)).toEqual(13140); expect(part1(sample2)).toEqual(13140);
}); });
test("solution", () => { test("solution", () => {
expectSolution(part1(puzzleInput)).toEqual(15020); expectSolution(part1(puzzleInput)).toEqual(15020);
}); });
}); });
describe.only("part 2", () => { describe("part 2", () => {
test("sample", () => { test("sample", () => {
expectSolution(part2(sample2)).toEqual("TODO"); expect(part2(sample2)).toEqual(`##..##..##..##..##..##..##..##..##..##..
###...###...###...###...###...###...###.
####....####....####....####....####....
#####.....#####.....#####.....#####.....
######......######......######......####
#######.......#######.......#######.....
`);
}); });
test.todo("solution", () => { test("solution", () => {
expectSolution(part2(puzzleInput)).toEqual("TODO"); expectSolution(part2(puzzleInput)).toEqual(
`####.####.#..#..##..#....###...##..###..
#....#....#..#.#..#.#....#..#.#..#.#..#.
###..###..#..#.#....#....#..#.#..#.#..#.
#....#....#..#.#.##.#....###..####.###..
#....#....#..#.#..#.#....#....#..#.#....
####.#.....##...###.####.#....#..#.#....
`
);
}); });
}); });