LLVM IR: Advent of Code (2025) Day 2

Published At: 11/11/2026, 9:31:38 AM ⋅ 56 views

Day 2 was a lot more challenging than Day 1. Well, part 2 was. Day 1's anti-double-counting logic was a lot simpler than Day 2's part 2. Let's not get too ahead of ourselves though, here's my solution to part 1 (this time without obfuscated SSAs):

target triple = "arm64-apple-macosx26.0.0"

@filename = private constant [15 x i8] c"day2/input.txt\00"
@mode = private constant [2 x i8] c"r\00"
@message = private constant [12 x i8] c"Result: %ld\00"

declare ptr @fopen(ptr, ptr)
declare i64 @fread(ptr, i64, i64, ptr)
declare void @fseek(ptr, i64, i32)
declare i64 @atol(ptr)
declare void @printf(ptr, ...)

define noundef i32 @digitCount(i64 %in) {
entry:
  br label %loop

loop:
  %count = phi i32 [ 1, %entry ], [ %next_count, %body ]
  %value = phi i64 [ %in, %entry ], [ %next_value, %body ]

  %incomplete = icmp sge i64 %value, 10
  br i1 %incomplete, label %body, label %end

body:
  %next_value = sdiv i64 %value, 10
  %next_count = add i32 %count, 1
  br label %loop

end:
  ret i32 %count
}

define noundef i1 @fetch(ptr %fp, ptr %start, ptr %end, ptr %offset) {
entry:
  %actualF = load ptr, ptr %fp
  %noFp = icmp eq ptr %actualF, null
  br i1 %noFp, label %initFp, label %fetchRange

initFp:
  %fpRes = tail call ptr @fopen(ptr @filename, ptr @mode)
  store ptr %fpRes, ptr %fp
  br label %fetchRange

fetchRange:
  %file = load ptr, ptr %fp
  %initialOffset = load i32, ptr %offset
  %initialOffsetExt = sext i32 %initialOffset to i64
  call void @fseek(ptr %file, i64 %initialOffsetExt, i32 0)
  %start_buffer = alloca [100 x i8]
  %start_bufferPtr = getelementptr [100 x i8], ptr %start_buffer, i64 0, i64 0
  %bytesRead = tail call i64 @fread(ptr %start_bufferPtr, i64 1, i64 100, ptr %file)
  %readFailed = icmp eq i64 %bytesRead, 0
  br i1 %readFailed, label %complete, label %continue

continue:
  %startNum = tail call i64 @atol(ptr %start_bufferPtr)
  %startCount = tail call i32 @digitCount(i64 %startNum)
  %newOffset = add i32 %startCount, 1
  %newOffsetLong = sext i32 %newOffset to i64
  %newOffsetAdj = add i64 %initialOffsetExt, %newOffsetLong
  tail call void @fseek(ptr %file, i64 %newOffsetAdj, i32 0)
  %end_buffer = alloca [100 x i8]
  %end_bufferPtr = getelementptr [100 x i8], ptr %end_buffer, i64 0, i64 0
  %end_bytesRead = tail call i64 @fread(ptr %end_bufferPtr, i64 1, i64 100, ptr %file)
  %endNum = tail call i64 @atol(ptr %end_bufferPtr)
  %endCount = tail call i32 @digitCount(i64 %endNum)
  %end_newOffset = add i32 %endCount, 1
  %end_newOffsetLong = sext i32 %end_newOffset to i64
  %totalOffset1 = add i32 %initialOffset, %newOffset
  %totalOffset2 = add i32 %totalOffset1, %end_newOffset
  store i32 %totalOffset2, ptr %offset
  store i64 %startNum, ptr %start
  store i64 %endNum, ptr %end
  br label %complete

complete:
  %res = phi i1 [ true, %fetchRange ], [ false, %continue ]
  ret i1 %res
}

define i32 @ipow(i32 %base, i32 %exp) {
  %valPtr = alloca i32
  store i32 %base, ptr %valPtr
  %iPtr = alloca i32
  store i32 1, ptr %iPtr
  br label %loopCheck

loopCheck:
  %i = load i32, ptr %iPtr
  %isComplete = icmp sge i32 %i, %exp
  br i1 %isComplete, label %loopEnd, label %loopBody

loopBody:
  %val = load i32, ptr %valPtr
  %newVal = mul i32 %val, %base
  store i32 %newVal, ptr %valPtr
  %newI = add i32 %i, 1
  store i32 %newI, ptr %iPtr
  br label %loopCheck

loopEnd:
  %fval = load i32, ptr %valPtr
  ret i32 %fval
}

define noundef i64 @part1() {
entry:
  %fpp = alloca ptr
  store ptr null, ptr %fpp
  %startPtr = alloca i64
  %endPtr = alloca i64
  %offsetPtr = alloca i32
  store i32 0, ptr %offsetPtr
  br label %loop

loop:
  %outerTotal = phi i64 [ 0, %entry ], [ %innerTotal, %loopCheck ]
  %res = call i1 @fetch(ptr %fpp, ptr %startPtr, ptr %endPtr, ptr %offsetPtr)
  %start = load i64, ptr %startPtr
  %end = load i64, ptr %endPtr
  br i1 %res, label %complete, label %loopCheck

loopCheck:
  %i = phi i64 [ %start, %loop ], [ %incI, %loopEnd ]
  %innerTotal = phi i64 [ %outerTotal, %loop ], [ %totalAtEnd, %loopEnd ]
  %isEnd = icmp sgt i64 %i, %end
  br i1 %isEnd, label %loop, label %loopBody

loopBody:
  %dCount = call i32 @digitCount(i64 %i)
  %drem = srem i32 %dCount, 2
  %isOdd = icmp eq i32 %drem, 1
  br i1 %isOdd, label %loopEnd, label %loopOp

loopOp:
  %size = sdiv i32 %dCount, 2
  %divisor = call i32 @ipow(i32 10, i32 %size)
  %divisorExt = sext i32 %divisor to i64
  %quo = sdiv i64 %i, %divisorExt
  %rem = srem i64 %i, %divisorExt
  %satisfied = icmp eq i64 %quo, %rem
  br i1 %satisfied, label %incTotal, label %loopEnd

incTotal:
  %nextTotal = add i64 %innerTotal, %i
  br label %loopEnd

loopEnd:
  %totalAtEnd = phi i64 [ %innerTotal, %loopOp ], [ %innerTotal, %loopBody ], [ %nextTotal, %incTotal ]
  %incI = add i64 %i, 1
  br label %loopCheck

complete:
  ret i64 %outerTotal
}

define void @main() {
  %res = call i64 @part1()
  call void (ptr, ...) @printf(ptr @message, i64 %res)
  ret void
}

The logic is pretty simple overall, you should be able to understand it just by skimming the code. It's just performing the following pseudocode:

function isValid(i: int): bool
    dcount := digitCount(i)
    if dcount % 2 == 1
        return TRUE

    topHalf := i / (10 ** (dcount / 2))
    bottomHalf := i % (10 ** (dcount / 2))
    return topHalf != bottomHalf

For all i in the ranges, and sums the i for which the function returns FALSE. Now we get to part 2. Honestly, I couldn't figure out how to do part 2, at least not in LLVM. The regex solution was beyond obvious, but there was no way I was going to implement a regex engine in LLVM IR just for this. As a compromise here's the solution in P*thon:

import re

def is_valid(i: int) -> bool:
    return not re.match(r'(\d+)\1+$', str(i))

with open('day2/input.txt') as f:
    ranges = map(lambda r: range(*map(int, r.split('-'))), f.read().split(','))
    total = 0
    for r in ranges:
        for i in r:
            if not is_valid(i):
                total += i

    print(total)

It's horribly slow, but it works. Do we really need efficient solutions for this? I mean, the goal is just to get the answer. I don't need to run this script a billion times after getting the answer. How does it matter if the script takes...

$ time python day2/day2.py
******* (answer here :P)
python day2/day2.py  0.55s user 0.02s system 83% cpu 0.676 total

Yikes.

P017R01