Thursday | 21 NOV 2024
[ previous ]
[ next ]

Advent of Code 2023 - Day 1

Title:
Date: 2023-12-01

A fun little puzzle but part 2 was troublesome because the test case worked but the real data didn't. I got the solution with a hint very quickly but it was annoying to have to rely on the hint to get through it.

I'm aiming to get past 7 days this time around. I'm also planning to go to reddit and reading the hints if it takes me longer than an hour.

I'm doing AOC with BASIC this year and I think that's going to be quite fun.

I would love to be able to use the multivalue aware functions like SUMS and OCONVS. I got the chance to use OCONVS to mask out the alphabet and get just the numbers.

   NUMBER.OF.LINES = DCOUNT(RAW.DATA,@AM)
*
   PUZZLE = OCONVS(RAW.DATA,'MCN')
*
   VALUES = ''
   FOR I = 1 TO NUMBER.OF.LINES
      LINE = PUZZLE<I>
      C = LINE[1,1] : LINE[1]
      VALUES<-1> = C
   NEXT I
*
   PRINT 'Answer 1: ' : SUM(VALUES)

The core idea is to get the numeric values of a line and then the first and last digit form a number. We add all the 2 digit numbers and that is the answer.

I would have liked to skip the FOR loop and used CATS and SUBSTRINGS but I couldn't figure out how to get the last character from each multivalue field. Getting the first was easy and concatenation on each field is also easy. I wonder if there is a way to do it BASIC or in Pick.

There was some clever solutions I saw. I really liked the idea of replacing the numbers like one with o1e. This way overlapping numbers would be picked up properly. I made it dumb and simply searched for each number at each character. Slow but it worked.

   PUZZLE = RAW.DATA
*
   VALUES = ''
   FOR I = 1 TO NUMBER.OF.LINES
      LINE = TRIM(PUZZLE<I>)
      NEW.LINE = ''
*
      FOR K = 1 TO LEN(LINE)
         CHR = LINE[K,1]
*
         IF NUM(CHR) THEN
            NEW.LINE := CHR
            CONTINUE
         END
*
         BEGIN CASE
            CASE LINE[K,3] = 'one'
               NEW.LINE := 1
*
            CASE LINE[K,3] = 'two'
               NEW.LINE := 2
*
            CASE LINE[K,5] = 'three'
               NEW.LINE := 3
*
            CASE LINE[K,4] = 'four'
               NEW.LINE := 4
*
            CASE LINE[K,4] = 'five'
               NEW.LINE := 5
*
            CASE LINE[K,3] = 'six'
               NEW.LINE := 6
*
            CASE LINE[K,5] = 'seven'
               NEW.LINE := 7
*
            CASE LINE[K,5] = 'eight'
               NEW.LINE := 8
*
            CASE LINE[K,4] = 'nine'
               NEW.LINE := 9
         END CASE
      NEXT K
*
      C = NEW.LINE[1,1] : NEW.LINE[1]
      PRINT LINE, NEW.LINE, C
      VALUES<-1> = C
   NEXT I
*
   PRINT 'Answer 2: ' : SUM(VALUES)