I tried some of this recently. The peach flavor was a bit too sweet for me, but the plain stuff is <3
That sounds lIke fun! What do you do about hills? Do you have power assist?
It was nice to see some of the same faces (as it were) again from last year!
Also great to see more Haskell solutions, and props to those crazy enough to write in J and Uiua.
Sorry to hear that :/
I think you handled it well.
Haskell
This was quite fun! I got a bit distracted trying to rewrite safe
in point-free style, but I think this version is the most readable. There's probably a more monadic way of writing lessOne
as well, but I can't immediately see it.
safe xs = any gradual [diffs, negate <$> diffs]
where
diffs = zipWith (-) (drop 1 xs) xs
gradual = all (`elem` [1 .. 3])
lessOne [] = []
lessOne (x : xs) = xs : map (x :) (lessOne xs)
main = do
input :: [[Int]] <- map (map read . words) . lines <$> readFile "input02"
print . length $ filter safe input
print . length $ filter (any safe . lessOne) input
Haskell
Plenty of scope for making part 2 faster, but I think simple is best here. Forgot to sort the lists in the first part, which pushed me waaay off the leaderboard.
import Data.List
main = do
[as, bs] <- transpose . map (map read . words) . lines <$> readFile "input01"
print . sum $ map abs $ zipWith (-) (sort as) (sort bs)
print . sum $ map (\a -> a * length (filter (== a) bs)) as
Most people would use "word", "half-word", "quarter-word" etc, but the Anglophiles insist on "tuppit", "ternary piece", "span" and "chunk" (that's 5 bits, or 12 old bits).
Maybe it was due to attempting the puzzles in real-time for the first time, but it felt like there was quite a spike in difficulty this year. Day 5 (If You Give A Seed A Fertilizer) in particular was pretty tough for an early puzzle.
Day 8 (Haunted Wasteland), Day 20 (Pulse Propagation) and Day 21 (Step Counter) were (I felt) a bit mean due to hidden properties of the input data.
I particularly liked Day 6 (Wait For It), Day 14 (Parabolic Reflector Dish) and Day 24 (Never Tell Me The Odds), although that one made my brain hurt.
Day 25 (Snowverload) had me reading research papers, although in the end I stumbled across Karger's algorithm. That's the first time I've used a probabilistic approach. This solution in particular was very clever.
I learned the Shoelace formula and Pick's theorem this year, which will be very helpful to remember.
Perhaps I'll try using Prolog or J next year :)
Haskell
A little slow (1.106s on my machine), but list operations made this really easy to write. I expect somebody more familiar with Haskell than me will be able to come up with a more elegant solution.
Nevertheless, 59th on the global leaderboard today! Woo!
Solution
import Data.List
import qualified Data.Map.Strict as Map
import Data.Semigroup
rotateL, rotateR, tiltW :: Endo [[Char]]
rotateL = Endo $ reverse . transpose
rotateR = Endo $ map reverse . transpose
tiltW = Endo $ map tiltRow
where
tiltRow xs =
let (a, b) = break (== '#') xs
(os, ds) = partition (== 'O') a
rest = case b of
('#' : b') -> '#' : tiltRow b'
[] -> []
in os ++ ds ++ rest
load rows = sum $ map rowLoad rows
where
rowLoad = sum . map (length rows -) . elemIndices 'O'
lookupCycle xs i =
let (o, p) = findCycle 0 Map.empty xs
in xs !! if i < o then i else (i - o) `rem` p + o
where
findCycle i seen (x : xs) =
case seen Map.!? x of
Just j -> (j, i - j)
Nothing -> findCycle (i + 1) (Map.insert x i seen) xs
main = do
input <- lines <$> readFile "input14"
print . load . appEndo (tiltW <> rotateL) $ input
print $
load $
lookupCycle
(iterate (appEndo $ stimes 4 (rotateR <> tiltW)) $ appEndo rotateL input)
1000000000
42.028 line-seconds
Haskell
This was fun and (fairly) easy! Off-by-one errors are a likely source of bugs here.
import Control.Monad
import Data.List
import Data.List.Split
import Data.Maybe
score d pat = ((100 *) <$> search pat) `mplus` search (transpose pat)
where
search pat' = find ((d ==) . rdiff pat') [1 .. length pat' - 1]
rdiff pat' i =
let (a, b) = splitAt i pat'
in length $ filter (uncurry (/=)) $ zip (concat $ reverse a) (concat b)
main = do
input <- splitOn [""] . lines <$> readFile "input13"
let go d = print . sum . map (fromJust . score d) $ input
go 0
go 1
Line-seconds score: 0.102
π
TDD
const max12 = (x, y) => {
if (x === 1 && y === 2) {
return 2;
} else if (x === 7 && y === 4) {
return 7;
} else {
return x;
}
};
Genius!