Safe Haskell | None |
---|
Day10
Description
General - Connect jolts/volts.
Part 1 - The trick here is to read the problem description carefully. You then realize that you can solve the problem by building the diffs between two consecutive numbers and just count the number of 1 and 3 diffs. Multiply the number of 1s and 3s.
Part 2 - My first attempt ... failed. The idea was to build all possible combinations/arrangements and then just filter for the valid ones. That worked for the testcases, but failed (miserably) for the puzzle input (had to kill it after running it for an hour).
My second attempt goes about it differently. It builds a tree. It starts with the outlet (0) and then tries to find all adapters that are [1,2,3] jolts away from the current adapter until it finds the adapter that fits into the device. The solution are all paths in the tree, but (again) ... this takes too long.
My third attempt just counts the paths that I can build, but (you guessed it) ...
My last attempt wasis based on this thread - https:tedn.lys5w.
Synopsis
- type Jolt = Int
- data Node = Node Jolt [Node]
- input :: String -> [Jolt]
- diffs :: [Jolt] -> [Int]
- part1 :: [Jolt] -> Int
- valid :: [Jolt] -> Bool
- combinations :: Int -> [Jolt] -> [[Jolt]]
- arrangements :: [Jolt] -> [[Jolt]]
- makeTree :: [Jolt] -> Jolt -> Node
- allPaths :: [Jolt] -> [[Jolt]] -> Node -> [[Jolt]]
- countPaths :: [Jolt] -> Jolt -> Jolt -> Integer -> Integer
- adjacent :: Jolt -> Jolt -> [Int] -> [[Int]] -> [[Int]]
- part2' :: [Jolt] -> Int
- part2'' :: [Jolt] -> Int
- part2''' :: [Jolt] -> Integer
- part2 :: [Jolt] -> Integer
Documentation
The tree of Jolts
combinations :: Int -> [Jolt] -> [[Jolt]] #
Build all combinations of jolts.
arrangements :: [Jolt] -> [[Jolt]] #
Build all possible arrangements.
makeTree :: [Jolt] -> Jolt -> Node #
Build a tree (from the given list of jolts) and return the root of the tree.
countPaths :: [Jolt] -> Jolt -> Jolt -> Integer -> Integer #
Count the paths that fit into the device.