Word Search for Advent of Code 2024 - Day 4

Date

Day 4 of Advent of Code had us do a word search for “XMAS”. My solution was to just look for the letter “X” and then spin around that point looking for the string. I though the spinning would look interesting, so I put together a simple 2D visualization.

Word Search

Lessons Learned

Rendering a grid of text is slow. I added a new offscreenImage function to my visualization library that lets you generate a CGImage using the same drawing tools you use for the main visualization.

let image = offscreenImage { offscreenContext in
    let rect = CGRect(origin: .zero, size: CGSize(width: 20, height: 20))
    let color = CGColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
    fill(rect: rect, color: red, in: offscreenContext)
}

context.draw(image, in: screenRect)

Since rendering the found words would also be slow, I ended up recreating the text grid any time new highlights were found, allowing the main rendering loop to only render cursors and search vectors.