-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.hs
61 lines (53 loc) · 1.45 KB
/
run.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
{-# LANGUAGE LambdaCase #-}
parse :: String -> (Char, Char)
parse =
\case [a, _, x] ->
(a, x)
parseAll :: String -> [(Char, Char)]
parseAll = map parse . lines
score1 :: (Char, Char) -> Int
score1 round =
let result = case round of
('A', 'X') -> 3
('A', 'Y') -> 6
('A', 'Z') -> 0
('B', 'X') -> 0
('B', 'Y') -> 3
('B', 'Z') -> 6
('C', 'X') -> 6
('C', 'Y') -> 0
('C', 'Z') -> 3
choice = case snd round of
'X' -> 1
'Y' -> 2
'Z' -> 3
in result + choice
score2 :: (Char, Char) -> Int
score2 round =
let (result, chosen) =
case round of
('A', 'X') -> (0, 'C')
('A', 'Y') -> (3, 'A')
('A', 'Z') -> (6, 'B')
('B', 'X') -> (0, 'A')
('B', 'Y') -> (3, 'B')
('B', 'Z') -> (6, 'C')
('C', 'X') -> (0, 'B')
('C', 'Y') -> (3, 'C')
('C', 'Z') -> (6, 'A')
choice = case chosen of
'A' -> 1
'B' -> 2
'C' -> 3
in result + choice
part1 :: [(Char, Char)] -> Int
part1 = sum . map score1
part2 :: [(Char, Char)] -> Int
part2 = sum . map score2
main :: IO ()
main = main' "input.txt"
main' :: FilePath -> IO ()
main' file = do
input <- parseAll <$> readFile file
print (part1 input)
print (part2 input)