Skip to content

Commit

Permalink
Day_18(2023): solved
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheinxy committed Dec 18, 2023
1 parent 10429c5 commit e58113e
Show file tree
Hide file tree
Showing 5 changed files with 695 additions and 2 deletions.
51 changes: 51 additions & 0 deletions 2023/Day_18/Day_18.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Main where

import Data.Char (digitToInt)
import Numeric (readHex)
import System.Environment

type Vertex = (Int ,Int)

type Input = [(String, Int, String)]
type Output = Int

parseInput :: String -> Input
parseInput = map (go . words) . lines
where go [dir, dist, _:colour] = (dir, read dist, init colour)

digTranches :: Input -> [Vertex]
digTranches = scanl dig (0, 0)
where dig (r, c) ("L", n, _) = (r , c - n)
dig (r, c) ("R", n, _) = (r , c + n)
dig (r, c) ("U", n, _) = (r - n, c )
dig (r, c) ("D", n, _) = (r + n, c )

area :: [Vertex] -> Int
area vertices = 1 + perimeter `div` 2 + (abs . (`div` 2) . sum . zipWith crossProduct vertices $ tail vertices)
where crossProduct (r1, c1) (r2, c2) = c1 * r2 - r1 * c2
dist (r1, c1) (r2, c2) = abs (r1 - r2) + abs (c1 - c2)
perimeter = sum . zipWith dist vertices $ tail vertices

convertColour :: (String, Int, String) -> (String, Int, String)
convertColour (_, _, '#':colour) = (newDir, newDist, "#ffffff")
where distHex = init colour
dirNum = (digitToInt . last) colour
newDir = ["R", "D", "L", "U"] !! dirNum
newDist = (fst . head . readHex) distHex

partOne :: Input -> Output
partOne = area . digTranches

partTwo :: Input -> Output
partTwo = partOne . map convertColour

compute :: Input -> String -> IO ()
compute input "parse" = print input
compute input "one" = print . partOne $ input
compute input "two" = print . partTwo $ input
compute input _ = error "Unknown part"

main = do
args <- getArgs
input <- parseInput <$> readFile (last args)
mapM (compute input) $ init args
1 change: 1 addition & 0 deletions 2023/Day_18/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Day 18
Loading

0 comments on commit e58113e

Please sign in to comment.