diff options
author | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
---|---|---|
committer | H.J <thechairman@thechairman.info> | 2024-10-09 11:36:55 -0400 |
commit | 8777ff071f7bb37631baa7b6717ad29961e50911 (patch) | |
tree | 6d59c4ed58e454b960339c3d1151f0a879e8d7cb /racket/aoc2022/day-02/day-02.pl | |
parent | 6156a9ef7be4012063a042aafb4e9b0d7eadde8e (diff) | |
download | gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.tar.gz gleam_aoc-8777ff071f7bb37631baa7b6717ad29961e50911.zip |
sorting by language
Diffstat (limited to 'racket/aoc2022/day-02/day-02.pl')
-rw-r--r-- | racket/aoc2022/day-02/day-02.pl | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/racket/aoc2022/day-02/day-02.pl b/racket/aoc2022/day-02/day-02.pl new file mode 100644 index 0000000..707da41 --- /dev/null +++ b/racket/aoc2022/day-02/day-02.pl @@ -0,0 +1,72 @@ +:- use_module(library(yall)). +:- use_module(library(apply)). + +output_solutions :- + get_data(Games), + part1_total(Games, Part1), + write(Part1), nl, !, + part2_total(Games, Part2), + write(Part2), nl. + +% Facts + +game(X, X, draw). +game(rock, scissors, win). +game(scissors, paper, win). +game(paper, rock, win). +game(rock, paper, lose). +game(scissors, rock, lose). +game(paper, scissors, lose). + +opponent_move("A", rock). +opponent_move("B", paper). +opponent_move("C", scissors). + +assume_move("X", rock). +assume_move("Y", paper). +assume_move("Z", scissors). + +assume_outcome("X", lose). +assume_outcome("Y", draw). +assume_outcome("Z", win). + +bonus(rock, 1). +bonus(paper, 2). +bonus(scissors, 3). +bonus(lose, 0). +bonus(draw, 3). +bonus(win, 6). + +% Rules + +get_data(Result) :- + setup_call_cleanup(open("2022/day-02/prolog-input.txt", read, In), + (read_string(In, _, Str), + split_string(Str, "\n", "\s\t\n", Lines), + maplist([In, Out] >> split_string(In, "\s", "", Out), Lines, Result)), + close(In)). + +score_game(MyMove, Result, Score) :- + bonus(Result, X), + bonus(MyMove, Y), + Score is X + Y. + +part1_score([Them, Me], Score) :- + opponent_move(Them, TheirMove), + assume_move(Me, MyMove), + game(MyMove, TheirMove, Result), + score_game(MyMove, Result, Score). + +part1_total(Games, Total) :- + maplist(part1_score, Games, Scores), + sum_list(Scores, Total). + +part2_score([Them, Outcome], Score) :- + opponent_move(Them, TheirMove), + assume_outcome(Outcome, Result), + game(MyMove, TheirMove, Result), + score_game(MyMove, Result, Score). + +part2_total(Games, Total) :- + maplist(part2_score, Games, Scores), + sum_list(Scores, Total).
\ No newline at end of file |