45 lines
1.3 KiB
Perl
45 lines
1.3 KiB
Perl
|
:- use_module(library(pio)).
|
||
|
:- initialization(main, main).
|
||
|
|
||
|
main([FileName|_]) :-
|
||
|
input(FileName, Blocks),
|
||
|
convlist(vert_reflect, Blocks, Vs),
|
||
|
convlist(horz_reflect, Blocks, Hs),
|
||
|
sum_list(Vs, V),
|
||
|
sum_list(Hs, H),
|
||
|
Answer is 100*V + H,
|
||
|
writef('Answer=%t\n', [Answer]).
|
||
|
|
||
|
vert_reflect(Block, N) :- reflect(Block, N).
|
||
|
horz_reflect(Block, N) :- maplist({N}/[Row]>>(reflect(Row, N)), Block).
|
||
|
|
||
|
reflect(Items, N) :- perfect_reflect(Items, N).
|
||
|
reflect(Items, N) :-
|
||
|
append(SubItems, [_|_], Items),
|
||
|
perfect_reflect(SubItems, N).
|
||
|
reflect(Items, N) :-
|
||
|
append(Prefix, SubItems, Items),
|
||
|
perfect_reflect(SubItems, SubN),
|
||
|
length(Prefix, Len), N is SubN + Len.
|
||
|
|
||
|
% reflect right down the middle. N is half list length.
|
||
|
perfect_reflect([A, A], 1).
|
||
|
perfect_reflect(Items, N) :-
|
||
|
append([[Item], OtherItems, [Item]], Items),
|
||
|
perfect_reflect(OtherItems, N_sub1),
|
||
|
N is N_sub1 + 1.
|
||
|
|
||
|
input(FileName, Blocks) :- phrase_from_file(blocks(Blocks), FileName).
|
||
|
|
||
|
blocks([]) --> eos, !.
|
||
|
blocks([Block|Blocks]) --> block(Block), blocks(Blocks).
|
||
|
|
||
|
block([Line]) --> line(Line), ("\n"; eos), !.
|
||
|
block([Line|Lines]) --> line(Line), block(Lines).
|
||
|
|
||
|
line([]) --> ("\n"; eos), !.
|
||
|
line([#|Chars]) --> "#", line(Chars).
|
||
|
line([o|Chars]) --> ".", line(Chars).
|
||
|
|
||
|
eos([], []).
|