Skip to content

Instantly share code, notes, and snippets.

@gnusosa
Created December 2, 2021 04:37
Show Gist options
  • Save gnusosa/8a2cd6a41c8c886b3902b7fa7aa630c3 to your computer and use it in GitHub Desktop.
Save gnusosa/8a2cd6a41c8c886b3902b7fa7aa630c3 to your computer and use it in GitHub Desktop.
sweeps :: [Integer]
sweeps = [199,
200,
208,
210,
200,
207,
240,
269,
260,
263]
countSweepsWindows :: (Ord b, Num a) => [b] -> a
countSweepsWindows xs = foldl (\acc x -> if ((snd x) > (fst x)) then acc + 1 else acc) 0 $ zip xs (drop 1 xs)
slidingWindowSweeps :: Int -> Int -> [a] -> [[a]]
slidingWindowSweeps n d [] = []
slidingWindowSweeps n d xs = [take n xs] ++ slidingWindowSweeps n d (drop d xs)
part1 :: (Ord b, Num a) => [b] -> a
part1 xs = countSweepsWindows xs
part2 :: (Ord b, Num a, Num b) => [b] -> a
part2 xs = countSweepsWindows $ map (sum) $ filter (\x -> length x == 3) (slidingWindowSweeps 3 1 xs)
@hg-ev
Copy link

hg-ev commented Dec 2, 2021

well done, need to read up on my haskell. Here are my answers:
perl -lne '$sum += 1 if $prev and ($_ - $prev) > 0; $prev = $_; END { print $sum }' input.txt
perl -lne '$sum += 1 if $n_1 and $n_2 and $n_3 and ($_ + $n_1 + $n_2) - ($n_1 + $n_2 + $n_3) > 0; $n_3 = $n_2; $n_2 = $n_1; $n_1 = $_; END { print $sum }' input.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment