Skip to content

Instantly share code, notes, and snippets.

@fastjack
Last active December 3, 2023 13:12
Show Gist options
  • Save fastjack/0d94472c4751cca2f45bc34dd33ea277 to your computer and use it in GitHub Desktop.
Save fastjack/0d94472c4751cca2f45bc34dd33ea277 to your computer and use it in GitHub Desktop.
Advent of Code 2023, Day 1, Part 2
100 DIM S$(9), D$(9)
110 S$(1)="ONE":S$(2)="TWO":S$(3)="THREE":S$(4)="FOUR":S$(5)="FIVE":S$(6)="SIX":S$(7)="SEVEN":S$(8)="EIGHT":S$(9)="NINE"
120 D$(1)="O1E":D$(2)="T2O":D$(3)="T333E":D$(4)="F44R":D$(5)="F55E":D$(6)="S6X":D$(7)="S777N":D$(8)="E888T":D$(9)="N99E"
130 OPEN 1, 8, 3, "INPUT.TXT,S,R"
140 S = 0
150 INPUT#1, A$
160 GOSUB 230
170 S = S + C
180 IF ST = 0 GOTO 150
190 CLOSE 1
200 PRINT S
210 END
220 REM FIND FIRST AND LAST DIGIT IN STRING
230 L = LEN(A$)
240 GOSUB 360
250 FOR I = 1 TO L
260 C1 = ASC(MID$(A$, I, 1))
270 IF C1 >= 48 AND C1 <= 57 THEN I = L : REM EXIT FOR LOOP EARLY
280 NEXT I
290 FOR I = L TO 1 STEP -1
300 C0 = ASC(MID$(A$, I, 1))
310 IF C0 >= 48 AND C0 <= 57 THEN I = 1 : REM EXIT FOR LOOP EARLY
320 NEXT I
330 C = (C1 - 48) * 10 + C0 - 48
340 RETURN
350 REM REPLACE NUMBERS AS WORDS (S$()) WITH NUMBERS (D$())
360 FOR J = 1 TO L
370 FOR N = 1 TO 9
380 SL = LEN(S$(N))
390 IF J + SL > L + 1 GOTO 410
400 IF MID$(A$, J, SL) = S$(N) THEN B$ = LEFT$(A$, J-1) + D$(N) + RIGHT$(A$, L - J - SL + 1): A$ = B$: N = 9: J = J + SL - 2
410 NEXT N
420 NEXT J
430 RETURN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment