Skip to content

Commit

Permalink
Year 2023, Day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlauer-Hax committed Dec 3, 2023
1 parent bbed5ee commit f213ebb
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
30 changes: 30 additions & 0 deletions clients/typescript/solutions/S2303.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import ISolution from "./ISolution.ts";

export default class S2303 implements ISolution {
firstPart(input: string): string | number {
return input.split('\n')
.map((line, index, lines) =>
[...line.matchAll(/[0-9]+/g)].filter((val) =>
[lines[index - 1], line, lines[index + 1]].some(s =>
s?.substring(val.index! - 1, val.index! + val[0].length + 1)
.match(/[^0-9.]/g))
).map(s => Number(s[0])))
.flat()
.reduce((a, b) => a + b, 0);
}
secondPart(input: string): string | number {
return input.split('\n')
.map((line, index, lines) =>
[...line.matchAll(/\*/g)].map((val) =>
[lines[index - 1], line, lines[index + 1]]
.map(s => [...s.matchAll(/[0-9]+/g)]
.filter((match) => !(match.index! > val.index! + 1 || match.index! + match[0].length < val.index!))
.map(s => Number(s[0])))
.flat()
.map((item, _, arr) => arr.length === 2 ? item : 0)
.reduce((a, b) => a * b)
))
.flat()
.reduce((a, b) => a + b, 0);
}
}
Loading

0 comments on commit f213ebb

Please sign in to comment.