Browse Source

Day 8 - cleanup

main
Jens Kadenbach 2 years ago
parent
commit
ce3466f746
2 changed files with 12 additions and 16 deletions
  1. 11
    14
      src/Day8.hs
  2. 1
    2
      test/Day8Spec.hs

+ 11
- 14
src/Day8.hs View File

35
       let row = M.getRow x forest
35
       let row = M.getRow x forest
36
           col = M.getCol y forest
36
           col = M.getCol y forest
37
           -- left and top are reversed to order the as seen from (x,y)
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
           right  = V.drop y row
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
           bottom = V.drop x col
41
           bottom = V.drop x col
42
           v = forest ! (x,y)
42
           v = forest ! (x,y)
43
       in f v [left, right, top, bottom]
43
       in f v [left, right, top, bottom]
45
 checkVisibility :: Forest -> Visibillity
45
 checkVisibility :: Forest -> Visibillity
46
 checkVisibility = checkAgainstNeighbours isVisible
46
 checkVisibility = checkAgainstNeighbours isVisible
47
   where
47
   where
48
-    isVisible v = any (all (< v))
48
+    isVisible treeHeight = any (all (< treeHeight))
49
 
49
 
50
 countVisible :: Visibillity -> Int
50
 countVisible :: Visibillity -> Int
51
-countVisible vis = getSum $ foldMap (Sum . isTrue) vis
51
+countVisible = foldMap (Sum . isTrue) >>> getSum
52
   where
52
   where
53
     isTrue True = 1
53
     isTrue True = 1
54
     isTrue False = 0
54
     isTrue False = 0
56
 checkScenicScores :: Forest -> M.Matrix Int
56
 checkScenicScores :: Forest -> M.Matrix Int
57
 checkScenicScores = checkAgainstNeighbours allDistances
57
 checkScenicScores = checkAgainstNeighbours allDistances
58
   where
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
 viewingDistance :: Int -> V.Vector Int -> Int
61
 viewingDistance :: Int -> V.Vector Int -> Int
62
 viewingDistance treeHeight neighbors
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
   | otherwise = 0
66
   | otherwise = 0
67
 
67
 
68
 findMaxScenicScore :: Forest -> Int
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
 day8 :: IO ()
71
 day8 :: IO ()
74
 day8 = do
72
 day8 = do
75
    input <- readFile "ressources/day08-input"
73
    input <- readFile "ressources/day08-input"
76
    putStrLn "Day8"
74
    putStrLn "Day8"
77
    let forest = parseForest input
75
    let forest = parseForest input
78
-   let visibleTrees = checkVisibility >>>
79
-                      countVisible $ forest
76
+   let visibleTrees = checkVisibility >>> countVisible $ forest
80
    putStrLn ("Number of visible trees is " ++ show visibleTrees)
77
    putStrLn ("Number of visible trees is " ++ show visibleTrees)
81
    let highestScenicScore = findMaxScenicScore forest
78
    let highestScenicScore = findMaxScenicScore forest
82
    putStrLn ("Highest scenic score is " ++ show highestScenicScore)
79
    putStrLn ("Highest scenic score is " ++ show highestScenicScore)
83
-   
80
+
84
 
81
 

+ 1
- 2
test/Day8Spec.hs View File

23
    x,x,x,o,x,
23
    x,x,x,o,x,
24
    x,x,o,x,x,
24
    x,x,o,x,x,
25
    x,o,x,o,x,
25
    x,o,x,o,x,
26
-   x,x,x,x,x
27
-  ]
26
+   x,x,x,x,x]
28
   where
27
   where
29
     x = True
28
     x = True
30
     o = False
29
     o = False

Loading…
Cancel
Save