BV for Advent of Code Problem

13 views
Skip to first unread message

David Silverman

unread,
Sep 16, 2025, 2:03:06 PMSep 16
to Picat
I thought bit vectors might be perfect for an Advent of Code problem, but couldn't get it to work with an #/\ condition between two bv_gt items.  



And copied below.

% import sat. % sub 4 seconds for sat
import cp. % 0.15 seconds for cp, ff, split
import util.

main =>
nolog,
% Data = read_file_lines("test"),
Data = read_file_lines("day"),
Rs = [[P[1], P[2]].map(to_int) : D in Data, P=split(D,"-")],
IP :: 0..max(Rs.flatten),
% IP = new_bv(32)
foreach (R in Rs)
% (bv_gt(R[1].int_to_bv,IP) #\/ bv_gt(IP,R[2].int_to_bv)) % doesn't work
(IP #< R[1] #\/ IP #> R[2])
end,
% Sols = solve_all([$seq],IP),
Sols = solve_all([ffs,split],IP),
printf("Answer Part 1: %w\n",min(Sols)),
printf("Answer Part 2: %w\n",len(Sols)).

Neng-Fa Zhou

unread,
Sep 16, 2025, 4:40:28 PMSep 16
to Picat
For this problem you don't need to use bv constraints, as the maximum integer allowed fits into a 32-bit word. But I agree with you that it'd be convenient to have implied and reified bv constraints for some applications. With these constraints, you can write something like:

reif_bv_gt(B1,X,Y), reif_bv(B2,U,V), B1+B2 #>= 1

Cheers,
NF
Reply all
Reply to author
Forward
0 new messages