-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
2,570 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
import Advent.Day1 | ||
import Advent.Day1 | ||
import Advent.Day2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Advent.Util | ||
|
||
inductive RPS | rock | paper | scissors | ||
deriving BEq, Repr | ||
|
||
inductive GameResult | pOne | pTwo | draw | ||
deriving Repr | ||
|
||
def runGame : RPS → RPS → GameResult | ||
| .rock , .scissors => .pOne | ||
| .paper , .rock => .pOne | ||
| .scissors, .paper => .pOne | ||
| a , b => if a == b then .draw else .pTwo | ||
|
||
def scoreMove : RPS → Nat | ||
| .rock => 1 | ||
| .paper => 2 | ||
| .scissors => 3 | ||
|
||
def scoreResult : GameResult → Nat | ||
| .pOne => 0 | ||
| .draw => 3 | ||
| .pTwo => 6 | ||
|
||
def scoreGame (move : RPS) (res : GameResult) : Nat := scoreMove move + scoreResult res | ||
|
||
def String.toMove : String → RPS | ||
| "A" | "X" => .rock | ||
| "B" | "Y" => .paper | ||
| "C" | "Z" => .scissors | ||
| _ => .rock -- dummy stuff because I'm lazy | ||
|
||
def String.toResult : String → GameResult | ||
| "X" => .pOne | ||
| "Y" => .draw | ||
| "Z" => .pTwo | ||
| _ => .pOne -- dummy stuff because I'm lazy | ||
|
||
def figureMove : RPS → GameResult → RPS | ||
| mov, .draw => mov | ||
| .rock, .pOne => .scissors | ||
| .paper, .pOne => .rock | ||
| .scissors, .pOne => .paper | ||
| .rock, .pTwo => .paper | ||
| .paper, .pTwo => .scissors | ||
| .scissors, .pTwo => .rock | ||
|
||
def day2 : IO Unit := do | ||
let inputLines ← getInputLines (test := false) 2 | ||
let parsedInput1 := inputLines |>.map (fun s => s.split' (· == ' ')) | ||
|>.map (fun (s₁, s₂) => (s₁.toMove, s₂.toMove)) | ||
|>.map (fun (m₁, m₂) => (m₂, runGame m₁ m₂)) | ||
let answer1 := parsedInput1 |>.map (fun (mov, res) => scoreGame mov res) | ||
|>.sum | ||
|
||
let parsedInput2 := inputLines |>.map (fun s => s.split' (· == ' ')) | ||
|>.map (fun (s₁, s₂) => (s₁.toMove, s₂.toResult)) | ||
|>.map (fun (m₁, res) => (figureMove m₁ res, res)) | ||
let answer2 := parsedInput2 |>.map (fun (mov, res) => scoreGame mov res) | ||
|>.sum | ||
-- dbg_trace parsedInput2.map (reprStr) | ||
printAnswer (day := 2) answer1 answer2 | ||
|
||
#eval day2 |
Oops, something went wrong.