Safe HaskellNone

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

Documentation

type Jolt = Int #

data Node #

The tree of Jolts

Constructors

Node Jolt [Node] 

Instances

Instances details
Eq Node # 
Instance details

Defined in Day10

Methods

(==) :: Node -> Node -> Bool

(/=) :: Node -> Node -> Bool

Show Node # 
Instance details

Defined in Day10

Methods

showsPrec :: Int -> Node -> ShowS

show :: Node -> String

showList :: [Node] -> ShowS

input :: String -> [Jolt] #

Read the input file.

diffs :: [Jolt] -> [Int] #

Returns the diffs between two consecutive jolts.

part1 :: [Jolt] -> Int #

Solve part1.

valid :: [Jolt] -> Bool #

A list of jolts is valid, if they are never more that 3 appart.

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.

allPaths :: [Jolt] -> [[Jolt]] -> Node -> [[Jolt]] #

Build all possible paths (from the given node).

countPaths :: [Jolt] -> Jolt -> Jolt -> Integer -> Integer #

Count the paths that fit into the device.

adjacent :: Jolt -> Jolt -> [Int] -> [[Int]] -> [[Int]] #

Built combination of paths based on adjacent jolts being equal.

part2' :: [Jolt] -> Int #

Solve part2 (based on counting the arrangements).

part2'' :: [Jolt] -> Int #

Solve part2 (based on counting all paths in the tree).

part2''' :: [Jolt] -> Integer #

Solve part2 (with an optimized version of part2'')

part2 :: [Jolt] -> Integer #

Solve part2.