Compare commits
3 Commits
8f0915640c
...
4906e74cf3
Author | SHA1 | Date |
---|---|---|
Bill Ewanick | 4906e74cf3 | |
Bill Ewanick | 51ae128a14 | |
Bill Ewanick | e0aab0618b |
|
@ -0,0 +1,38 @@
|
|||
-- https://adventofcode.com/2024/day/1
|
||||
|
||||
import Data.List
|
||||
import GHC.IO
|
||||
|
||||
input :: FilePath
|
||||
input = "src/advent_of_code/2024/1.input"
|
||||
|
||||
entries :: ([Int], [Int])
|
||||
entries = unzip $ map parse' $ unsafePerformIO $ lines <$> readFile input
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
entries <- lines <$> readFile input
|
||||
print "Advent of Code 2024 - Day 1"
|
||||
|
||||
print $ "Part 1: " <> show (solveP1 $ unzip $ map parse' entries)
|
||||
print $ "Part 2: " <> show (solveP2 $ unzip $ map parse' entries)
|
||||
|
||||
parse' :: String -> (Int, Int)
|
||||
parse' str = (read l, read r)
|
||||
where
|
||||
l = take' str
|
||||
r = reverse $ take' $ reverse str
|
||||
take' = takeWhile (/= ' ')
|
||||
|
||||
solveP1 :: ([Int], [Int]) -> Int
|
||||
solveP1 (as, bs) = sum $ zipWith (\i j -> abs (i - j)) as' bs'
|
||||
where
|
||||
as' = sort as
|
||||
bs' = sort bs
|
||||
|
||||
solveP2 :: ([Int], [Int]) -> Int
|
||||
solveP2 (as, bs) = sum $ zipWith (*) as (map (f bs) as)
|
||||
where
|
||||
f :: [Int] -> Int -> Int
|
||||
f bs a = length $ elemIndices a bs
|
||||
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,6 @@
|
|||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
|
@ -5,9 +5,6 @@ The sum of these multiples is 23.
|
|||
|
||||
Find the sum of all the multiples of 3 or 5 below 1000.
|
||||
-}
|
||||
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
|
||||
{-# HLINT ignore "Use sum" #-}
|
||||
|
||||
module Main where
|
||||
|
||||
import Data.List (union)
|
||||
|
@ -16,26 +13,13 @@ main :: IO ()
|
|||
main = print ans
|
||||
|
||||
ans :: Integer
|
||||
ans = sum $ allNumbersUnder 1000
|
||||
|
||||
sum' :: Num a => [a] -> a
|
||||
sum' = foldl (+) 0
|
||||
|
||||
isMultipleOf :: Integral a => a -> a -> Bool
|
||||
isMultipleOf n x = x `rem` n == 0
|
||||
|
||||
all3sUnder :: Integral a => a -> [a]
|
||||
all3sUnder n = filter (isMultipleOf 3) [1..n-1]
|
||||
all5sUnder :: Integral a => a -> [a]
|
||||
all5sUnder n = filter (isMultipleOf 5) [1..n-1]
|
||||
|
||||
allXsUnderN :: Integral a => a -> a -> [a]
|
||||
allXsUnderN x n = filter (isMultipleOf x) [1..n-1]
|
||||
|
||||
allNumbersUnder :: Integral a => a -> [a]
|
||||
allNumbersUnder l = allXsUnderN 3 l <> allXsUnderN 5 l
|
||||
|
||||
ans = sum (_3s <> _5s)
|
||||
|
||||
_3s :: [Integer]
|
||||
_3s = [3,6..999]
|
||||
|
||||
_5s :: [Integer]
|
||||
_5s = [5,10..999]
|
||||
|
||||
solution :: Integer
|
||||
solution = sum $ union _3s _5s
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
fibs2 = [1, 2, 3, 5, 8, 13]
|
||||
|
||||
i = 1
|
||||
j = 2
|
||||
b = 0
|
||||
n = 4000000
|
||||
fibs = []
|
||||
fibs.append(i)
|
||||
fibs.append(j)
|
||||
|
||||
while b < n:
|
||||
b = i + j
|
||||
if b < n:
|
||||
fibs.append(b)
|
||||
else:
|
||||
fibs
|
||||
i = j
|
||||
j = b
|
||||
|
||||
count = 0
|
||||
for fib in fibs:
|
||||
if (fib % 2) == 0:
|
||||
count += fib
|
||||
else:
|
||||
count += 0
|
||||
|
||||
print(count)
|
||||
|
Loading…
Reference in New Issue