Browse Source

Starting Day 6

master
Nicolas Lenz 5 months ago
parent
commit
c809f3bb4d
4 changed files with 103 additions and 1 deletions
  1. 2
    0
      advent-of-code.cabal
  2. 50
    0
      input/day06
  3. 47
    0
      src/Day06.hs
  4. 4
    1
      src/Main.hs

+ 2
- 0
advent-of-code.cabal View File

@@ -4,7 +4,7 @@ cabal-version: 1.12
4 4
 --
5 5
 -- see: https://github.com/sol/hpack
6 6
 --
7
+-- hash: 75f48b18b69a82c9f1087410f03812dc6fc808bbb2528a2b9c06446be8979f29
7 8
 
8 9
 name:           advent-of-code
9 10
 version:        0.0.0
@@ -32,6 +32,7 @@ executable advent-of-code
32 32
       Day03
33 33
       Day04
34 34
       Day05
35
+      Day06
35 36
       Paths_advent_of_code
36 37
   hs-source-dirs:
37 38
       src

+ 50
- 0
input/day06 View File

@@ -0,0 +1,50 @@
1
+357, 59
2
+312, 283
3
+130, 47
4
+89, 87
5
+87, 58
6
+158, 169
7
+182, 183
8
+300, 318
9
+82, 257
10
+200, 194
11
+71, 259
12
+112, 67
13
+82, 163
14
+107, 302
15
+58, 194
16
+40, 88
17
+288, 339
18
+64, 245
19
+243, 302
20
+41, 43
21
+147, 276
22
+143, 116
23
+103, 178
24
+262, 226
25
+253, 157
26
+313, 71
27
+202, 236
28
+353, 192
29
+96, 74
30
+167, 50
31
+125, 132
32
+90, 315
33
+174, 232
34
+185, 237
35
+126, 134
36
+152, 191
37
+104, 315
38
+283, 90
39
+95, 193
40
+252, 286
41
+48, 166
42
+69, 75
43
+48, 349
44
+59, 124
45
+334, 95
46
+263, 134
47
+50, 314
48
+196, 66
49
+342, 221
50
+60, 217

+ 47
- 0
src/Day06.hs View File

@@ -0,0 +1,47 @@
1
+module Day06 (part1, part2) where
2
+
3
+import Text.Parsec
4
+import Data.Maybe
5
+import Data.Either
6
+import Data.List
7
+
8
+-- Reduces the input and returns the length.
9
+part1 :: [String] -> String
10
+part1 input = show $ "…"  where
11
+    coords = rights $ map (parse parseCoord "input/day06") input
12
+
13
+-- Reduces the input once with every character removed and returns the length of the shortest reduct.
14
+part2 :: [String] -> String
15
+part2 [input] = show $ "…"
16
+
17
+example :: [String]
18
+example = ["1, 1", "1, 6", "8, 3", "3, 4", "5, 5", "8, 9"]
19
+
20
+
21
+-- |Checks whether the coordinate at the given index will is bounded, i.e. whether exists coordinates with bigger and smaller x- and y-values.
22
+isBounded :: [(Int, Int)] -> Int -> Bool
23
+isBounded coords i = existsSmallerX && existsBiggerX && existsSmallerY && existsBiggerY where
24
+    from = coords !! i
25
+    existsSmallerX = isJust $ find ((>(fst from)). fst) coords
26
+    existsBiggerX = isJust $ find ((<(fst from)). fst) coords
27
+    existsSmallerY = isJust $ find ((>(snd from)). snd) coords
28
+    existsBiggerY = isJust $ find ((<(snd from)). snd) coords
29
+
30
+nearestCoordIndex :: [(Int, Int)] -> (Int, Int) -> Maybe Int
31
+nearestCoordIndex coords from = case findIndices distMinimal coords of
32
+    [] -> Nothing
33
+    [coord] -> Just coord
34
+    _ -> Nothing
35
+    where
36
+        distMinimal :: (Int, Int) -> Bool
37
+        distMinimal coord = all (\otherCoord -> from `distance` otherCoord >= from `distance` coord) coords
38
+
39
+distance :: (Int, Int) -> (Int, Int) -> Int
40
+distance (x1,y1) (x2,y2) = abs (x1 - x2) + abs (y1 - y2)
41
+
42
+parseCoord :: Parsec String () (Int, Int)
43
+parseCoord = do
44
+    x <- many1 digit
45
+    string ", "
46
+    y <- many1 digit
47
+    return (read x, read y)

+ 4
- 1
src/Main.hs View File

@@ -4,6 +4,7 @@ import Day02
4 4
 import Day03
5 5
 import Day04
6 6
 import Day05
7
+import Day06
7 8
 
8 9
 
9 10
 solutions :: [((String, String), [String] -> String)]
@@ -17,7 +18,9 @@ solutions = [
17 18
     (("04", "1"), Day04.part1),
18 19
     (("04", "2"), Day04.part2),
19 20
     (("05", "1"), Day05.part1),
20
-    (("05", "2"), Day05.part2)]
21
+    (("05", "2"), Day05.part2),
22
+    (("06", "1"), Day06.part1),
23
+    (("06", "2"), Day06.part2)]
21 24
 
22 25
 main :: IO ()
23 26
 main = do

Loading…
Cancel
Save