advent of code 2023 day 3: too much fiddling with indiches!

This commit is contained in:
Wouter Groeneveld 2023-12-03 14:56:34 +01:00
parent a66110f9b9
commit 54c4dc3db6
5 changed files with 104 additions and 0 deletions

30
2023/03/assignment.md Normal file
View File

@ -0,0 +1,30 @@
# --- Day 3: Gear Ratios ---
You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source, but this is as far as he can bring you. You go inside.
It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.
"Aaah!"
You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help.
The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can add up all the part numbers in the engine schematic, it should be easy to work out which part is missing.
The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don't really understand, but apparently any number adjacent to a symbol, even diagonally, is a "part number" and should be included in your sum. (Periods (.) do not count as a symbol.)
Here is an example engine schematic:
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..
In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114 (top right) and 58 (middle right). Every other number is adjacent to a symbol and so is a part number; their sum is 4361.
Of course, the actual engine schematic is much larger. What is the sum of all of the part numbers in the engine schematic?

33
2023/03/impl.js Normal file
View File

@ -0,0 +1,33 @@
module.exports.solve = function(input) {
const len = input.indexOf('\n') === -1 ? input.length : input.indexOf('\n')
// This idea actually works against me: symbols on edges of the previous line are suddenly taken into account...
const schematic = input.split('\n').join('') // e.g. 467..114.. or 617*......
const isSymbolOn = (index) => {
// assumes it can't be a number since we regex-matched those.
return index < 0 || index >= schematic.length ? false : schematic[index] !== "."
}
const isSymbolInRange = (begin, end) => {
if(begin >= schematic.length || begin < 0 || end < 0) return false
if(end >= schematic.length) end = schematic.length - 1
return schematic.slice(begin, end).replaceAll(".", "").length > 0
}
return [...schematic.matchAll(/\d+/g)].filter(nr => {
const i = nr.index
const l = nr[0].length
const edge = i % len == 0
const edgeTrail = (i + l) % len == 0
const adjLeft = isSymbolOn(i - 1)
const adjRight = isSymbolOn(i + l)
const adjAbove = isSymbolInRange(edge ? i - len : i - 1 - len, edgeTrail ? i + l - len : i + 1 + l - len)
const adjBelow = isSymbolInRange(edge ? i + len + 1 : i - 1 + len, edgeTrail ? i + l + len : i + 1 + l + len)
return adjLeft || adjRight || adjAbove || adjBelow
})
.map(nr => parseInt(nr[0]))
.reduce((a, b) => a + b, 0)
}

10
2023/03/input.txt Normal file
View File

@ -0,0 +1,10 @@
467..114..
...*......
..35..633.
......#...
617*......
.....+.58.
..592.....
......755.
...$.*....
.664.598..

1
2023/03/output.txt Normal file
View File

@ -0,0 +1 @@
4361

30
2023/03/test.js Normal file
View File

@ -0,0 +1,30 @@
const test = require('node:test')
const assert = require('node:assert').strict;
const { readFileSync } = require('fs')
const { solve } = require('./impl.js')
test('take the sum of all numbers with a symbol on for a single line with one dot between', (t) => {
assert.equal(123, solve("123*.456"))
})
test('take the sum of all numbers with a symbol on for a single line without dot between', (t) => {
assert.equal(579, solve("123*456"))
})
test('take the orignal number if no others found', (t) => {
assert.equal(123, solve("123*"))
})
test('zero if no numbers surrounded by sumbol', (t) => {
assert.equal(0, solve("123"))
assert.equal(0, solve("123..456"))
assert.equal(0, solve("123..456\n........\n789..111"))
})
test('two of the same numbers', (t) => {
assert.equal(20, solve("10*10\n....."))
})
test('solve schematic of assignment', (t) => {
const input = readFileSync('input.txt').toString()
const expected_output = parseInt(readFileSync('output.txt').toString())
const out = solve(input)
assert.equal(out, expected_output)
})