-
-
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
7 changed files
with
71 additions
and
77 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 |
---|---|---|
@@ -1,24 +1,25 @@ | ||
import { DOUBLE_NEWLINE, task } from '@alexaegis/advent-of-code-lib'; | ||
import { DOUBLE_NEWLINE, isNotNullish, task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json'; | ||
import { findReflection } from './internal/matrix-reflection.js'; | ||
|
||
export const p1 = (input: string): number => | ||
input | ||
.split(DOUBLE_NEWLINE) | ||
.map((m) => m.toGridGraph()) | ||
.map((graph) => { | ||
const columnReflectionIndex = findReflection(graph, 'column'); | ||
const rowReflectionIndex = findReflection(graph, 'row'); | ||
.map((m) => m.toMatrix()) | ||
.map((matrix) => { | ||
const rowWise = matrix; | ||
const columnWise = matrix.rotateMatrix('r'); | ||
const rowReflectionIndex = findReflection(rowWise); | ||
const columnReflectionIndex = findReflection(columnWise); | ||
|
||
let value = 0; | ||
if (columnReflectionIndex !== undefined) { | ||
value = columnReflectionIndex + 1; | ||
if (isNotNullish(columnReflectionIndex)) { | ||
return columnReflectionIndex + 1; | ||
} else if (isNotNullish(rowReflectionIndex)) { | ||
return (rowReflectionIndex + 1) * 100; | ||
} else { | ||
throw new Error('Reflection not found'); | ||
} | ||
if (rowReflectionIndex !== undefined) { | ||
value = (rowReflectionIndex + 1) * 100; | ||
} | ||
return value; | ||
}) | ||
.sum(); | ||
|
||
await task(p1, packageJson.aoc); // 37718 ~371.96ms | ||
await task(p1, packageJson.aoc); // 37718 ~2.59ms |
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,27 +1,28 @@ | ||
import { DOUBLE_NEWLINE, task } from '@alexaegis/advent-of-code-lib'; | ||
import { DOUBLE_NEWLINE, isNotNullish, task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json'; | ||
import { findReflection } from './internal/matrix-reflection.js'; | ||
|
||
export const p2 = (input: string): number => { | ||
return input | ||
export const p2 = (input: string): number => | ||
input | ||
.split(DOUBLE_NEWLINE) | ||
.map((m) => m.toGridGraph()) | ||
.map((graph) => { | ||
const smudgyRowIndex = findReflection(graph, 'row'); | ||
const smudgyColumnIndex = findReflection(graph, 'column'); | ||
const rowIndex = findReflection(graph, 'row', 1, smudgyRowIndex); | ||
const columnIndex = findReflection(graph, 'column', 1, smudgyColumnIndex); | ||
.map((m) => m.toMatrix()) | ||
.map((matrix) => { | ||
const rowWise = matrix; | ||
const columnWise = matrix.rotateMatrix('r'); | ||
|
||
let value = 0; | ||
if (columnIndex !== undefined) { | ||
value = columnIndex + 1; | ||
} | ||
if (rowIndex !== undefined) { | ||
value = (rowIndex + 1) * 100; | ||
const smudgyRowIndex = findReflection(rowWise); | ||
const smudgyColumnIndex = findReflection(columnWise); | ||
const rowIndex = findReflection(rowWise, 1, smudgyRowIndex); | ||
const columnIndex = findReflection(columnWise, 1, smudgyColumnIndex); | ||
|
||
if (isNotNullish(columnIndex)) { | ||
return columnIndex + 1; | ||
} else if (isNotNullish(rowIndex)) { | ||
return (rowIndex + 1) * 100; | ||
} else { | ||
throw new Error('Reflection not found'); | ||
} | ||
return value; | ||
}) | ||
.sum(); | ||
}; | ||
|
||
await task(p2, packageJson.aoc); // 40995 ~658.23ms | ||
await task(p2, packageJson.aoc); // 40995 ~4.95ms |
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