1
0
Fork 0

Compare commits

..

No commits in common. "4906e74cf3ce4dfdf150c045321479bfa04afa5d" and "8f0915640c3d31748d87db068e0f8e305f1569af" have entirely different histories.

6 changed files with 22 additions and 1081 deletions

View File

@ -83,9 +83,6 @@
gofumpt
nodejs
python310
black
]);
shellHook = ''

View File

@ -1,38 +0,0 @@
-- 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

View File

@ -1,6 +0,0 @@
3 4
4 3
2 5
1 3
3 9
3 3

View File

@ -5,6 +5,9 @@ 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)
@ -13,13 +16,26 @@ main :: IO ()
main = print ans
ans :: Integer
ans = sum (_3s <> _5s)
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
_3s :: [Integer]
_3s = [3,6..999]
_5s :: [Integer]
_5s = [5,10..999]
solution :: Integer
solution = sum $ union _3s _5s

View File

@ -1,28 +0,0 @@
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)