aoc2022/03.yab
2022-12-05 03:06:22 -06:00

64 lines
1.1 KiB
Text

dim yabasic 2.90.2, built on x86_64-unknown-linux-gnu
sub getPriority(ch$)
local a
a = asc(ch$)
if and(a>=asc("a"), a<=asc("z")) return a-asc("a")+1
if and(a >= asc("A"), a<=asc("Z")) return a-asc("A")+27
end sub
open "03.txt" for reading as #1
sum = 0
while not eof(1)
line input #1 line$
lineLen = len(line$)
halfLen = lineLen/2
firstHalf$ = mid$(line$, 1, halfLen)
secondHalf$ = mid$(line$, halfLen + 1)
for i=1 to halfLen
for j=1 to halfLen
if mid$(firstHalf$, i, 1) == mid$(secondHalf$, j, 1) then
dup$ = mid$(firstHalf$, i, 1)
break 2
endif
next j
next i
sum = sum + getPriority(dup$)
wend
close #1
print sum
open "03.txt" for reading as #1
sum = 0
while not eof(1)
line input #1 line1$
line input #1 line2$
line input #1 line3$
for i=1 to len(line1$)
c1$ = mid$(line1$, i, 1)
for j=1 to len(line2$)
c2$ = mid$(line2$, j, 1)
if c1$ != c2$ continue
for k=1 to len(line3$)
c3$ = mid$(line3$, k, 1)
if c2$ != c3$ continue
badge$ = c3$
next k
next j
next i
sum = sum + getPriority(badge$)
wend
close #1
print sum