-
Notifications
You must be signed in to change notification settings - Fork 8
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
3 changed files
with
297 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,100 @@ | ||
function signed(map, i, j) { | ||
const neighbors = [ | ||
map[i - 1]?.[j - 1], | ||
map[i - 1]?.[j], | ||
map[i - 1]?.[j + 1], | ||
map[i]?.[j - 1], | ||
map[i]?.[j + 1], | ||
map[i + 1]?.[j - 1], | ||
map[i + 1]?.[j], | ||
map[i + 1]?.[j + 1], | ||
]; | ||
return neighbors.some(n => n !== '.' && (n < '0' || n > '9')); | ||
} | ||
|
||
function gear(map, i, j) { | ||
const neighbors = [ | ||
{ x: i - 1, y: j - 1, c: map[i - 1]?.[j - 1] }, | ||
{ x: i - 1, y: j, c: map[i - 1]?.[j] }, | ||
{ x: i - 1, y: j + 1, c: map[i - 1]?.[j + 1] }, | ||
{ x: i, y: j - 1, c: map[i]?.[j - 1] }, | ||
{ x: i, y: j + 1, c: map[i]?.[j + 1] }, | ||
{ x: i + 1, y: j - 1, c: map[i + 1]?.[j - 1] }, | ||
{ x: i + 1, y: j, c: map[i + 1]?.[j] }, | ||
{ x: i + 1, y: j + 1, c: map[i + 1]?.[j + 1] }, | ||
]; | ||
const gears = neighbors.filter(n => n.c === '*'); | ||
if (gears.length > 1) { | ||
console.log(gears); //? | ||
} | ||
return gears.length === 0 ? undefined : `${gears[0].x},${gears[0].y}`; | ||
} | ||
|
||
export function part1(input) { | ||
const map = input.split('\n').map(line => line.split('')); | ||
let current = ''; | ||
let sign = false; | ||
let sum = 0; | ||
for (let i = 0; i < map.length; i++) { | ||
const line = map[i]; | ||
for (let j = 0; j < line.length; j++) { | ||
const char = line[j]; | ||
if (char >= '0' && char <= '9') { | ||
current += char; | ||
if (signed(map, i, j)) { | ||
sign = true; | ||
} | ||
} else { | ||
if (current) { | ||
if (sign) sum += +current; | ||
current = ''; | ||
sign = false; | ||
} | ||
} | ||
} | ||
if (current) { | ||
if (sign) sum += +current; | ||
current = ''; | ||
sign = false; | ||
} | ||
} | ||
return sum; | ||
} | ||
|
||
export function part2(input) { | ||
const map = input.split('\n').map(line => line.split('')); | ||
let current = ''; | ||
let pos = undefined; | ||
let gears = {}; | ||
for (let i = 0; i < map.length; i++) { | ||
const line = map[i]; | ||
for (let j = 0; j < line.length; j++) { | ||
const char = line[j]; | ||
if (char >= '0' && char <= '9') { | ||
current += char; | ||
pos = pos || gear(map, i, j); | ||
} else { | ||
if (current) { | ||
if (pos) | ||
gears[pos] = gears[pos] ? gears[pos].concat(current) : [+current]; | ||
current = ''; | ||
pos = undefined; | ||
} | ||
} | ||
} | ||
if (current) { | ||
if (pos) | ||
gears[pos] = gears[pos] ? gears[pos].concat(current) : [+current]; | ||
current = ''; | ||
pos = undefined; | ||
} | ||
} | ||
gears = Object.values(gears).map(gear => { | ||
if (gear.length > 1) { | ||
return gear.reduce((a, b) => a * b, 1); | ||
} else { | ||
return 0; | ||
} | ||
}); | ||
return Object.values(gears).reduce((a, b) => a + b, 0); | ||
} |
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,56 @@ | ||
import { part1, part2 } from './day03.js'; | ||
import readInput from '../utils/read-input.js'; | ||
|
||
const input = readInput(import.meta.url); | ||
|
||
describe('day03 2023', () => { | ||
describe('part1', () => { | ||
it('should work for part 1 examples', () => { | ||
expect( | ||
part1( | ||
[ | ||
'467..114..', | ||
'...*......', | ||
'..35..633.', | ||
'......#...', | ||
'617*......', | ||
'.....+.58.', | ||
'..592.....', | ||
'......755.', | ||
'...$.*....', | ||
'.664.598..', | ||
].join('\n'), | ||
), | ||
).toEqual(4361); | ||
}); | ||
|
||
it('should work for part 1 input', () => { | ||
expect(part1(input)).toEqual(528799); | ||
}); | ||
}); | ||
|
||
describe('part2', () => { | ||
it('should work for part 2 examples', () => { | ||
expect( | ||
part2( | ||
[ | ||
'467..114..', | ||
'...*......', | ||
'..35..633.', | ||
'......#...', | ||
'617*......', | ||
'.....+.58.', | ||
'..592.....', | ||
'......755.', | ||
'...$.*....', | ||
'.664.598..', | ||
].join('\n'), | ||
), | ||
).toEqual(467835); | ||
}); | ||
|
||
it('should work for part 2 input', () => { | ||
expect(part2(input)).toEqual(84907174); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.