-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
72 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters