Skip to content

Commit

Permalink
Solve 2024/day02
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Dec 2, 2024
1 parent 78eac1f commit 7fc6a5e
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 7 deletions.
3 changes: 2 additions & 1 deletion 2024/day01/part1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import {
words,
mapToInt,
sort,
self,
} from '@helper';

const input = await loadData();
const parsed = lines(input, (l) => mapToInt(words(l)));
const nums = transpose(parsed).map((x) => sort(x, (x) => x));
const nums = transpose(parsed).map((x) => sort(x, self));

console.log(sumBy(nums[0], (num1, index) => Math.abs(num1 - nums[1][index])));
15 changes: 15 additions & 0 deletions 2024/day02/part1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { lines, loadData, words, mapToInt, pair, isInRange } from '@helper';

const input = await loadData();
const parsed = lines(input, (l) => mapToInt(words(l)));

const isSafe = (nums: number[]) => {
const diffs = pair(nums).map(([num1, num2]) => num1 - num2);

const increasing = diffs.every((d) => isInRange(1, d, 3));
const decreasing = diffs.every((d) => isInRange(-3, d, -1));

return increasing || decreasing;
};

console.log(parsed.filter(isSafe).length);
30 changes: 30 additions & 0 deletions 2024/day02/part2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
lines,
loadData,
words,
mapToInt,
pair,
isInRange,
range,
removeIndex,
} from '@helper';

const input = await loadData();
const parsed = lines(input, (l) => mapToInt(words(l)));

const isSafe = (nums: number[]) => {
const diffs = pair(nums).map(([num1, num2]) => num1 - num2);

const increasing = diffs.every((d) => isInRange(1, d, 3));
const decreasing = diffs.every((d) => isInRange(-3, d, -1));

return increasing || decreasing;
};

const saveEntries = parsed.filter((nums) => {
const removedNums = range(nums.length).map((i) => removeIndex(nums, i));
const allNums = [nums, ...removedNums];
return allNums.some(isSafe);
});

console.log(saveEntries.length);
2 changes: 2 additions & 0 deletions helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export * from './matrix/getCoordinate';
export * from './matrix/getNextToCoordinate';
export * from './matrix/newMatrix';
export * from './sort';
export * from './removeIndex';
// TODO: Test set value at coord
export * from './matrix/setValueAtCoord';
export * from './commonChars';
Expand Down Expand Up @@ -64,3 +65,4 @@ export {
lines,
words,
};
export const self = <T>(x: T) => x
11 changes: 11 additions & 0 deletions helpers/removeIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Removes the element at the specified index from an array without modifying the original array.
*
* @param arr - The original array.
* @param index - The index of the element to remove.
* @returns A new array with the specified element removed.
*/
export const removeIndex = <T>(arr: T[], index: number) => [
...arr.slice(0, index),
...arr.slice(index + 1),
];
6 changes: 6 additions & 0 deletions input/2024/day02.debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
12 changes: 6 additions & 6 deletions runNewest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ DAY=1
PART=1

DATA=debug
SUBMIT=false
COPY=false

PROJECT_ROOT="$PWD"
while [ ! -f "$PROJECT_ROOT/README.md" ]; do
Expand All @@ -25,7 +25,7 @@ usage() {
echo " -d, --day Day (default: 1) (range: 1-25)"
echo " -p, --part Part (default: 1) (options: 1 2)"
echo " -D --data Use real input data or just debug (default: debug) (options: debug solve)"
echo " --submit Submit the answer (default: false)"
echo " -c --copy Copy the answer (default: false)"
exit 1
}

Expand Down Expand Up @@ -75,8 +75,8 @@ while [[ $# -gt 0 ]]; do
DATA="$2"
shift 2
;;
--submit)
SUBMIT=true
-c|--copy)
COPY=true
shift
;;
*)
Expand Down Expand Up @@ -125,6 +125,6 @@ export PART=$PART

SOLUTION=$(bun run "$FILE_PATH" "$INPUT_PATH" | tee /dev/tty)

if [ "$SUBMIT" == true ]; then
aoc submit -d "$DAY" -y "$YEAR" "$PART" "$SOLUTION"
if [ "$COPY" == true ]; then
echo "$SOLUTION" | sed -r 's/\x1B\[[0-9;]*m//g' | wl-copy
fi

0 comments on commit 7fc6a5e

Please sign in to comment.