day 2: rewrite
use Data.Text Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
		
							
								
								
									
										10
									
								
								day2.hs
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								day2.hs
									
									
									
									
									
								
							@@ -1,8 +1,12 @@
 | 
				
			|||||||
 | 
					import Data.Text (Text)
 | 
				
			||||||
 | 
					import qualified Data.Text as T (lines, unpack)
 | 
				
			||||||
 | 
					import Lib (readFile')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
-- we do not use a simple 9 value case cuz yeah
 | 
					-- we do not use a simple 9 value case cuz yeah
 | 
				
			||||||
 | 
					
 | 
				
			||||||
main :: IO ()
 | 
					main :: IO ()
 | 
				
			||||||
main = do
 | 
					main = do
 | 
				
			||||||
  choices <- parse <$> readFile "day2.in"
 | 
					  choices <- parse <$> readFile' "day2.in"
 | 
				
			||||||
  putStr "Q1: "
 | 
					  putStr "Q1: "
 | 
				
			||||||
  print $ q1 choices
 | 
					  print $ q1 choices
 | 
				
			||||||
  putStr "Q2: "
 | 
					  putStr "Q2: "
 | 
				
			||||||
@@ -17,8 +21,8 @@ instance Ord Choice where
 | 
				
			|||||||
  compare Scissors Rock = LT
 | 
					  compare Scissors Rock = LT
 | 
				
			||||||
  compare _ _ = GT
 | 
					  compare _ _ = GT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
parse :: String -> [(Choice, Choice)]
 | 
					parse :: Text -> [(Choice, Choice)]
 | 
				
			||||||
parse = map (\[x, _, y] -> (val x 'A', val y 'X')) . lines
 | 
					parse = map ((\[x, _, y] -> (val x 'A', val y 'X')) . T.unpack) . T.lines
 | 
				
			||||||
 | 
					
 | 
				
			||||||
score :: (Choice, Choice) -> Int
 | 
					score :: (Choice, Choice) -> Int
 | 
				
			||||||
score (i, j) = 1 + fromEnum j + 3 * fromEnum (compare j i)
 | 
					score (i, j) = 1 + fromEnum j + 3 * fromEnum (compare j i)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user