-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): add solutions for 2023 day 3
- Loading branch information
1 parent
48b9bd9
commit 4ff47b7
Showing
4 changed files
with
218 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Grid, cast, isNumber, multiply, solution, sum } from '../../utils'; | ||
|
||
const EMPTY = '.'; | ||
const GEAR = '*'; | ||
|
||
const parse = (input) => Grid.from(input, { cast }); | ||
|
||
const isGear = (point) => point.value === GEAR; | ||
const isSymbol = (point) => !isNumber(point.value) && point.value !== EMPTY; | ||
|
||
const process = (schema) => { | ||
const parts = []; | ||
let current = { part: 0, gears: new Set(), valid: false }; | ||
for (const point of schema.points) { | ||
const { neighbors, value } = point; | ||
|
||
if (!isNumber(value)) continue; | ||
|
||
current.part = current.part * 10 + value; | ||
|
||
for (const neighbor of neighbors) { | ||
if (isSymbol(neighbor)) { | ||
current.valid = true; | ||
} | ||
if (isGear(neighbor)) { | ||
current.gears.add(neighbor); | ||
} | ||
} | ||
|
||
if (!point.right || !isNumber(point.right.value)) { | ||
if (current.valid) { | ||
// Have reached the end of a valid part number | ||
parts.push(current.part); | ||
for (const gear of current.gears) { | ||
(gear.parts ||= []).push(current.part); | ||
} | ||
} | ||
current = { part: 0, gears: new Set(), valid: false }; | ||
} | ||
} | ||
|
||
const gears = schema.filter((point) => ( | ||
isGear(point) && point.parts.length === 2 | ||
)); | ||
|
||
return { parts, gears }; | ||
}; | ||
|
||
export const partOne = solution((input) => { | ||
const schema = parse(input); | ||
const { parts } = process(schema); | ||
return sum(parts); | ||
}); | ||
|
||
export const partTwo = solution((input) => { | ||
const schema = parse(input); | ||
const { gears } = process(schema); | ||
return sum(gears.map((gear) => multiply(gear.parts))); | ||
}); |
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,17 @@ | ||
part-one: | ||
- input: &example1 | | ||
467..114.. | ||
...*...... | ||
..35..633. | ||
......#... | ||
617*...... | ||
.....+.58. | ||
..592..... | ||
......755. | ||
...$.*.... | ||
.664.598.. | ||
answer: 4361 | ||
|
||
part-two: | ||
- input: *example1 | ||
answer: 467835 |
Oops, something went wrong.