Compare commits
No commits in common. "a2d4fdc341b386ef60f1bc2c7db54256327f4dc7" and "173bab5a0fcf3d8c06eaaa3015ccec9116e91960" have entirely different histories.
a2d4fdc341
...
173bab5a0f
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +0,0 @@
|
|||
example.txt
|
|
@ -12,8 +12,6 @@ https://adventofcode.com/2020/
|
|||
| 4 | `**` | `**` | |
|
||||
| 5 | | `**` | |
|
||||
| 6 | | `**` | |
|
||||
| 7 | `**` | | |
|
||||
| 8 | | `**` | |
|
||||
| 9 | | `**` | |
|
||||
| 6 | `**` | | |
|
||||
|
||||
`test.sh` can be used to run all solutions and automatically compares them to (my) puzzle inputs and the expected outputs.
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
26134589
|
||||
3535124
|
1000
data/day9.input
1000
data/day9.input
File diff suppressed because it is too large
Load diff
30
day9/day9.hs
30
day9/day9.hs
|
@ -1,30 +0,0 @@
|
|||
import AoC
|
||||
|
||||
import Control.Monad
|
||||
import Data.List
|
||||
import Data.Maybe
|
||||
|
||||
slidingWindow :: Int -> [a] -> [[a]]
|
||||
slidingWindow n xs
|
||||
| length xs < n = []
|
||||
| otherwise = take n xs : slidingWindow n (tail xs)
|
||||
|
||||
summingTo :: (Eq a, Num a) => Int -> a -> [a] -> Maybe [a]
|
||||
n `summingTo` x = find ((==x) . sum) . replicateM n
|
||||
|
||||
sublistSummingTo :: (Num a, Ord a) => a -> [a] -> Maybe [a]
|
||||
sublistSummingTo n = fmap snd . find ((== n) . fst) . scanl go (0, [])
|
||||
where go = dropNeeded .: addToAcc
|
||||
addToAcc (sum, parts) x = (sum+x, parts++[x])
|
||||
dropNeeded = fromJust . find ((<= n) . fst) . iterate dropPart
|
||||
dropPart (sum, (x:xs)) = (sum-x, xs)
|
||||
|
||||
part1 :: [Int] -> Int
|
||||
part1 = snd . fromJust . find (not . uncurry doesSum) . map (\xs -> (init xs, last xs)) . slidingWindow 26
|
||||
where doesSum xs y = isJust $ 2 `summingTo` y $ xs
|
||||
|
||||
part2 :: [Int] -> Int
|
||||
part2 xs = minimum range + maximum range
|
||||
where range = fromJust $ sublistSummingTo (part1 xs) xs
|
||||
|
||||
main = runAoC (map read . lines) part1 part2
|
13
test.sh
13
test.sh
|
@ -25,22 +25,13 @@ run_solution() {
|
|||
fi
|
||||
}
|
||||
|
||||
shopt -s dotglob
|
||||
|
||||
for day in day*; do
|
||||
input=data/$day.input
|
||||
expected=data/$day.expected
|
||||
for solution in "$day"/*; do
|
||||
echo -n "$solution... "
|
||||
|
||||
case "${solution##*/}" in
|
||||
example.txt|.*)
|
||||
echo SKIP
|
||||
continue
|
||||
esac
|
||||
|
||||
solution_output=$(mktemp)
|
||||
trap "rm -f '$solution_output'" EXIT
|
||||
trap "rm $solution_output" EXIT
|
||||
run_solution "$solution" > "$solution_output"
|
||||
|
||||
if ! diff -u "$expected" "$solution_output"; then
|
||||
|
@ -57,7 +48,5 @@ for day in day*; do
|
|||
else
|
||||
echo ok
|
||||
fi
|
||||
|
||||
rm "$solution_output"
|
||||
done
|
||||
done
|
||||
|
|
Loading…
Reference in a new issue