re: AoC Day 1: Chronal Calibration VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Is it normal that part 2 is killing my CPU :D ? It's been running for minutes in Elixir but nada. Anyhow, I've tried day 1 with all three language...
 

At a glance, in the Elixir part two solution it looks like the list of numbers is just being passed through as is. So, it is never moving on to the rest of the numbers and instead the sum is just adding the first number in the list over and over.

Because repeated_numbers is never reassigned the recursive call find_total(repeated_numbers, totals, sum) is passing the unchanged list of all the numbers through to the next iteration.

 

At a glance, in the Elixir part two solution it looks like the list of numbers is just being passed through as is. So, it is never moving on to the rest of the numbers and instead the sum is just adding the first number in the list over and over.

That's probably it, I thought that:

hd(Enum.take(repeated_numbers, 1))

would take 1 number (and hence move the cursor).

It does, but because Elixir is immutable it returns a new list with the first element from repeated_numbers instead of mutating (changing in place) repeating_numbers

Also, this line hd(Enum.take(repeated_numbers, 1)) could be rewritten as hd(repeated_numbers) to get the same functionality. The more "Elixir way to write that would be to use pattern matching. Something like [head | tail] = repeated_numbers Here is some more info on pattern matching, if you're interested. I think it is super cool!

Thank you! :-)

I have to get used again to immutability and transformation instead of mutability and assignment :D

code of conduct - report abuse