day1
This commit is contained in:
		
							
								
								
									
										25
									
								
								Makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								Makefile
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,25 @@
 | 
			
		||||
DAY = 1
 | 
			
		||||
 | 
			
		||||
DIR = day$(DAY)
 | 
			
		||||
 | 
			
		||||
INPUT = $(DIR)/input.txt
 | 
			
		||||
 | 
			
		||||
SRC = $(shell find $(DIR) -name "main.hs" -or -name "main.zig")
 | 
			
		||||
 | 
			
		||||
ifeq ($(patsubst %hs,,$(SRC)),)
 | 
			
		||||
	RUNNER = runhaskell
 | 
			
		||||
else
 | 
			
		||||
	RUNNER = zig run
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
.PHONY: run
 | 
			
		||||
run: $(SRC) $(INPUT)
 | 
			
		||||
	$(RUNNER) $(SRC) < $(INPUT)
 | 
			
		||||
 | 
			
		||||
$(DIR):
 | 
			
		||||
	mkdir -p $(DIR)
 | 
			
		||||
 | 
			
		||||
$(INPUT): | $(DIR)
 | 
			
		||||
	@echo on
 | 
			
		||||
	curl -H @${HOME}/creds/aoc-cookie https://adventofcode.com/2022/day/$(DAY)/input > $@
 | 
			
		||||
							
								
								
									
										2265
									
								
								day1/input.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2265
									
								
								day1/input.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										52
									
								
								day1/main.hs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								day1/main.hs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,52 @@
 | 
			
		||||
import System.IO
 | 
			
		||||
import Data.List
 | 
			
		||||
import Data.List.Split
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
toNum' :: String -> Int
 | 
			
		||||
toNum' "" = -1
 | 
			
		||||
toNum' s = read s :: Int
 | 
			
		||||
 | 
			
		||||
toNum :: [String] -> [Int]
 | 
			
		||||
toNum = map toNum'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
groups :: [Int] -> [[Int]]
 | 
			
		||||
groups = splitOn [-1]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
parseElves :: String -> [[Int]]
 | 
			
		||||
parseElves = groups . toNum . lines
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
elveTotals :: [[Int]] -> [Int]
 | 
			
		||||
elveTotals = map sum
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
topElf :: [[Int]] -> Int
 | 
			
		||||
topElf = maximum . elveTotals
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
sortDesc :: Ord a => [a] -> [a]
 | 
			
		||||
sortDesc = sortBy (flip compare)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
top3Elves :: [[Int]] -> Int
 | 
			
		||||
top3Elves = sum . take 3 . sortDesc . elveTotals
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
handler :: String -> String
 | 
			
		||||
handler s = (show $ topElf $ parseElves s) ++ "\n" ++
 | 
			
		||||
            (show $ top3Elves $ parseElves s) ++ "\n"
 | 
			
		||||
 | 
			
		||||
main :: IO ()
 | 
			
		||||
main = do
 | 
			
		||||
    interact handler
 | 
			
		||||
		Reference in New Issue
	
	Block a user