lyxell.dev

Advent of Code 2024 in SQLite: Day 3

On Day 3 the task was to extract strings such as mul(1,3) from the problem input and use the integers inside the extracted strings to compute the result. This problem was nice since it could be solved mostly by regular expressions.

The solution for part one:

create table T (row text);
.import 03_input.txt T
.load ./regex0.so
select
	sum(regex_capture(captures, 1) * regex_capture(captures, 2))
from
	regex_captures(
		'mul\(([0-9]{1,3}),([0-9]{1,3})\)',
		T.row
	)
join T;

The solution for part two:

create table T (row text);
.import 03_input.txt T
.load ./regex0.so
select
	sum(regex_capture(captures, 1) * regex_capture(captures, 2))
from
	regex_captures(
		'mul\(([0-9]{1,3}),([0-9]{1,3})\)',
		regex_replace_all("don't\(\)(.*?)(do\(\)|$)", prog, "")
	)
join (
	-- concatenate the file into a single line
	select group_concat(T.row, '') as prog from T
);

The runnable scripts can be found on GitHub.