-
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
10 changed files
with
128 additions
and
16 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
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 @@ | ||
export const engraveStone = (stone: number) => { | ||
if (stone === 0) { | ||
return [1]; | ||
} | ||
const stoneString = `${stone}`; | ||
if (stoneString.length % 2 === 0) { | ||
const middlePoint = stoneString.length / 2; | ||
return [ | ||
parseInt(stoneString.slice(0, middlePoint)), | ||
parseInt(stoneString.slice(middlePoint)), | ||
]; | ||
} | ||
|
||
return [stone * 2024]; | ||
}; |
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,29 @@ | ||
import { loadData, mapToInt, showVisualization, words } from '@helper'; | ||
import { engraveStone } from './lib'; | ||
|
||
const input = await loadData(); | ||
const initialStones = mapToInt(words(input)); | ||
|
||
const cache = new Map<number, number[]>(); | ||
|
||
const cachedEngraving = (stone: number) => { | ||
const cached = cache.get(stone); | ||
if (cached) { | ||
return cached; | ||
} | ||
|
||
const calculated = engraveStone(stone); | ||
cache.set(stone, calculated); | ||
return calculated; | ||
}; | ||
|
||
let stones = initialStones; | ||
for (let i = 0; i < 25; i++) { | ||
stones = stones.flatMap(cachedEngraving); | ||
} | ||
|
||
if (showVisualization()) { | ||
console.log(stones.join(' ')); | ||
} | ||
|
||
console.log(stones.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,36 @@ | ||
import { | ||
countGroups, | ||
loadData, | ||
mapToInt, | ||
mapValues, | ||
showVisualization, | ||
sum, | ||
mergeObjects, | ||
words, | ||
} from '@helper'; | ||
import { engraveStone } from './lib'; | ||
|
||
const input = await loadData(); | ||
const stones = mapToInt(words(input)); | ||
|
||
function proccessStoneCount(stoneCounts: Record<string | number, number>) { | ||
const counts = Object.entries(stoneCounts).map(([stone, count]) => { | ||
const newStones = engraveStone(parseInt(stone)); | ||
return mapValues(countGroups(newStones), (v) => v! * count); | ||
}); | ||
return mergeObjects(counts, sum); | ||
} | ||
|
||
let counts = countGroups(stones) as Record<number, number>; | ||
for (let i = 0; i < 75; i++) { | ||
counts = proccessStoneCount(counts); | ||
} | ||
|
||
if (showVisualization()) { | ||
const stoneGroups = Object.entries(counts).map(([stone, length]) => | ||
Array.from({ length }, () => stone).join(' '), | ||
); | ||
console.log(stoneGroups.join(' ')); | ||
} | ||
|
||
console.log(sum(Object.values(counts))); |
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,26 @@ | ||
import { unique } from './unique'; | ||
|
||
// TODO: Test and document it | ||
// TODO: preserve key type (numbers are turned to strings) | ||
|
||
export function mergeObjects<T>( | ||
objects: Record<string | number, T>[], | ||
conflictResolver: (values: T[]) => T, | ||
) { | ||
const keys = unique(objects.flatMap(Object.keys)); | ||
|
||
const merged: Record<string, T> = {}; | ||
|
||
for (const key of keys) { | ||
const initialValues = objects | ||
.filter((obj) => key in obj) | ||
.map((obj) => obj[key]); | ||
const newValue = | ||
initialValues.length === 1 | ||
? initialValues[0] | ||
: conflictResolver(initialValues); | ||
merged[key] = newValue; | ||
} | ||
|
||
return merged; | ||
} |
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 @@ | ||
125 17 |
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