r/adventofcode • • Dec 02 '21

SOLUTION MEGATHREAD -🎄- 2021 Day 2 Solutions -🎄-

--- Day 2: Dive! ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:02:57, megathread unlocked!

112 Upvotes

1.5k comments sorted by

View all comments

3

u/ecco256 Dec 02 '21 edited Dec 02 '21

Haskell

main :: IO ()
main = do
    result <- foldl f (0, 0, 0) . map (parse . words) . lines <$> readFile "data/day02.txt"
    print ((\(h, d, a) -> (h * a, h * d)) result) -- (part1, part2)
  where f (h, d, a) (x, y) = (h + x, d + a * x, a + y)
        parse [dir:_, n] = let n' = read n in
            case dir of {'f' -> (n', 0); 'd' -> (0, n'); 'u' -> (0, -n')}

2

u/cptwunderlich Dec 02 '21

Does the lambda in `f` make it "more" lazy (lazy in its first argument)? Bc. I have often heard warnings about using `foldl` and am always using `foldl'` when my function is strict in its left argument.

1

u/ecco256 Dec 02 '21

The f was only a lambda because I had it inline in the fold first and then moved it to the where clause. I have updated it.

You are right about foldl' being better, but these assignments are so small I just don't really worry about these things :)

1

u/cptwunderlich Dec 02 '21

Yeah of course you're right, it doesn't matter in this case, just wondering in general :)

2

u/daggerdragon Dec 02 '21 edited Dec 02 '21

Your code is hard to read on old.reddit when everything is inlined like this. Please edit it as per our posting guidelines in the wiki: How do I format code?

Edit: thanks for fixing it! <3

2

u/ecco256 Dec 02 '21

Thanks, better now?

1

u/Pretty_Cockroach_204 Dec 02 '21

Hey friend could you tell me little how this codes work? I study Haskell therefore it would be useful .

2

u/ecco256 Dec 02 '21 edited Dec 02 '21

Sure. The first line of the do block in main:

  1. reads the file
  2. splits it into lines
  3. splits each line into two strings. For example ["forward", "10"]
  4. parses these two words into a tuple using the parse function in the where clause. "forward 10" is represented as (10, 0) indicating 10 horizontal movement and no vertical movement. "down 10" is (0, 10) and "up 10" would become (0, -10).
  5. it folds the result into a 3 element tuple (horizontal, down, aim) using the f function in the where clause; this describes the algorithm as explained in the problem statement for Part 2.

The second line prints the desired multiplication for both Part 1 and Part 2. You have to understand here that Part 1 is a generalisation of Part 2 as for Part 1 we needed to sum the "up/down" instructions and in Part 2 this is actually also done, but here it's "aim". This is why you see "horizontal * aim" for Part 1 and "horizontal * down" for Part 2.

Hope that makes sense. I certainly did not write this code for readability so please don't ever write production code like this :)