-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
32ece78
commit 485d694
Showing
1 changed file
with
59 additions
and
0 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,59 @@ | ||
import ISolution from "./ISolution.ts"; | ||
|
||
export default class S2409 implements ISolution { | ||
firstPart(input: string): (number | string) | Promise<number | string> { | ||
const numbers = input.split("").map(Number); | ||
const map: (string | number)[] = []; | ||
for (let i = 0; i < numbers.length / 2; i++) { | ||
const size = numbers[ i * 2 ]; | ||
const space = numbers[ i * 2 + 1 ]; | ||
map.push(...Array.from({ length: size }).fill(i) as number[]); | ||
map.push(...Array.from({ length: space }).fill(".") as string[]); | ||
} | ||
for (let i = map.length - 1; i > 0; i--) { | ||
if (map[ i ] === ".") { | ||
continue; | ||
} | ||
const lastIndex = map.indexOf("."); | ||
if (lastIndex < i) { | ||
map[ lastIndex ] = map[ i ]; | ||
map[ i ] = "."; | ||
} | ||
} | ||
return map.reduce((acc: number, val, index) => { | ||
if (val === ".") { | ||
return acc; | ||
} | ||
return acc + (typeof val === 'number' ? val * index : 0); | ||
}, 0); | ||
} | ||
secondPart(input: string): (number | string) | Promise<number | string> { | ||
const numbers = input.split("").map(Number); | ||
const map: (string | number)[] = []; | ||
for (let i = 0; i < numbers.length / 2; i++) { | ||
const size = numbers[ i * 2 ]; | ||
const space = numbers[ i * 2 + 1 ]; | ||
map.push(...Array.from({ length: size }).fill(i) as number[]); | ||
map.push(...Array.from({ length: space }).fill(".") as string[]); | ||
} | ||
for (let i = map.length - 1; i > 0; i--) { | ||
if (map[ i ] === ".") { | ||
continue; | ||
} | ||
const length = map.filter(val => val === map[i]).length; | ||
const first = map.indexOf(map[i]); | ||
const lastIndex = map.map(x => x === "." ? x : "x").join("").indexOf(".".repeat(length)); | ||
if (lastIndex < i && lastIndex !== -1) { | ||
map.splice(lastIndex, length, ...Array.from({ length }).fill(map[i]) as number[]); | ||
map.splice(first, length, ...Array.from({ length }).fill(".") as string[]); | ||
} | ||
} | ||
return map.reduce((acc: number, val, index) => { | ||
if (val === ".") { | ||
return acc; | ||
} | ||
return acc + (typeof val === 'number' ? val * index : 0); | ||
}, 0); | ||
} | ||
|
||
} |