d15
This commit is contained in:
27
15/part1.pl
Normal file
27
15/part1.pl
Normal file
@@ -0,0 +1,27 @@
|
||||
:- 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([], []).
|
Reference in New Issue
Block a user