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!
  • 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!

--- 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

2

u/wace001 Dec 01 '20

Kotlin. 1293/1888. Technical issues suck. Hope the servers have had their morning coffee tomorrow.

fun main() {
    val nums = getInput(2020, 1).lines().map { it.toInt() }
    for (a in nums) for (b in nums) if (a + b == 2020) println("Part 1: ${a * b}")
    for (a in nums) for (b in nums) for (c in nums) if (a + b + c == 2020) println("Part 2: ${a * b * c}")
}

2

u/reckter Dec 01 '20

I always forget how clean kotlin with non functional chains can be^^I prefere those for aoc though:

    override fun solvePart1() {
        loadInput()
            .toIntegers()
            .allPairings(includeSelf = false, bothDirections = false)
            .find { (a, b)-> a + b == 2020 }
            ?.let { (a, b) -> a * b }
            ?.solution(1)
    }

    override fun solvePart2() {
        val input = loadInput().toIntegers()

        input
            .allPairings(input)
            .allPairings(input)
            .map { (pair, last) ->
                Triple(pair.first, pair.second, last)
            }
            .find { (a, b , c) -> a + b + c == 2020 }
            ?.let { (a, b, c) -> a * b * c }
            ?.solution(2)
    }

And: .allPairings .solution\ .loadInput .toIntegers are helper functions by me, written over the last few years. They allow regression test, auto-downloading (with caching of course!)

1

u/daggerdragon Dec 01 '20

Hope the servers have had their morning coffee tomorrow.

Us too... oy. Thank you for your patience while /u/topaz2078 got them back up!

1

u/or9ob Dec 01 '20

Mine was a bit more complicated... :)

Part 1

val data = File("path/to/input.txt").readLines().map { it.toInt() }
val complements = data.associateBy({ it }, { 2020 - it })
val match = complements.filterValues {
    complements.keys.contains(it)
}.entries.first()

return match.key * match.value

Part 2

val complementOfPairs = data.map { x -> data.minus(x).map { y -> Pair(x, y) } }
    .flatten()
    .associateBy({ it }, { 2020 - (it.first + it.second) })

val match = complementOfPairs.filterValues {
    data.contains(it)
}.entries.first()

return match.key.first * match.key.second *match.value