aboutsummaryrefslogtreecommitdiff
path: root/2022/day-02
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-02 16:19:50 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-02 16:19:50 -0500
commit3d979e5bcc7808d231f4749e810382abfb57a831 (patch)
treefc32e8ca8f7eaf1c365f692ea060d166d1d97ff4 /2022/day-02
parent9ab985aca36a98b446bf6fed1d26764b0d327c70 (diff)
downloadgleam_aoc-3d979e5bcc7808d231f4749e810382abfb57a831.tar.gz
gleam_aoc-3d979e5bcc7808d231f4749e810382abfb57a831.zip
day 2 in Prolog
Diffstat (limited to '2022/day-02')
-rw-r--r--2022/day-02/day-02.pl67
1 files changed, 67 insertions, 0 deletions
diff --git a/2022/day-02/day-02.pl b/2022/day-02/day-02.pl
new file mode 100644
index 0000000..6561ea5
--- /dev/null
+++ b/2022/day-02/day-02.pl
@@ -0,0 +1,67 @@
+:- use_module(library(yall)).
+:- use_module(library(apply)).
+
+% 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).
+
+% Predicates
+
+get_data(Result) :-
+ setup_call_cleanup(open("2022/day-02/input", 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.
+
+part_1_score([Them, Me], Score) :-
+ opponent_move(Them, TheirMove),
+ assume_move(Me, MyMove),
+ game(MyMove, TheirMove, Result),
+ score_game(MyMove, Result, Score).
+
+part_1_total(Total) :-
+ get_data(Games),
+ maplist(part_1_score, Games, Scores),
+ sum_list(Scores, Total).
+
+part_2_score([Them, Outcome], Score) :-
+ opponent_move(Them, TheirMove),
+ assume_outcome(Outcome, Result),
+ game(MyMove, TheirMove, Result),
+ score_game(MyMove, Result, Score).
+
+part_2_total(Total) :-
+ get_data(Games),
+ maplist(part_2_score, Games, Scores),
+ sum_list(Scores, Total). \ No newline at end of file