From 54c4dc3db633a05f3fb817113f7d9bd14be8dd18 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Sun, 3 Dec 2023 14:56:34 +0100 Subject: [PATCH] advent of code 2023 day 3: too much fiddling with indiches! --- 2023/03/assignment.md | 30 ++++++++++++++++++++++++++++++ 2023/03/impl.js | 33 +++++++++++++++++++++++++++++++++ 2023/03/input.txt | 10 ++++++++++ 2023/03/output.txt | 1 + 2023/03/test.js | 30 ++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+) create mode 100644 2023/03/assignment.md create mode 100644 2023/03/impl.js create mode 100644 2023/03/input.txt create mode 100644 2023/03/output.txt create mode 100644 2023/03/test.js diff --git a/2023/03/assignment.md b/2023/03/assignment.md new file mode 100644 index 0000000..d2b7273 --- /dev/null +++ b/2023/03/assignment.md @@ -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? diff --git a/2023/03/impl.js b/2023/03/impl.js new file mode 100644 index 0000000..a101b18 --- /dev/null +++ b/2023/03/impl.js @@ -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) + +} diff --git a/2023/03/input.txt b/2023/03/input.txt new file mode 100644 index 0000000..624ea4f --- /dev/null +++ b/2023/03/input.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. \ No newline at end of file diff --git a/2023/03/output.txt b/2023/03/output.txt new file mode 100644 index 0000000..95e0fa0 --- /dev/null +++ b/2023/03/output.txt @@ -0,0 +1 @@ +4361 \ No newline at end of file diff --git a/2023/03/test.js b/2023/03/test.js new file mode 100644 index 0000000..0f4de18 --- /dev/null +++ b/2023/03/test.js @@ -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) +})