main
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

@ -15,49 +15,48 @@ export const sample = readInput(import.meta.url, "sample");
export const sample2 = readInput(import.meta.url, "sample.2"); export const sample2 = readInput(import.meta.url, "sample.2");
export const comsys = () => { export const comsys = () => {
let cycle = 1; let cycle = 1;
let register = 1; let register = 1;
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++;
register += scheduled.shift() ?? 0;
return { cycle, register };
} }
cycle++;
} register += scheduled.shift() ?? 0;
return { cycle, register };
};
};
export const gatherStrength = () => { export const gatherStrength = () => {
let strength = 0; let strength = 0;
return ({cycle, signal, register}) => { return ({ cycle, signal, register }) => {
if( (cycle - 20) % 40 === 0 ) { if ((cycle - 20) % 40 === 0) {
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();
return R.pipe( instructions = [...instructions];
R.range(0,220),
R.map( () => sys(instructions.shift()) ),
R.map( addSignal ),
R.map( gatherStrength() ),
R.last,
R.prop('strength')
)
return R.pipe(
R.range(0, 220),
R.map(() => sys(instructions.shift())),
R.map(addSignal),
R.map(gatherStrength()),
R.last,
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();
return R.pipe(
R.range(0,3),
R.map( () => sys(instructions.shift()) ),
)
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("")
);
}; };

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();
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", () => {
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); 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(
`####.####.#..#..##..#....###...##..###..
#....#....#..#.#..#.#....#..#.#..#.#..#.
###..###..#..#.#....#....#..#.#..#.#..#.
#....#....#..#.#.##.#....###..####.###..
#....#....#..#.#..#.#....#....#..#.#....
####.#.....##...###.####.#....#..#.#....
`
);
}); });
}); });