1
0
Fork 0

Day 24, a day late because I'm on vacation :/

This commit is contained in:
Jonathan Chan 2017-12-25 08:40:14 -08:00
parent adec393e53
commit 42ef138744
3 changed files with 90 additions and 1 deletions

33
24.hs Normal file
View File

@ -0,0 +1,33 @@
import Data.List.Split (splitOn)
import Data.Sequence (Seq, (!?), (<|), fromList, empty, findIndicesL, deleteAt)
type Port = Int
type Component = (Port, Port)
type Bridge = Seq Component
type Est = Bridge -> (Int, Bridge) -> (Int, Bridge)
bridgeStrength :: Bridge -> Int
bridgeStrength components = sum . fmap (uncurry (+)) $ components
strongest :: Est
strongest bridge s@(currStrength, _) =
if bridgeStrength bridge > currStrength then (bridgeStrength bridge, bridge) else s
longest :: Est
longest bridge l@(currLength, _) =
if length bridge > currLength then (length bridge, bridge) else l
estBridge :: Est -> Port -> Bridge -> Bridge
estBridge est port components =
let indicesOfPort = findIndicesL (\(a, b) -> a == port || b == port) components
subbridges = map subbridge indicesOfPort
in snd $ foldr est (0, empty) subbridges
where subbridge i =
let Just c@(a, b) = components !? i
in c <| estBridge est (if a == port then b else a) (deleteAt i components)
main :: IO ()
main = do
components <- fromList . map (\line -> let a : b : [] = splitOn "/" line in (read a, read b)) . lines <$> readFile "24.txt"
print $ bridgeStrength $ estBridge strongest 0 components
print $ bridgeStrength $ estBridge longest 0 components

55
24.txt Normal file
View File

@ -0,0 +1,55 @@
50/41
19/43
17/50
32/32
22/44
9/39
49/49
50/39
49/10
37/28
33/44
14/14
14/40
8/40
10/25
38/26
23/6
4/16
49/25
6/39
0/50
19/36
37/37
42/26
17/0
24/4
0/36
6/9
41/3
13/3
49/21
19/34
16/46
22/33
11/6
22/26
16/40
27/21
31/46
13/2
24/7
37/45
49/2
32/11
3/10
32/49
36/21
47/47
43/43
27/19
14/22
13/43
29/0
33/36
2/6

View File

@ -31,7 +31,8 @@ These are the runtimes of only one trial but the variances are fairly small and
| 20 | 0.168 |
| 21 | 4.013 |
| 22 | 12.867 | 7.880
| 23 | 0.274 |
| 23 | 0.274 |
| 24 | 6.887 |
Problems that could use some work: 05, 22, 14, 15, 21, 17