# 2020 Advent of Code Day 1

Prolog. Developed and tested with SWI-Prolog 8.0.3.

```check_and_read(10, [] ,_):-
!.
!.
!.
!.
get_code(Stream, NextChar),

at_end_of_stream(Stream).
\+ at_end_of_stream(Stream),
get_code(Stream, Char),
number_codes(X, Chars),

pair_sum_to_2020(Numbers, Pair):-
pair_sum_to_2020(Numbers, Numbers, Numbers, Pair).
pair_sum_to_2020([], _, _, _).
pair_sum_to_2020([], [], _, _).
pair_sum_to_2020([_|TI], [], Numbers, Pair):-
pair_sum_to_2020(TI, Numbers, Numbers, Pair).
pair_sum_to_2020([HI|_], [HJ|_], Numbers, Pair):-
Sum is HI + HJ,
Sum == 2020,
Pair = [HI, HJ],
pair_sum_to_2020([], [], Numbers, Pair).
pair_sum_to_2020([HI|TI], [_|TJ], Numbers, Pair):-
pair_sum_to_2020([HI|TI], TJ, Numbers, Pair).

triple_sum_to_2020(Numbers, Triple):-
triple_sum_to_2020(Numbers, Numbers, Numbers, Triple).
triple_sum_to_2020([], _, _, _).
triple_sum_to_2020([], [], _, _).
triple_sum_to_2020([_|TI], [], Numbers, Triple):-
triple_sum_to_2020(TI, Numbers, Numbers, Triple).
triple_sum_to_2020([HI|_], [HJ|_], Numbers, Triple):-
Delta is 2020 - HI - HJ,
member(Delta, Numbers),
Triple = [HI, HJ, Delta],
triple_sum_to_2020([], [], Numbers, Triple).
triple_sum_to_2020([HI|TI], [_|TJ], Numbers, Triple):-
triple_sum_to_2020([HI|TI], TJ, Numbers, Triple).

write_pair_product([P0, P1]):-
Product is P0 * P1,
format("Product of ~d and ~d is ~d~n",[P0, P1, Product]).

write_triple_product([T0, T1, T2]):-
Product is T0 * T1 * T2,
format("Product of ~d and ~d and ~d is ~d~n", [T0, T1, T2, Product]).

main:-
close(Stream),
pair_sum_to_2020(Numbers, Pair),
write_pair_product(Pair),
triple_sum_to_2020(Numbers, Triple),
write_triple_product(Triple),
halt.
```

### Recent Posts from This Journal

• #### A Simple Binary Tree in Perl

This Binary Tree implementation uses Class::Struct and only defines find() and insert() operations, along with a simple print function for…

• #### The Weekly Challenge 127: Conflict Intervals (C++ Solutions)

Part 2 of The Weekly Challenge 127: You are given two sets with unique numbers. Write a script to figure out if they are disjoint. #include

• #### The Weekly Challenge 127: Disjoint Lists (C++ Solutions)

Part 1 of The Weekly Challenge 127: You are given two sets with unique numbers. Write a script to figure out if they are disjoint. #include

Comments for this post were disabled by the author