Sfoglia il codice sorgente

Day 8 - cleanup

main
Jens Kadenbach 2 anni fa
parent
commit
ce3466f746
2 ha cambiato i file con 12 aggiunte e 16 eliminazioni
  1. 11
    14
      src/Day8.hs
  2. 1
    2
      test/Day8Spec.hs

+ 11
- 14
src/Day8.hs Vedi File

@@ -35,9 +35,9 @@ checkAgainstNeighbours f forest  = M.matrix (M.nrows forest) (M.ncols forest) ch
35 35
       let row = M.getRow x forest
36 36
           col = M.getCol y forest
37 37
           -- left and top are reversed to order the as seen from (x,y)
38
-          left   = V.reverse $ V.take (y - 1) row 
38
+          left   = V.reverse $ V.take (y - 1) row
39 39
           right  = V.drop y row
40
-          top    = V.reverse $ V.take (x - 1) col 
40
+          top    = V.reverse $ V.take (x - 1) col
41 41
           bottom = V.drop x col
42 42
           v = forest ! (x,y)
43 43
       in f v [left, right, top, bottom]
@@ -45,10 +45,10 @@ checkAgainstNeighbours f forest  = M.matrix (M.nrows forest) (M.ncols forest) ch
45 45
 checkVisibility :: Forest -> Visibillity
46 46
 checkVisibility = checkAgainstNeighbours isVisible
47 47
   where
48
-    isVisible v = any (all (< v))
48
+    isVisible treeHeight = any (all (< treeHeight))
49 49
 
50 50
 countVisible :: Visibillity -> Int
51
-countVisible vis = getSum $ foldMap (Sum . isTrue) vis
51
+countVisible = foldMap (Sum . isTrue) >>> getSum
52 52
   where
53 53
     isTrue True = 1
54 54
     isTrue False = 0
@@ -56,29 +56,26 @@ countVisible vis = getSum $ foldMap (Sum . isTrue) vis
56 56
 checkScenicScores :: Forest -> M.Matrix Int
57 57
 checkScenicScores = checkAgainstNeighbours allDistances
58 58
   where
59
-    allDistances tree neighbors = getProduct $ foldMap (Product . viewingDistance tree) neighbors
59
+    allDistances treeHeight neighbors = foldMap (viewingDistance treeHeight >>> Product) >>> getProduct $ neighbors
60 60
 
61 61
 viewingDistance :: Int -> V.Vector Int -> Int
62 62
 viewingDistance treeHeight neighbors
63
-  | V.length neighbors == 0 = 0
64
-  | V.head neighbors >= treeHeight = 1
65
-  | V.head neighbors < treeHeight = 1 + viewingDistance treeHeight (V.tail neighbors)
63
+  | V.length neighbors == 0         = 0
64
+  | V.head neighbors >= treeHeight  = 1
65
+  | V.head neighbors <  treeHeight  = 1 + viewingDistance treeHeight (V.tail neighbors)
66 66
   | otherwise = 0
67 67
 
68 68
 findMaxScenicScore :: Forest -> Int
69
-findMaxScenicScore forest = maximum $ M.toList scores
70
-  where
71
-    scores = checkScenicScores forest
69
+findMaxScenicScore = checkScenicScores >>> M.toList >>> maximum
72 70
 
73 71
 day8 :: IO ()
74 72
 day8 = do
75 73
    input <- readFile "ressources/day08-input"
76 74
    putStrLn "Day8"
77 75
    let forest = parseForest input
78
-   let visibleTrees = checkVisibility >>>
79
-                      countVisible $ forest
76
+   let visibleTrees = checkVisibility >>> countVisible $ forest
80 77
    putStrLn ("Number of visible trees is " ++ show visibleTrees)
81 78
    let highestScenicScore = findMaxScenicScore forest
82 79
    putStrLn ("Highest scenic score is " ++ show highestScenicScore)
83
-   
80
+
84 81
 

+ 1
- 2
test/Day8Spec.hs Vedi File

@@ -23,8 +23,7 @@ testVisibility = M.fromList 5 5
23 23
    x,x,x,o,x,
24 24
    x,x,o,x,x,
25 25
    x,o,x,o,x,
26
-   x,x,x,x,x
27
-  ]
26
+   x,x,x,x,x]
28 27
   where
29 28
     x = True
30 29
     o = False

Loading…
Annulla
Salva