-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.pl
74 lines (55 loc) · 1.84 KB
/
day5.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
% Advent of Code 2020, Day 5
% (c) blu3r4y
:- initialization main, halt.
% read the file, map the lines to seat ids, and compute the result
main() :-
read_input('./data/day5.txt', Input),
maplist(seat_id, Input, SeatIds),
part_one(SeatIds, ResultPart1),
part_two(SeatIds, ResultPart2),
write(ResultPart1), nl,
write(ResultPart2).
% read the file into a list of strings
read_input(FilePath, Input) :-
read_file_to_string(FilePath, String, []),
split_string(String, '\n', '\n', Input).
% PART 1: maximum of seat ids
part_one(SeatIds, Result) :-
max_list(SeatIds, Result).
% PART 2: the missing spot in all those ids
part_two(SeatIds, Result) :-
sort(SeatIds, SortedIds),
free_spot(SortedIds, Result).
% get the first value of the list item that is non-consecutive
free_spot(List, FreeSpot) :-
length(List, Length),
between(1, Length, Index1),
Index2 is Index1 + 1,
nth1(Index1, List, Ele1),
nth1(Index2, List, Ele2),
Ele2 - Ele1 =\= 1,
FreeSpot is Ele1 + 1.
% compute the seat id by transcoding the row and col parts
seat_id(String, Result) :-
sub_string(String, 0, 7, _, RowStr),
sub_string(String, 7, 3, _, ColStr),
string_chars(RowStr, RowArr),
string_chars(ColStr, ColArr),
transcode(RowArr, RowResult),
transcode(ColArr, ColResult),
Result is RowResult * 8 + ColResult.
% transcode a list of characters to their numeric value
transcode(Code, Result) :-
length(Code, Length),
transcodeRec(Code, Length, 0, Result).
transcodeRec(_, 0, Result, Result) :- !.
transcodeRec(Code, Pos, Sum, Result) :-
[Head|CodeRest] = Code,
bit(Head, BitVal),
PosNext is Pos - 1,
SumNext is Sum + BitVal * (2 ^ PosNext),
transcodeRec(CodeRest, PosNext, SumNext, Result).
% only 'B' and 'R' represent a binary 1 in the code
bit(Char, Val) :- Char = 'B', Val = 1.
bit(Char, Val) :- Char = 'R', Val = 1.
bit(_, Val) :- Val = 0.