Created
December 11, 2020 02:52
-
-
Save mjgpy3/f8ee61117b15d9f71b6076a570512e04 to your computer and use it in GitHub Desktop.
aoc-2020-day10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import qualified Data.Set as S | |
paths n vs = do | |
next <- S.toList $ S.filter (`elem` [n+1, n+2, n+3]) vs | |
case paths next vs of | |
[] -> [[n, next]] | |
subpaths -> do | |
subpath <- subpaths | |
[n:subpath] | |
s x y z = x z (y z) | |
pairwise = s zip tail | |
deltas = map (uncurry $ flip (-)) . pairwise | |
solve vs = | |
let | |
path = head $ filter (\p -> length p == length vs) $ paths 0 $ S.fromList vs | |
ds = deltas path | |
ones = filter (== 1) ds | |
threes = filter (== 3) ds | |
in | |
length ones * length threes | |
main = do | |
vs <- map read <$> words <$> readFile "./d10.txt" | |
let withAdaptor = [0] ++ vs ++ [maximum vs + 3] | |
print $ solve withAdaptor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import qualified Data.Map.Strict as M | |
import Data.List (sort, foldl') | |
import Data.Maybe (catMaybes) | |
solve = foldl' accumulateWaysToMake (M.singleton 0 1) . sort | |
where | |
accumulateWaysToMake wtm v = M.insert v (sum $ prior3 v wtm) wtm | |
prior3 v nums = catMaybes $ (`M.lookup` nums) . (v -) <$> [1, 2, 3] | |
main = do | |
vs <- map read . words <$> readFile "./d10.txt" | |
let final = maximum vs + 3 | |
print $ solve (vs ++ [final]) M.! final |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open('./d10.txt', 'r') as f: | |
nums = set(map(int, f.read().strip().split('\n'))) | |
last = max(nums) + 3 | |
first = 0 | |
nums.add(first) | |
nums.add(last) | |
ways_to_make = { 0: 1 } | |
def last_3(v, nums): | |
for d in [1, 2, 3]: | |
if v-d in nums: | |
yield v-d | |
for v in sorted(nums): | |
if v: | |
ways_to_make[v] = sum(ways_to_make[prior] for prior in last_3(v, nums)) | |
print(ways_to_make[last]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment