aoc23/15/part1.pl
2023-12-15 00:17:21 -08:00

28 lines
684 B
Prolog

:- use_module(library(pio)).
:- use_module(library(clpfd)).
:- initialization(main, main).
main([FileName|_]) :-
input(FileName, Input),
hashes(Input, Hash),
writef('Answer=%t\n', [Hash]).
hashes(Strs, Hash) :-
concurrent_maplist(reverse, Strs, RStrs),
concurrent_maplist(hash, RStrs, Hashes),
sum_list(Hashes, Hash).
hash([], 0).
hash([C|Str], Hash) :- hash(Str, Prev), Hash is (Prev + C)*17 mod 256.
% Input stuff
input(FileName, Input) :- phrase_from_file(string(Input), FileName).
string([]) --> (eos; "\n"), !.
string([Item|Items]) --> item(Item), string(Items).
item([]) --> (","; "\n"; eos), !.
item([C|Chars]) --> [C], item(Chars).
eos([], []).