Day 5: Cafeteria
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465


Javascript
Short and sweet. Basically just handle all the range overlaps for part 2 and then do basic subtraction to get the answer.
spoiler
const input = require('fs').readFileSync('input-day5.txt', 'utf-8'); /** @typedef {[number, number]} Range */ /** @type {[Range[], number[]]} */ const [freshRanges, availableIngredients] = input.split("\n\n").map(l => l.split("\n")).map((l, i) => (i === 1) ? l.map(v => parseInt(v, 10)) : l.map(v => v.split('-').map(vv => parseInt(vv, 10)))); let freshOnHand = 0; for (const ingredient of availableIngredients) { for (const [start, stop] of freshRanges) { if (ingredient >= start && ingredient <= stop) { freshOnHand += 1; break; } } } console.log(`Part 1 Answer: ${freshOnHand}`); const sortedRanges = [...freshRanges].sort((a, b) => a[0] - b[0]); const mergedRanges = []; let current = sortedRanges[0]; for (let i = 1; i < sortedRanges.length; i++) { const [nextStart, nextEnd] = sortedRanges[i]; if (nextStart <= current[1] + 1) { current = [current[0], Math.max(current[1], nextEnd)]; } else { mergedRanges.push(current); current = [nextStart, nextEnd]; } } mergedRanges.push(current); const freshIngredientCount = mergedRanges.reduce((acc, [start, stop]) => acc + ((stop + 1) - start), 0) console.log(`Part 2 Answer: ${freshIngredientCount}`);