Skip to content

Commit 4593a7a

Browse files
committed
refactor
1 parent c85fb43 commit 4593a7a

File tree

5 files changed

+21
-34
lines changed

5 files changed

+21
-34
lines changed

src/2015/day22.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { memoize } from "../utils/memoize.js";
2+
13
const spells = {
24
Missile: {
35
mana: 53,
@@ -76,14 +78,6 @@ function playBoss(game) {
7678
};
7779
}
7880

79-
function memoize(fn) {
80-
let memo = {};
81-
return (...x) => {
82-
let s = JSON.stringify(x);
83-
return (memo[s] = memo[s] ?? fn(...x));
84-
};
85-
}
86-
8781
let play = memoize(game => {
8882
if (game.boss.hit <= 0) {
8983
return 0;

src/2023/day12.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
function memoize(fn) {
2-
let memo = {};
3-
return (...x) => {
4-
let s = JSON.stringify(x);
5-
return (memo[s] = memo[s] ?? fn(...x));
6-
};
7-
}
1+
import { memoize } from "../utils/memoize.js";
82

93
function advance(options, char) {
104
let { len, groups, i, left } = options;

src/2025/day09.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export function part2(input) {
4141
}
4242

4343
const disjoint = (a1, a2, b1, b2) =>
44-
(a1 <= b1 && a1 <= b2 && a2 <= b1 && a2 <= b2) ||
45-
(a1 >= b1 && a1 >= b2 && a2 >= b1 && a2 >= b2);
44+
Math.max(a1, a2) <= Math.min(b1, b2) ||
45+
Math.max(b1, b2) <= Math.min(a1, a2);
4646

4747
return squares.find(square => {
4848
return sides.every(

src/2025/day11.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1-
function memoize(fn) {
2-
let memo = {};
3-
return (...x) => {
4-
let s = JSON.stringify(x);
5-
return (memo[s] = memo[s] ?? fn(...x));
6-
};
7-
}
1+
import { memoize } from "../utils/memoize.js";
82

93
let solve = memoize((devices, curr, dac = true, fft = true) => {
104
if (curr === "out") return dac && fft ? 1 : 0;
11-
if (curr === "dac") dac = true;
12-
if (curr === "fft") fft = true;
135
return devices[curr].reduce(
14-
(sum, next) => sum + solve(devices, next, dac, fft),
6+
(sum, next) =>
7+
sum + solve(devices, next, dac || curr === "dac", fft || curr === "fft"),
158
0,
169
);
1710
});
1811

1912
function parse(input) {
20-
let devices = {};
21-
input.split("\n").forEach(line => {
22-
let [name, rest] = line.split(": ");
23-
let outputs = rest.split(" ");
24-
devices[name] = outputs;
25-
});
26-
return devices;
13+
return Object.fromEntries(
14+
input.split("\n").map(line => {
15+
let [name, rest] = line.split(": ");
16+
return [name, rest.split(" ")];
17+
}),
18+
);
2719
}
2820

2921
export function part1(input) {

src/utils/memoize.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function memoize(fn) {
2+
let memo = {};
3+
return (...x) => {
4+
let s = JSON.stringify(x);
5+
return (memo[s] = memo[s] ?? fn(...x));
6+
};
7+
}

0 commit comments

Comments
 (0)