Skip to content

Commit 193cbe0

Browse files
committed
refactor: extract mod, lcm, and gcd to lib
1 parent 0cd9032 commit 193cbe0

File tree

9 files changed

+26
-52
lines changed

9 files changed

+26
-52
lines changed

2022/day/24/part/1/solve.ts

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { Grid, ReadonlyGrid } from "../../grid.ts";
22

3+
import lcm from "../../../../../lib/lcm.ts";
4+
import mod from "../../../../../lib/mod.ts";
5+
36
const charToDirection = {
47
"^": { x: 0, y: -1 },
58
"v": { x: 0, y: 1 },
@@ -13,19 +16,6 @@ function parseGrid(text: string): ReadonlyGrid {
1316
);
1417
}
1518

16-
function gcd(a: number, b: number): number {
17-
while (b) [a, b] = [b, a % b];
18-
return a;
19-
}
20-
21-
function lcm(a: number, b: number) {
22-
return a * b / gcd(a, b);
23-
}
24-
25-
function mod(n: number, d: number) {
26-
return ((n % d) + d) % d;
27-
}
28-
2919
function calculateGrid(initialGrid: ReadonlyGrid, step: number): ReadonlyGrid {
3020
const { length: height, 0: { length: width } } = initialGrid;
3121
const result = structuredClone(initialGrid) as Grid;

2022/day/24/part/2/solve.ts

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
import type { Grid, ReadonlyGrid } from "../../grid.ts";
22

3+
import lcm from "../../../../../lib/lcm.ts";
4+
import mod from "../../../../../lib/mod.ts";
5+
36
const charToDirection = {
47
"^": { x: 0, y: -1 },
58
"v": { x: 0, y: 1 },
69
"<": { x: -1, y: 0 },
710
">": { x: 1, y: 0 },
811
};
912

10-
function gcd(a: number, b: number): number {
11-
while (b) [a, b] = [b, a % b];
12-
return a;
13-
}
14-
15-
function lcm(a: number, b: number) {
16-
return a * b / gcd(a, b);
17-
}
18-
19-
function mod(n: number, d: number) {
20-
return ((n % d) + d) % d;
21-
}
22-
2313
function calculateGrid(initialGrid: ReadonlyGrid, step: number): ReadonlyGrid {
2414
const { length: height, 0: { length: width } } = initialGrid;
2515
const result = structuredClone(initialGrid) as Grid;

2023/day/20/part/2/solve.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import lcm from "../../../../../lib/lcm.ts";
2+
13
type Pulse = { source: string; type: "low" | "high"; destination: string };
24

35
type CommonModuleProps = {
@@ -18,15 +20,6 @@ type UntypedModule = CommonModuleProps & {
1820
};
1921
type Module = FlipFlopModule | ConjunctionModule | UntypedModule;
2022

21-
function gcd(a: number, b: number): number {
22-
while (b) [a, b] = [b, a % b];
23-
return a;
24-
}
25-
26-
function lcm(a: number, b: number) {
27-
return a * b / gcd(a, b);
28-
}
29-
3023
export default function solve(input: string) {
3124
const modules = input.split("\n").map<Module>((line) => {
3225
const [left, right] = line.split(" -> ");

2023/day/21/part/2/solve.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { BinaryHeap } from "std/data_structures/binary_heap.ts";
22

3+
import mod from "../../../../../lib/mod.ts";
4+
35
const directions = [
46
{ x: 0, y: 1 },
57
{ x: 0, y: -1 },
68
{ x: -1, y: 0 },
79
{ x: 1, y: 0 },
810
];
911

10-
function mod(a: number, b: number) {
11-
return ((a % b) + b) % b;
12-
}
13-
1412
function _solve(input: string, { steps = 26501365 } = {}) {
1513
const map = input.split("\n");
1614
const { length: height, 0: { length: width } } = map;

2023/day/8/part/2/solve.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import lcm from "../../../../../lib/lcm.ts";
2+
13
function parseDocuments(text: string) {
24
const [instructionsText, networkText] = text.split("\n\n");
35
const instructions = Array.from(
@@ -12,15 +14,6 @@ function parseDocuments(text: string) {
1214
return { instructions, network };
1315
}
1416

15-
function gcd(a: number, b: number): number {
16-
while (b) [a, b] = [b, a % b];
17-
return a;
18-
}
19-
20-
function lcm(a: number, b: number) {
21-
return a * b / gcd(a, b);
22-
}
23-
2417
export default function solve(input: string) {
2518
const { instructions, network } = parseDocuments(input);
2619
return Array.from(network.keys())

2024/day/14/part/1/solve.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import mod from "../../../../../lib/mod.ts";
2+
13
const regExp = /^p=(?<px>\d+),(?<py>\d+) v=(?<vx>-?\d+),(?<vy>-?\d+)$/gm;
24

35
export default function solve(
@@ -15,7 +17,3 @@ export default function solve(
1517
}
1618
return quadrantCounts.reduce((product, count) => product * count, 1);
1719
}
18-
19-
function mod(a: number, b: number) {
20-
return ((a % b) + b) % b;
21-
}

lib/gcd.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default function gcd(a: number, b: number): number {
2+
while (b) [a, b] = [b, a % b];
3+
return a;
4+
}

lib/lcm.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import gcd from "./gcd.ts";
2+
3+
export default function lcm(a: number, b: number) {
4+
return a * b / gcd(a, b);
5+
}

lib/mod.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function mod(n: number, d: number) {
2+
return ((n % d) + d) % d;
3+
}

0 commit comments

Comments
 (0)