part 2
This commit is contained in:
parent
31fab2a508
commit
99e244c054
@ -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")
|
||||
);
|
||||
};
|
||||
|
@ -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("")
|
||||
);
|
||||
};
|
||||
|
@ -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(
|
||||
`####.####.#..#..##..#....###...##..###..
|
||||
#....#....#..#.#..#.#....#..#.#..#.#..#.
|
||||
###..###..#..#.#....#....#..#.#..#.#..#.
|
||||
#....#....#..#.#.##.#....###..####.###..
|
||||
#....#....#..#.#..#.#....#....#..#.#....
|
||||
####.#.....##...###.####.#....#..#.#....
|
||||
`
|
||||
);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user