-
-
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
12 changed files
with
115 additions
and
452 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"schemaVersion": 1, | ||
"label": "Advent of TypeScript 2023", | ||
"message": "14/25", | ||
"message": "15/25", | ||
"color": "orange" | ||
} |
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
20 changes: 20 additions & 0 deletions
20
solutions/typescript/2023/13/src/internal/matrix-reflection.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,20 @@ | ||
import { Interval } from '@alexaegis/advent-of-code-lib'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { findReflectivePairings } from './matrix-reflection.js'; | ||
|
||
describe('findReflectivePairings', () => { | ||
it('should be able to collect reflective pairs from the middle', () => { | ||
const result = findReflectivePairings(Interval.closed(0, 8), 4); | ||
expect(result.length).toEqual(4); | ||
expect(result[0]).toEqual([4, 5]); | ||
expect(result[1]).toEqual([3, 6]); | ||
expect(result[2]).toEqual([2, 7]); | ||
expect(result[3]).toEqual([1, 8]); | ||
}); | ||
|
||
it('should be able to collect reflective pairs from the start', () => { | ||
const result = findReflectivePairings(Interval.closed(0, 8), 0); | ||
expect(result.length).toEqual(1); | ||
expect(result[0]).toEqual([0, 1]); | ||
}); | ||
}); |
61 changes: 61 additions & 0 deletions
61
solutions/typescript/2023/13/src/internal/matrix-reflection.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,61 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import type { GridGraph, GridGraphNode, Interval, ToString } from '@alexaegis/advent-of-code-lib'; | ||
|
||
export const matchingNodes = < | ||
T extends ToString = string, | ||
N extends GridGraphNode<T> = GridGraphNode<T>, | ||
>( | ||
a: N[], | ||
b: N[], | ||
): number => { | ||
return a.zip(b).filter(([an, bn]) => an.value === bn.value).length; | ||
}; | ||
|
||
export const findReflectivePairings = ( | ||
interval: Interval, | ||
reflectionOrigin: number, | ||
): [number, number][] => { | ||
return reflectionOrigin | ||
.interval(interval.high) | ||
.collectValues() | ||
.filterMap((right) => { | ||
const left = reflectionOrigin - (right - reflectionOrigin); | ||
return left >= interval.low ? [left, right + 1] : undefined; | ||
}); | ||
}; | ||
|
||
export const findReflection = < | ||
T extends ToString = string, | ||
N extends GridGraphNode<T> = GridGraphNode<T>, | ||
>( | ||
gg: GridGraph<T, N>, | ||
axis: 'row' | 'column', | ||
smudges = 0, | ||
skip?: number, | ||
): number | undefined => { | ||
const aabb = gg.boundingBox(); | ||
const axisInterval = axis === 'column' ? aabb.horizontal : aabb.vertical; | ||
const otherAxistInterval = axis === 'column' ? aabb.vertical : aabb.horizontal; | ||
const getNodesOfAxis = (n: number) => (axis === 'column' ? gg.getColumn(n) : gg.getRow(n)); | ||
return axisInterval.collectValues().find((index) => { | ||
if (skip !== undefined && index === skip) { | ||
return false; | ||
} | ||
|
||
const pairs = findReflectivePairings(axisInterval, index); | ||
|
||
if (pairs.length === 0) { | ||
return false; | ||
} | ||
|
||
const matching = pairs | ||
.map(([left, right]) => { | ||
const leftNodes = getNodesOfAxis(left)!; | ||
const rightNodes = getNodesOfAxis(right)!; | ||
return matchingNodes<T, N>(leftNodes, rightNodes); | ||
}) | ||
.sum(); | ||
|
||
return matching + smudges === pairs.length * otherAxistInterval.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
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
Oops, something went wrong.