-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
13 changed files
with
192 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
24889 | ||
32129 | ||
95111 |
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 |
---|---|---|
@@ -1,8 +1,99 @@ | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
import { Direction, task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json'; | ||
|
||
export const p1 = (_input: string): number => { | ||
return 0; | ||
export const p1 = (input: string): number => { | ||
const graph = input.toGridGraph<number>({ | ||
valueConverter: (value) => Number.parseInt(value, 10), | ||
weighter: (_from, to, _dir) => to.value, | ||
}); // heat loss | ||
|
||
const aabb = graph.boundingBox(); | ||
const start = graph.getNode(aabb.topLeft); | ||
const end = graph.getNode(aabb.bottomRight); | ||
|
||
if (!start || !end) { | ||
throw new Error('No start or end node!'); | ||
} | ||
|
||
const path = graph.aStar(start, end, { | ||
heuristic: (node) => node.coordinate.manhattan(end.coordinate), | ||
edgeGenerator: (nodeMap, from, tentativePath) => { | ||
const maxConsecutivePathInTheSameDirection = 4; | ||
const lastThree = tentativePath.slice(-maxConsecutivePathInTheSameDirection); | ||
const whenAllTheSame = lastThree | ||
.slideWindow(2) | ||
.map(([a, b]) => a.directionTo(b)) | ||
.reduceIfAllTheSame(); // todo: add a minlength filter | ||
|
||
const last = tentativePath.at(-2); | ||
//&& last?.directionTo(from)?.equals(d.reverse()) | ||
return Direction.cardinalDirections | ||
.filter((d) => | ||
whenAllTheSame && lastThree.length === maxConsecutivePathInTheSameDirection | ||
? !d.equals(whenAllTheSame) | ||
: true, | ||
) | ||
.filterMap((direction) => { | ||
const to = nodeMap.get(from.coordinate.add(direction).toString()); | ||
return to | ||
? { | ||
direction, | ||
from, | ||
to, | ||
weight: to.value, | ||
} | ||
: undefined; | ||
}); | ||
}, | ||
/*currentPathWeighter: (from, to, direction, tentativePath) => { | ||
//console.log( | ||
// 'tentativePath', | ||
// tentativePath.map((a) => a.coordinate.toString() + ' ' + a.value), | ||
//); | ||
//console.log('a', from.value, from.coordinate.toString()); | ||
//console.log('b', to.value, to.coordinate.toString()); | ||
const maxConsecutivePathInTheSameDirection = 4; | ||
const lastThree = tentativePath.slice(-maxConsecutivePathInTheSameDirection); | ||
//console.log( | ||
// 'lastThree', | ||
// lastThree.map((a) => a.coordinate.toString() + ' ' + a.value), | ||
//); | ||
const whenAllTheSame = lastThree | ||
.slideWindow(2) | ||
.map(([a, b]) => a.directionTo(b)) | ||
.reduceIfAllTheSame(); | ||
if ( | ||
lastThree.length === maxConsecutivePathInTheSameDirection && | ||
whenAllTheSame && | ||
direction.equals(whenAllTheSame) | ||
) { | ||
return 100; | ||
} | ||
const last = tentativePath.at(-2); | ||
if (last === from) { | ||
console.log('WHATTEFEFEFEFEF'); | ||
} | ||
if (last?.directionTo(from)?.equals(direction.reverse())) { | ||
return 100; | ||
} | ||
console.log('VAL'); | ||
return to.value; | ||
},*/ | ||
}); | ||
|
||
graph.printPath(path.path); | ||
|
||
return path.path | ||
.slice(1) | ||
.map((node) => node.value) | ||
.sum(); | ||
}; | ||
|
||
await task(p1, packageJson.aoc); // 0 ~0ms | ||
// 1014 too high |
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
24 changes: 24 additions & 0 deletions
24
solutions/typescript/libs/lib/src/array/reduce-if-all-the-same.function.spec.ts
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,24 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { reduceIfAllTheSame } from './reduce-if-all-the-same.function.js'; | ||
|
||
describe('reduceIfAllTheSame', () => { | ||
it('should return undefined for empty arrays', () => { | ||
const result = reduceIfAllTheSame<unknown>([]); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined for arrays where items are different', () => { | ||
const result = reduceIfAllTheSame([1, 1, 2]); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it('should return 1 for arrays where items are 1', () => { | ||
const result = reduceIfAllTheSame([1, 1, 1]); | ||
expect(result).toEqual(1); | ||
}); | ||
|
||
it('should return 1 for arrays where there is only one item', () => { | ||
const result = reduceIfAllTheSame([1]); | ||
expect(result).toEqual(1); | ||
}); | ||
}); |
4 changes: 4 additions & 0 deletions
4
solutions/typescript/libs/lib/src/array/reduce-if-all-the-same.function.ts
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,4 @@ | ||
export const reduceIfAllTheSame = <T>(array: T[]): T | undefined => { | ||
const first = array.first(); | ||
return array.every((i) => i === first) ? first : undefined; | ||
}; |
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
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