Skip to content

Commit

Permalink
Merge pull request #24 from hildjj/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
hildjj authored Dec 11, 2024
2 parents 4c7123e + 10d9372 commit 8cc85e9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
19 changes: 7 additions & 12 deletions day11.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { type MainArgs, parseFile } from './lib/utils.ts';
import { divmod, type MainArgs, parseFile } from './lib/utils.ts';

type Parsed = number[];

function mapAdd(m: Map<number, number>, k: number, v = 1): void {
const se = m.get(k);
m.set(k, (se ?? 0) + v);
m.set(k, (m.get(k) ?? 0) + v);
}

function blink(inp: Parsed, num: number): number {
Expand All @@ -23,22 +22,18 @@ function blink(inp: Parsed, num: number): number {
mapAdd(next, 1, v);
} else {
const ks = String(k);
if (ks.length % 2 === 0) {
const kl = ks.length / 2;
mapAdd(next, Number(ks.slice(0, kl)), v);
mapAdd(next, Number(ks.slice(kl)), v);
const [div, mod] = divmod(ks.length, 2);
if (mod === 0) {
mapAdd(next, Number(ks.slice(0, div)), v);
mapAdd(next, Number(ks.slice(div)), v);
} else {
mapAdd(next, k * 2024, v);
}
}
}
prev = next;
}
let count = 0;
for (const [_k, v] of prev) {
count += v;
}
return count;
return prev.values().reduce((t, v) => t + v, 0);
}

function part1(inp: Parsed): number {
Expand Down
9 changes: 4 additions & 5 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,11 @@ export function adjacentFile(
*/
export function mod<T extends number | bigint>(x: T, y: T): T {
// == works with either 0 or 0n.
// eslint-disable-next-line eqeqeq
if (y === 0) {
// deno-lint-ignore eqeqeq
if (y == 0) {
throw new Error('Division by zero');
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: TS2365. tsc can't see that x and y are always the same type
// @ts-expect-error: TS2365. tsc can't see that x and y are always the same type
return ((x % y) + y) % y;
}

Expand All @@ -195,7 +194,7 @@ export function mod<T extends number | bigint>(x: T, y: T): T {
*/
export function divmod<T extends number | bigint>(x: T, y: T): [T, T] {
let q = (x / y) as unknown as T;
const r: T = mod(x, y);
const r = mod(x, y);
if (typeof x === 'bigint') {
// Not only does Math.floor not work for BigInt, it's not needed because
// `/` does the right thing in the first place.
Expand Down

0 comments on commit 8cc85e9

Please sign in to comment.