-
-
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
5 changed files
with
146 additions
and
151 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": "11/25", | ||
"message": "12/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
import { cartesianCombinations, task } from '@alexaegis/advent-of-code-lib'; | ||
import { task } from '@alexaegis/advent-of-code-lib'; | ||
import packageJson from '../package.json'; | ||
import { parse } from './parse.js'; | ||
|
||
function generateVariations(damagedLog: string, criteria: RegExp): string[] { | ||
const variants = [...damagedLog].map((entry) => (entry === '?' ? ['#', '.'] : [entry])); | ||
return cartesianCombinations(...variants) | ||
.map((combination) => combination.join('')) | ||
.filter((combination) => criteria.test(combination)); | ||
} | ||
import { calculateVariations, parse } from './parse.js'; | ||
|
||
export const p1 = (input: string): number => | ||
parse(input) | ||
.map((spring) => generateVariations(spring.log, spring.criteriaRegExp).length) | ||
.map((entry) => { | ||
const [currentCriteria, ...remainingCriteria] = entry.criteria; | ||
if (!currentCriteria) { | ||
throw new Error('no criteria for line'); | ||
} | ||
return calculateVariations({ | ||
currentCriteria, | ||
currentOriginalCriteria: currentCriteria, | ||
rebuiltLog: '', | ||
remainingCriteria, | ||
remainingDamagedLog: entry.log, | ||
}); | ||
}) | ||
.sum(); | ||
|
||
await task(p1, packageJson.aoc); // 7118 ~0ms | ||
await task(p1, packageJson.aoc); // 7118 ~30.83ms |
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,25 +1,132 @@ | ||
export interface SpringLog { | ||
/** | ||
* `?` unknown | ||
* `.` operational | ||
* `#` damaged | ||
*/ | ||
log: string; | ||
criteria: number[]; | ||
criteriaRegExp: RegExp; | ||
} | ||
|
||
export const criteriaToRegExp = (criteria: number[]): RegExp => | ||
new RegExp('^\\.*' + criteria.map((brokenCount) => `#{${brokenCount}}`).join('\\.+') + '\\.*$'); | ||
|
||
export const parse = (input: string): SpringLog[] => { | ||
return input.lines(false).map<SpringLog>((line) => { | ||
const [log, critRaw] = line.splitIntoStringPair(' '); | ||
const crit = critRaw.splitToInt({ delimiter: /,/ }); | ||
const criteria = critRaw.splitToInt({ delimiter: /,/ }); | ||
return { | ||
log, | ||
criteria: crit, | ||
criteriaRegExp: criteriaToRegExp(crit), | ||
criteria, | ||
}; | ||
}); | ||
}; | ||
|
||
export interface State { | ||
rebuiltLog: string; | ||
currentCriteria: number; | ||
currentOriginalCriteria: number; | ||
remainingDamagedLog: string; | ||
remainingCriteria: number[]; | ||
} | ||
|
||
export const calculateVariations = ( | ||
state: State, | ||
cache: Map<string, number> = new Map(), | ||
): number => { | ||
const key = | ||
state.remainingCriteria.join(',') + | ||
';' + | ||
state.remainingDamagedLog + | ||
';' + | ||
state.currentOriginalCriteria + | ||
';' + | ||
state.currentCriteria; | ||
|
||
const cachedResult = cache.get(key); | ||
if (cachedResult !== undefined) { | ||
return cachedResult; | ||
} | ||
|
||
let result = cachedResult ?? 0; | ||
|
||
while (state.currentCriteria > 0 && state.remainingDamagedLog.startsWith('#')) { | ||
state.currentCriteria = state.currentCriteria - 1; | ||
state.remainingDamagedLog = state.remainingDamagedLog.slice(1); | ||
state.rebuiltLog = state.rebuiltLog + '#'; | ||
} | ||
|
||
if ( | ||
state.currentCriteria === 0 && | ||
state.remainingCriteria.length === 0 && | ||
!state.remainingDamagedLog.includes('#') | ||
) { | ||
state.rebuiltLog += state.remainingDamagedLog.replaceAll('?', '.'); | ||
return 1; | ||
} | ||
|
||
while ( | ||
state.currentCriteria === state.currentOriginalCriteria && | ||
state.remainingDamagedLog.startsWith('.') | ||
) { | ||
state.remainingDamagedLog = state.remainingDamagedLog.slice(1); | ||
state.rebuiltLog = state.rebuiltLog + '.'; | ||
} | ||
|
||
if (state.currentCriteria > 0 && state.remainingDamagedLog.startsWith('.')) { | ||
return 0; | ||
} | ||
|
||
if (state.currentCriteria === 0 && state.remainingDamagedLog.startsWith('#')) { | ||
return 0; | ||
} | ||
|
||
if ( | ||
state.currentCriteria === 0 && | ||
(state.remainingDamagedLog.startsWith('.') || state.remainingDamagedLog.startsWith('?')) | ||
) { | ||
const nextCriteria = state.remainingCriteria.shift(); | ||
|
||
if (nextCriteria === undefined) { | ||
state.currentOriginalCriteria = 0; | ||
} else { | ||
state.currentCriteria = nextCriteria; | ||
state.currentOriginalCriteria = nextCriteria; | ||
} | ||
|
||
state.rebuiltLog = state.rebuiltLog + '.'; | ||
state.remainingDamagedLog = state.remainingDamagedLog.slice(1); | ||
} | ||
|
||
while ( | ||
state.currentCriteria === state.currentOriginalCriteria && | ||
state.remainingDamagedLog.startsWith('.') | ||
) { | ||
state.remainingDamagedLog = state.remainingDamagedLog.slice(1); | ||
state.rebuiltLog = state.rebuiltLog + '.'; | ||
} | ||
|
||
if ( | ||
state.currentCriteria === 0 && | ||
state.remainingDamagedLog.length === 0 && | ||
state.remainingCriteria.length === 0 | ||
) { | ||
return 1; | ||
} | ||
|
||
if (state.remainingDamagedLog.startsWith('?')) { | ||
result += calculateVariations( | ||
{ | ||
...state, | ||
remainingCriteria: [...state.remainingCriteria], | ||
remainingDamagedLog: '#' + state.remainingDamagedLog.slice(1), | ||
}, | ||
cache, | ||
); | ||
|
||
result += calculateVariations( | ||
{ | ||
...state, | ||
remainingCriteria: [...state.remainingCriteria], | ||
remainingDamagedLog: '.' + state.remainingDamagedLog.slice(1), | ||
}, | ||
cache, | ||
); | ||
} else if (state.currentCriteria > 0 && state.remainingDamagedLog.length > 0) { | ||
result += calculateVariations(state, cache); | ||
} | ||
cache.set(key, result); | ||
return result; | ||
}; |
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