lwhjp, (edited )
@lwhjp@lemmy.sdf.org avatar

Haskell

This problem has a nice closed form solution, but brute force also works.

(My keyboard broke during part two. Yet another day off the bottom of the leaderboard…)


<span style="color:#323232;">import Control.Monad
</span><span style="color:#323232;">import Data.Bifunctor
</span><span style="color:#323232;">import Data.List
</span><span style="color:#323232;">
</span><span style="color:#323232;">readInput :: String -> [(Int, Int)]
</span><span style="color:#323232;">readInput = map ([t, d] -> (read t, read d)) . tail . transpose . map words . lines
</span><span style="color:#323232;">
</span><span style="color:#323232;">-- Quadratic formula
</span><span style="color:#323232;">wins :: (Int, Int) -> Int
</span><span style="color:#323232;">wins (t, d) =
</span><span style="color:#323232;">  let c = fromIntegral t / 2 :: Double
</span><span style="color:#323232;">      h = sqrt (fromIntegral $ t * t - 4 * d) / 2
</span><span style="color:#323232;">   in ceiling (c + h) - floor (c - h) - 1
</span><span style="color:#323232;">
</span><span style="color:#323232;">main = do
</span><span style="color:#323232;">  input <- readInput <$> readFile "input06"
</span><span style="color:#323232;">  print $ product . map wins $ input
</span><span style="color:#323232;">  print $ wins . join bimap (read . concatMap show) . unzip $ input
</span>
  • All
  • Subscribed
  • Moderated
  • Favorites
  • advent_of_code@programming.dev
  • kavyap
  • thenastyranch
  • mdbf
  • DreamBathrooms
  • everett
  • magazineikmin
  • GTA5RPClips
  • Youngstown
  • cisconetworking
  • ethstaker
  • slotface
  • ngwrru68w68
  • rosin
  • cubers
  • JUstTest
  • InstantRegret
  • Durango
  • osvaldo12
  • modclub
  • tester
  • Leos
  • khanakhh
  • normalnudes
  • tacticalgear
  • megavids
  • anitta
  • provamag3
  • lostlight
  • All magazines