AnnouncementsMatrixEventsFunnyVideosMusicAncapsTechnologyEconomicsPrivacyGIFSCringeAnarchyFilmPicsThemesIdeas4MatrixAskMatrixHelpTop Subs
3

I'm still doing Advent behind schedule. Join me if you want a fun coding challenge.
Official link: https://adventofcode.com/2025/day/2
Day 1: https://goatmatrix.net/c/AdventOfCode/7ZJB6GR7kL
You are welcome to start a thread if you want to do Advent of Code on schedule.

--- Day 2: Gift Shop ---
You get inside and take the elevator to its only other stop: the gift shop. "Thank you for visiting the North Pole!" gleefully exclaims a nearby sign. You aren't sure who is even allowed to visit the North Pole, but you know you can access the lobby through here, and from there you can access the rest of the North Pole base.

As you make your way through the surprisingly extensive selection, one of the clerks recognizes you and asks for your help.

As it turns out, one of the younger Elves was playing on a gift shop computer and managed to add a whole bunch of invalid product IDs to their gift shop database! Surely, it would be no trouble for you to identify the invalid product IDs for them, right?

They've even checked most of the product ID ranges already; they only have a few product ID ranges (your puzzle input) that you'll need to check. For example:

11-22,95-115,998-1012,1188511880-1188511890,222220-222224,
1698522-1698528,446443-446449,38593856-38593862,565653-565659,
824824821-824824827,2121212118-2121212124

(The ID ranges are wrapped here for legibility; in your input, they appear on a single long line.)

The ranges are separated by commas (,); each range gives its first ID and last ID separated by a dash (-).

Since the young Elf was just doing silly patterns, you can find the invalid IDs by looking for any ID which is made only of some sequence of digits repeated twice. So, 55 (5 twice), 6464 (64 twice), and 123123 (123 twice) would all be invalid IDs.

None of the numbers have leading zeroes; 0101 isn't an ID at all. (101 is a valid ID that you would ignore.)

Your job is to find all of the invalid IDs that appear in the given ranges. In the above example:

  • 11-22 has two invalid IDs, 11 and 22.
  • 95-115 has one invalid ID, 99.
  • 998-1012 has one invalid ID, 1010.
  • 1188511880-1188511890 has one invalid ID, 1188511885.
  • 222220-222224 has one invalid ID, 222222.
  • 1698522-1698528 contains no invalid IDs.
  • 446443-446449 has one invalid ID, 446446.
  • 38593856-38593862 has one invalid ID, 38593859.
  • The rest of the ranges contain no invalid IDs.

Adding up all the invalid IDs in this example produces 1227775554.

What do you get if you add up all of the invalid IDs?

Full puzzle input:

197-407,262128-339499,557930-573266,25-57,92856246-93001520,2-12,1919108745-1919268183,48414903-48538379,38342224-38444598,483824-534754,1056-1771,4603696-4688732,75712519-75792205,20124-44038,714164-782292,4429019-4570680,9648251-9913729,6812551522-6812585188,58-134,881574-897488,648613-673853,5261723647-5261785283,60035-128980,9944818-10047126,857821365-857927915,206885-246173,1922-9652,424942-446151,408-1000

Comment preview

[-]x0x70(0|0)

I'm slow rolling Advent of code this year because I do have other things I'm working on. So I'm just doing one part a day-ish. Day 2 part 1 was interesting. This is another one where I chose to do a lot of map filter reduce (MFR). Most of the code is manipulating the input into an equivalent input with nice properties. I view these challenges as an excuse to write fun code instead of clean code.

#!/usr/bin/env node

//Splits [2-12]->[2-9,10-12], or [44-1024]->[44-99,100-999,1000-1024]
module.exports.splitByDigitLen=splitByDigitLen;
function splitByDigitLen(start, end) {
 var out = [];
 out.push({start,end:'9'.repeat(start.length)});
 //For loop could trigger if we jump two orders of magintude
 for (var length = start.length+1; length < end.length; ++length) {
  out.push({
   start:'1'+'0'.repeat(length-1),
   end:'9'.repeat(length)
  });
 }
 out.push({end,start:'1'+'0'.repeat(end.length-1)});
 return out;
}

module.exports.readData=readData;
function readData(givenpath) {
 var fs = require('fs');
 var path = givenpath || process.argv[2] || 'input2.txt';
 var data = fs.readFileSync(path,'utf8');
    // Get rid of newline at end and split into ranges
    data = data.trim().split(',');  
    // Split into the range parts
    data = data.map(i=>i.split('-')); 
    // Convert the range parts into an object representation
    data = data.map(i=>({start:i[0],end:i[1]}));
    // Make all starts and ends the same length by splitting some into sub-ranges
    data = data.flatMap(i=>(i.start.length==i.end.length)?i:splitByDigitLen(i.start,i.end)); 
    // Get rid of odd length numbers because they can't create a repeat pattern
    data = data.filter(i=>i.start.length%2==0); 
    // Enrich the object with more properties to access conveniently
    data.forEach(i=>{
     i.halfStr=i.start.slice(0,i.start.length/2); //First half of start
     i.halfInt=parseInt(i.halfStr);
     i.startStr=i.start;
     i.startInt=parseInt(i.start);
     i.endStr=i.end;
     i.endInt=parseInt(i.end);
    });
 return data;
}

module.exports.rangeToInvalidIds=rangeToInvalidIds;
function rangeToInvalidIds(i) {
 //Add concatenate the first half of start with itself, test if in range, increment it, do it again till out of range.
 var out=[];
 for(var j=i.halfInt;;++j) {
  //console.log({i,j});
  var test = parseInt(''+j+j);
  if(test<i.startInt) continue;
  if(test>i.endInt) break;
  out.push(test);
 }
 return out;
}

module.exports.main=main;
function main() {
 //Read, map to invalid ids, sum
 console.log(readData().flatMap(rangeToInvalidIds).reduce((a,b)=>a+b));
}
if(!module.parent) main();