aoc23/15/part1.pl

27 lines
653 B
Perl
Raw Permalink Normal View History

2023-12-15 02:17:21 -06:00
:- use_module(library(pio)).
:- 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([], []).