-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5-2.pl
32 lines (23 loc) · 867 Bytes
/
day5-2.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
:- use_module(library(dcg/basics)).
parse([]) --> eos.
parse([H|T]) --> string(Hc), { string_codes(Hs, Hc), string_lower(Hs, Hl), string_chars(Hl, H) },blank, parse(T).
row([l|T], [l|T], _, 0).
row([r|T], [r|T], _, 0).
row([f|T], Rest, N, Row):- N2 is div(N,2), row(T, Rest, N2, Row).
row([b|T], Rest, N, Row):- N2 is div(N,2), row(T, Rest, N2, C2), Row is C2+N2.
col([], _, 0).
col([l|T], N, Col):- N2 is div(N,2), col(T, N2, Col).
col([r|T], N, Col):- N2 is div(N,2), col(T, N2, C2), Col is C2+N2.
code(Seat, Code):- row(Seat, Rest, 128, Row),
col(Rest, 8, Col),
Code is Row*8+Col.
:- phrase_from_file(parse(Seats), 'input-5.txt'),
maplist(code, Seats, Codes),
max_list(Codes, Max),
between(0, Max, S),
\+member(S, Codes),
succ(S, After),
succ(Before, S),
member(After, Codes),
member(Before, Codes),
write(S), nl.