Skip to content

Commit

Permalink
2022 02: don't use strIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
cherryblossom000 committed Dec 6, 2022
1 parent e92ceb7 commit 9e51689
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions 2022/02/main.idr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ Outcome = Int
Input : Type
Input = List (Shape, Int)

partial parse : String -> Input
parse = map (\l => (ord (l `strIndex` 0) - 65, ord (l `strIndex` 2) - 88)) . lines . trim
parse : String -> Maybe Input
parse = traverse ((\l => Just (ord !(getAt 0 l) - 65, ord !(getAt 2 l) - 88)) . fastUnpack)
. lines
. trim

shapeScore : Shape -> Int
shapeScore = (+1)
Expand All @@ -28,10 +30,11 @@ part1 = sum . map (\(a, b) => shapeScore b + outcomeScore ((b - a + 1) `mod` 3))
part2 : Input -> Int
part2 = sum . map (\(a, b) => shapeScore ((a + b - 1) `mod` 3) + outcomeScore b)

partial main : IO ()
main : IO ()
main = do
Right input <- readFile {io = IO} "input.txt"
| Left e => die (show e)
let xs = parse input
let Just xs = parse input
| Nothing => die "parse error"
printLn $ part1 xs
printLn $ part2 xs

0 comments on commit 9e51689

Please sign in to comment.