Skip to content

Commit

Permalink
day 3 initial solution
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 3, 2023
1 parent 5b1a7e0 commit ca947a3
Show file tree
Hide file tree
Showing 3 changed files with 297 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/2023/day03.js
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);
}
56 changes: 56 additions & 0 deletions src/2023/day03.spec.js
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);
});
});
});
Loading

0 comments on commit ca947a3

Please sign in to comment.