Browse Source

Starting Day 6

master
Nicolas Lenz 9 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
--
-- see: https://github.com/sol/hpack
--
-- hash: 75f48b18b69a82c9f1087410f03812dc6fc808bbb2528a2b9c06446be8979f29

name: advent-of-code
version: 0.0.0
@@ -32,6 +32,7 @@ executable advent-of-code
Day03
Day04
Day05
Day06
Paths_advent_of_code
hs-source-dirs:
src

+ 50
- 0
input/day06 View File

@@ -0,0 +1,50 @@
357, 59
312, 283
130, 47
89, 87
87, 58
158, 169
182, 183
300, 318
82, 257
200, 194
71, 259
112, 67
82, 163
107, 302
58, 194
40, 88
288, 339
64, 245
243, 302
41, 43
147, 276
143, 116
103, 178
262, 226
253, 157
313, 71
202, 236
353, 192
96, 74
167, 50
125, 132
90, 315
174, 232
185, 237
126, 134
152, 191
104, 315
283, 90
95, 193
252, 286
48, 166
69, 75
48, 349
59, 124
334, 95
263, 134
50, 314
196, 66
342, 221
60, 217

+ 47
- 0
src/Day06.hs View File

@@ -0,0 +1,47 @@
module Day06 (part1, part2) where

import Text.Parsec
import Data.Maybe
import Data.Either
import Data.List

-- Reduces the input and returns the length.
part1 :: [String] -> String
part1 input = show $ "…" where
coords = rights $ map (parse parseCoord "input/day06") input

-- Reduces the input once with every character removed and returns the length of the shortest reduct.
part2 :: [String] -> String
part2 [input] = show $ "…"

example :: [String]
example = ["1, 1", "1, 6", "8, 3", "3, 4", "5, 5", "8, 9"]


-- |Checks whether the coordinate at the given index will is bounded, i.e. whether exists coordinates with bigger and smaller x- and y-values.
isBounded :: [(Int, Int)] -> Int -> Bool
isBounded coords i = existsSmallerX && existsBiggerX && existsSmallerY && existsBiggerY where
from = coords !! i
existsSmallerX = isJust $ find ((>(fst from)). fst) coords
existsBiggerX = isJust $ find ((<(fst from)). fst) coords
existsSmallerY = isJust $ find ((>(snd from)). snd) coords
existsBiggerY = isJust $ find ((<(snd from)). snd) coords

nearestCoordIndex :: [(Int, Int)] -> (Int, Int) -> Maybe Int
nearestCoordIndex coords from = case findIndices distMinimal coords of
[] -> Nothing
[coord] -> Just coord
_ -> Nothing
where
distMinimal :: (Int, Int) -> Bool
distMinimal coord = all (\otherCoord -> from `distance` otherCoord >= from `distance` coord) coords

distance :: (Int, Int) -> (Int, Int) -> Int
distance (x1,y1) (x2,y2) = abs (x1 - x2) + abs (y1 - y2)

parseCoord :: Parsec String () (Int, Int)
parseCoord = do
x <- many1 digit
string ", "
y <- many1 digit
return (read x, read y)

+ 4
- 1
src/Main.hs View File

@@ -4,6 +4,7 @@ import Day02
import Day03
import Day04
import Day05
import Day06


solutions :: [((String, String), [String] -> String)]
@@ -17,7 +18,9 @@ solutions = [
(("04", "1"), Day04.part1),
(("04", "2"), Day04.part2),
(("05", "1"), Day05.part1),
(("05", "2"), Day05.part2)]
(("05", "2"), Day05.part2),
(("06", "1"), Day06.part1),
(("06", "2"), Day06.part2)]

main :: IO ()
main = do

Loading…
Cancel
Save