day2
This commit is contained in:
		
							
								
								
									
										2500
									
								
								day2/input.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2500
									
								
								day2/input.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										72
									
								
								day2/main.hs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								day2/main.hs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,72 @@
 | 
			
		||||
import System.IO
 | 
			
		||||
 | 
			
		||||
data Part = P1 | P2
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
data Shape = Rock | Paper | Scissors deriving (Eq, Show)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
shape :: Char -> Shape
 | 
			
		||||
shape 'A' = Rock
 | 
			
		||||
shape 'B' = Paper
 | 
			
		||||
shape 'C' = Scissors
 | 
			
		||||
shape 'X' = Rock
 | 
			
		||||
shape 'Y' = Paper
 | 
			
		||||
shape 'Z' = Scissors
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
opShp :: String -> Shape
 | 
			
		||||
opShp = shape . head
 | 
			
		||||
 | 
			
		||||
beats = [(Rock, Scissors),(Scissors, Paper),(Paper, Rock)]
 | 
			
		||||
 | 
			
		||||
myShp2 :: Char -> Shape -> Shape
 | 
			
		||||
myShp2 'X' o = snd $ head $ filter ((== o) . fst) beats
 | 
			
		||||
myShp2 'Y' o = o
 | 
			
		||||
myShp2 'Z' o = fst $ head $ filter ((== o) . snd) beats
 | 
			
		||||
 | 
			
		||||
myShp :: Part -> String -> Shape
 | 
			
		||||
myShp P1 g = shape $ last g
 | 
			
		||||
myShp P2 g = myShp2 (last g) (opShp g)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
shapeScore :: Shape -> Int
 | 
			
		||||
shapeScore Rock     = 1
 | 
			
		||||
shapeScore Paper    = 2
 | 
			
		||||
shapeScore Scissors = 3
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
instance Ord Shape where
 | 
			
		||||
    Rock `compare` Scissors = GT
 | 
			
		||||
    Scissors `compare` Rock = LT
 | 
			
		||||
    s1 `compare` s2 = (shapeScore s1) `compare` (shapeScore s2)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
winScore :: (Shape, Shape) -> Int
 | 
			
		||||
winScore (me, op)
 | 
			
		||||
    | me <  op = 0
 | 
			
		||||
    | me == op = 3
 | 
			
		||||
    | me >  op = 6
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
gameScore :: Part -> String -> Int
 | 
			
		||||
gameScore p s = shapeScore me + winScore (me, op)
 | 
			
		||||
    where me = myShp p s
 | 
			
		||||
          op = opShp s
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
handler :: String -> String
 | 
			
		||||
handler s = (show $ sum $ map (gameScore P1) $ lines s) ++ "\n" ++
 | 
			
		||||
            (show $ sum $ map (gameScore P2) $ lines s) ++ "\n"
 | 
			
		||||
 | 
			
		||||
main :: IO ()
 | 
			
		||||
main = do
 | 
			
		||||
    interact handler
 | 
			
		||||
		Reference in New Issue
	
	Block a user