2015 Day 01 in Haskell
This commit is contained in:
		
							parent
							
								
									dde31570ed
								
							
						
					
					
						commit
						6e4b95af18
					
				
							
								
								
									
										19
									
								
								2015/day01/day01.hs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								2015/day01/day01.hs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
				
			|||||||
 | 
					import Aoc ( aocMain )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					parseFile :: [String] -> [Char]
 | 
				
			||||||
 | 
					parseFile lines = let [line] = lines in line
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					part1 :: [Char] -> Int
 | 
				
			||||||
 | 
					part1 ps = count '(' - count ')'
 | 
				
			||||||
 | 
					    where count c = length $ filter (==c) ps
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					part2 :: [Char] -> Int
 | 
				
			||||||
 | 
					part2 = f 0
 | 
				
			||||||
 | 
					    where 
 | 
				
			||||||
 | 
					        f 0 (')':_) = 1
 | 
				
			||||||
 | 
					        f n (')':ps) = f (n-1) ps + 1
 | 
				
			||||||
 | 
					        f n ('(':ps) = f (n+1) ps + 1
 | 
				
			||||||
 | 
					        f _ _ = undefined
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					main :: IO ()
 | 
				
			||||||
 | 
					main = aocMain parseFile part1 part2
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user