lwhjp,
@lwhjp@lemmy.sdf.org avatar

Haskell

Wasn’t able to start on time today, but this was a fun one! Got to apply the two theorems I learned from somebody else’s solution to Day 10.

Solutionimport Data.Char import Data.List readInput :: String -> (Char, Int, String) readInput s = let [d, n, c] = words s in (head d, read n, drop 2 $ init c) boundary :: [(Char, Int)] -> [(Int, Int)] boundary = scanl’ step (0, 0) where step (x, y) (d, n) = let (dx, dy) = case d of ‘U’ -> (0, 1) ‘D’ -> (0, -1) ‘L’ -> (-1, 0) ‘R’ -> (1, 0) in (x + n * dx, y + n * dy) area :: [(Char, Int)] -> Int area steps = let a = – shoelace formula (abs . (quot2) . sum) . (zipWith ((x, y) (x’, y’) -> x * y’ - x’ * y) <*> tail) $ boundary steps in a + 1 + sum (map snd steps)quot 2 – Pick’s theorem part1, part2 :: [(Char, Int, String)] -> Int part1 = area . map ((d, n, _) -> (d, n)) part2 = area . map ((_, _, c) -> decode c) where decode s = (“RDLU” !! digitToInt (last s), read $ “0x” ++ init s) main = do input <- map readInput . lines <$> readFile “input18” print $ part1 input print $ part2 input

  • All
  • Subscribed
  • Moderated
  • Favorites
  • advent_of_code@programming.dev
  • DreamBathrooms
  • magazineikmin
  • Durango
  • ngwrru68w68
  • rosin
  • Youngstown
  • osvaldo12
  • cubers
  • slotface
  • InstantRegret
  • tester
  • kavyap
  • thenastyranch
  • khanakhh
  • JUstTest
  • mdbf
  • modclub
  • everett
  • cisconetworking
  • GTA5RPClips
  • Leos
  • ethstaker
  • tacticalgear
  • normalnudes
  • anitta
  • provamag3
  • megavids
  • lostlight
  • All magazines