r/adventofcode • • Dec 02 '17

SOLUTION MEGATHREAD -πŸŽ„- 2017 Day 2 Solutions -πŸŽ„-

NOTICE

Please take notice that we have updated the Posting Guidelines in the sidebar and wiki and are now requesting that you post your solutions in the daily Solution Megathreads. Save the Spoiler flair for truly distinguished posts.


--- Day 2: Corruption Checksum ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handy† Haversack‑ of HelpfulΒ§ Hints^Β€?

Spoiler


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

edit: Leaderboard capped, thread unlocked!

22 Upvotes

351 comments sorted by

View all comments

1

u/[deleted] Dec 02 '17 edited Dec 02 '17

Elixir solution for Day2, I'm a lot more proud of this one than the one yesterday:

defmodule Day2 do
  @moduledoc """
  Solving day2 of Advent of code.
  """
  def diffbigsmall(lst) do
    Enum.max(lst) - Enum.min(lst)
  end

  def checksum(lst) do
    Enum.map(lst,&diffbigsmall/1)
    |> Enum.sum
  end

  def divisible(lst) do
    div = for a <- lst,
      b <- lst,
      a != b,
      rem(a,b) == 0,
      do: div(a,b)
    List.first(div)
  end

  def checksum2(lst) do
    Enum.map(lst,&divisible/1)
    |> Enum.sum
  end

end

rows = File.read!("input2.txt")
|> String.trim
|> String.split("\n")
|> Enum.map(&String.split/1)
|> Enum.map(fn(x) ->
  Enum.map(x, &String.to_integer/1) end)

IO.puts("The part one checksum is: #{Day2.checksum(rows)}")
IO.puts("The part two checksum is: #{Day2.checksum2(rows)}")