Skip to content

Commit

Permalink
Remove uncompleted 2023/day05/part2
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Dec 5, 2024
1 parent ea5e4a0 commit c40fdd2
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 119 deletions.
14 changes: 6 additions & 8 deletions 2023/day05/part1.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { isInRange, loadData, mapToInt } from '@helper';
import { isInRange, loadData, mapToInt, words } from '@helper';

const input = await loadData();

const getNumbers = (block: string) =>
block
.split('\n')
.slice(1)
.map((l) => mapToInt(l.split(' '))) as [number, number, number][];
.map((l) => mapToInt(words(l))) as [number, number, number][];

const convert = (input: number, maps: [number, number, number][]) => {
const appliedMaps = maps.filter((m) =>
Expand All @@ -21,16 +21,14 @@ const convert = (input: number, maps: [number, number, number][]) => {
};

const getLocationForSeed = (seed: number, blocks: string[]) => {
let number = seed;
for (const block of blocks) {
number = convert(number, getNumbers(block));
}
return number;
return blocks.reduce(
(number, block) => convert(number, getNumbers(block)),
seed,
);
};

const blocks = input.split('\n\n');
const seeds = mapToInt(blocks.shift()?.match(/\d+/g) ?? []);

const locations = seeds.map((s) => getLocationForSeed(s, blocks));

console.log(Math.min(...locations));
46 changes: 0 additions & 46 deletions 2023/day05/part2.ts

This file was deleted.

29 changes: 29 additions & 0 deletions 2024/day05/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { lines, mapToInt } from '@helper';

export const parse = (input: string) => {
const [rulesString, instructionsString] = input.split('\n\n', 2);

const rules = lines(rulesString, (l) => mapToInt(l.split('|', 2)));

return {
instructions: lines(instructionsString, (l) => mapToInt(l.split(','))),
rulesGraph: rules.reduce<Record<number, number[]>>(
(acc, [r1, r2]) => ({
...acc,
[r1]: [...(acc[r1] ?? []), r2],
}),
{},
),
};
};

export const comparePages =
(rulesGraph: Record<number, number[]>) => (a: number, b: number) => {
if ((rulesGraph[a] ?? []).includes(b)) {
return -1;
}
if ((rulesGraph[b] ?? []).includes(a)) {
return 1;
}
return 0;
};
37 changes: 4 additions & 33 deletions 2024/day05/part1.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,12 @@
import { isSorted, lines, loadData, mapToInt, sumBy } from '@helper';
import { isSorted, loadData, sumBy } from '@helper';
import { comparePages, parse } from './lib';

const input = await loadData();

const parse = (input: string) => {
const [rulesString, instructionsString] = input.split('\n\n', 2);

const rules = lines(rulesString, (l) => mapToInt(l.split('|', 2)));

return {
instructions: lines(instructionsString, (l) => mapToInt(l.split(','))),
rulesGraph: rules.reduce<Record<number, number[]>>(
(acc, [r1, r2]) => ({
...acc,
[r1]: [...(acc[r1] ?? []), r2],
}),
{},
),
};
};

const { instructions, rulesGraph } = parse(input);

const isCorrectlySorted = (instruction: number[]) => {
return isSorted(instruction, (a, b) => {
if ((rulesGraph[a] ?? []).includes(b)) {
return -1;
}
if ((rulesGraph[b] ?? []).includes(a)) {
return 1;
}
return 0;
});
};
const { rulesGraph, instructions } = parse(input);

console.log(
sumBy(
instructions.filter(isCorrectlySorted),
instructions.filter((ins) => isSorted(ins, comparePages(rulesGraph))),
(instruction) => instruction[(instruction.length - 1) / 2],
),
);
36 changes: 5 additions & 31 deletions 2024/day05/part2.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,13 @@
import { quickSelect, isSorted, lines, loadData, mapToInt, sumBy } from '@helper';
import { quickSelect, isSorted, loadData, sumBy } from '@helper';
import { parse, comparePages } from './lib';

const input = await loadData();

const parse = (input: string) => {
const [rulesString, instructionsString] = input.split('\n\n', 2);

const rules = lines(rulesString, (l) => mapToInt(l.split('|', 2)));

return {
instructions: lines(instructionsString, (l) => mapToInt(l.split(','))),
rulesGraph: rules.reduce<Record<number, number[]>>(
(acc, [r1, r2]) => ({
...acc,
[r1]: [...(acc[r1] ?? []), r2],
}),
{},
),
};
};

const { rulesGraph, instructions } = parse(input);

const comparePages = (a: number, b: number) => {
if ((rulesGraph[a] ?? []).includes(b)) {
return -1;
}
if ((rulesGraph[b] ?? []).includes(a)) {
return 1;
}
return 0;
};
const compare = comparePages(rulesGraph);

console.log(
sumBy(
instructions.filter((ins) => !isSorted(ins, comparePages)),
(ins) => quickSelect(ins, (ins.length - 1) / 2, comparePages),
instructions.filter((ins) => !isSorted(ins, compare)),
(ins) => quickSelect(ins, (ins.length - 1) / 2, compare),
),
);
8 changes: 8 additions & 0 deletions helpers/matrix/getCoordinate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ export const findItemCoordinates = <T>(

return null;
};

export const applyOffset = (
coordinate: Coordinate,
[colOffset, rowOffset]: [number, number],
) => ({
row: coordinate.row + rowOffset,
col: coordinate.col + colOffset,
});
2 changes: 1 addition & 1 deletion runNewest
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ while [ ! -f "$PROJECT_ROOT/README.md" ]; do
fi
done

NEWEST_PATH=$(find $PROJECT_ROOT/20* -type f -name '*.ts' | sort -r | head -n 1 | sed "s|^$PROJECT_ROOT/||" | sed "s/day//; s/part//; s/\.ts//")
NEWEST_PATH=$(find $PROJECT_ROOT/20* -type f -name 'part*.ts' | sort -r | head -n 1 | sed "s|^$PROJECT_ROOT/||" | sed "s/day//; s/part//; s/\.ts//")
IFS='/' read -r YEAR DAY PART <<< "$NEWEST_PATH"

usage() {
Expand Down

0 comments on commit c40fdd2

Please sign in to comment.