Init
This commit is contained in:
35
2/solution.nix
Normal file
35
2/solution.nix
Normal file
@@ -0,0 +1,35 @@
|
||||
with import ../utilities.nix;
|
||||
|
||||
let
|
||||
# read file
|
||||
input = readFile ./input;
|
||||
matches = map (line: convertMvId (splitString " " line)) (splitString "\n" input);
|
||||
|
||||
# convert moves to numbers for opponent and player
|
||||
# Rock -> 0, Paper -> 1, Scissors 2
|
||||
mvId = c: m: (charToInt m) - (charToInt c);
|
||||
convertMvId = pair: split pair (a: b: [(mvId "A" a) (mvId "X" b)] );
|
||||
|
||||
# scoring matches
|
||||
isWin = a: b: (mod (a+1) 3) == b;
|
||||
isTie = a: b: a == b;
|
||||
getScore = a: b:
|
||||
b + 1 + # add score for the move itself
|
||||
(if isWin a b then 6
|
||||
else if isTie a b then 3
|
||||
else 0); # loss
|
||||
totalScore = m: sum (map (pair: split pair getScore) m);
|
||||
|
||||
# part 2
|
||||
mkDraw = a: a;
|
||||
mkWin = a: (mod (a+1) 3);
|
||||
mkLoss = a: (mod (a+2) 3);
|
||||
bestMove = a: b:
|
||||
if b == 0 then mkLoss a
|
||||
else if b == 1 then mkDraw a
|
||||
else mkWin a;
|
||||
makeBestMoves = m: map (pair: split pair (a: b: [a (bestMove a b)] )) m;
|
||||
in {
|
||||
part1 = totalScore matches;
|
||||
part2 = totalScore (makeBestMoves matches);
|
||||
}
|
||||
Reference in New Issue
Block a user