r/adventofcode • • Dec 01 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 1 Solutions -🎄-

It's been one heck of a crappy year, so let's make the holidays bright with Advent of Code 2020! If you participated in a previous year, welcome back, and if you're new this year, we hope you have fun and learn lots!

We're following the same general format as previous years' megathreads, so make sure to read the full description in the wiki (How Do the Daily Megathreads Work?) before you post! If you have any questions, please create your own thread and ask!

Above all, remember, AoC is all about having fun and learning more about the wonderful world of programming!


[Update @ 00:04] Oops, server issues!

[Update @ 00:06]

  • Servers are up!

[Update @ 00:27]

[Update @ 01:26]

  • Many thanks to our live deejay Veloxxmusic for providing the best tunes I've heard all year!!!

NEW AND NOTEWORTHY THIS YEAR

  • Created new post flair for Other
  • When posting in the daily megathreads, make sure to mention somewhere in your post which language(s) your solution is written in

COMMUNITY NEWS

Advent of Code Community Fun 2020: Gettin' Crafty With It

  • Last year y'all got real creative with poetry and we all loved it. This year we're gonna up our own ante and increase scope to anything you make yourself that is related to Advent of Code. Any form of craft is valid as long as you make it yourself!
  • Full details, rules, timeline, templates, etc. are in the Submissions Megathread.
  • Several folks have forked /u/topaz2078's paste (source on GitHub) to create less minimalistic clones. If you wished paste had code syntax coloring and/or other nifty features, well then, check 'em out!
  • /u/brskbk - NoPaste
  • GitHub user EthanJohnson - paste fork on GitHub
  • /u/Tranzystorek - fork of EthanJohnson's fork on GitHub

--- Day 1: Report Repair ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for 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, thread unlocked at 00:??:??!

137 Upvotes

1.4k comments sorted by

View all comments

1

u/Mayalabielle Dec 01 '20 edited Dec 01 '20

Elixir, with a custom combinator in case we need it later in the AOC.

defmodule AOC do
  import AOC.Helper.Input

  defmodule D1 do
    def run1() do
      solve(2)
    end

    def run2() do
      solve(3)
    end

    def solve(len) do
      get_input("D1")
      |> split_input()
      |> Enum.map(fn v ->
        {int, _} = Integer.parse(v)
        int
      end)
      |> AOC.Helper.Combinator.combine(len)
      |> Enum.find(&(Enum.sum(&1) == 2020))
      |> List.flatten()
      |> Enum.reduce(1, &(&2 * &1))
    end
  end
end

And the combinator :

  defmodule Combinator do
    def combine(input, len)
    def combine(_, 0), do: [[]]
    def combine([], _), do: []

    def combine([h | t], len),
      do: Enum.map(combine(t, len - 1), &[h | &1]) ++ combine(t, len)
  end

Also my AOC solutions repo

1

u/daggerdragon Dec 01 '20

The code blocks are screwed up on old.reddit. Could you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks? Note that if you're using the visual editor, you may have to "Switch to Markdown" to get Reddit to understand the formatting properly.

2

u/Mayalabielle Dec 01 '20

Oh, didn't know about this, should be fixed. Thanks !

1

u/daggerdragon Dec 01 '20

There we go, thanks!

2

u/[deleted] Dec 01 '20 edited Jul 03 '23

[removed] — view removed comment

2

u/daggerdragon Dec 01 '20

I normally ban bots from /r/adventofcode on site. That /u/backtickbot is actively and noticeably reducing my workload on the megathreads, though, so... I think I've found a bot that's actually useful for once. It can stay as long as it doesn't get spammy!

1

u/[deleted] Dec 01 '20

Nice. Have to look into how this combinator works. Relatively new to elixir

1

u/Mayalabielle Dec 01 '20

Not an expert neither, but it's one of the language I love the most.