r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -πŸŽ„- 2018 Day 3 Solutions -πŸŽ„-

--- Day 3: No Matter How You Slice It ---


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.


Advent of Code: The Party Game!

Click here for rules

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


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!

41 Upvotes

426 comments sorted by

View all comments

Show parent comment

3

u/pindab0ter Dec 06 '18 edited Dec 06 '18

Very nice solution and a nice write-up on the blog as well!

I didn't know where to start on this day's challenge, so I started off by copying yours and trying to learn ways of tackling this problem.

First of all, I haven't ever used groupingBy before, so thanks for showing me that. Without it it would've been a much taller order.

Secondly, I'm very excited about how much of a perfect marriage Kotlin seems to be between FP and OO. This assignment is a prime example.

I'll showcase two things I'm proud of:

1: Creating a Claim from a String

Your way of doing this was brilliant. I love the .destructured, but wanted to take it a little further by mapping String::toInt and dot-chaining everything, and the coolest thing; destructuring the List<Int> in the closure (I didn't know Kotlin allowed for destructuring lists):

fun fromString(input: String): Claim = Regex("""^#(\d+) @ (\d+),(\d+): (\d+)x(\d+)$""")
    .find(input)
    .let { it ?: throw IllegalArgumentException("Cannot parse $input to Claim") }
    .destructured
    .toList()
    .map(String::toInt)
    .let { (id, x, y, width, height) ->
        Claim(id, Square(x, y, width, height))
    }

2: I found a nice single expression for part two

This is where I diverge from your solution. You maintain state for both yet uncovered claims and for the cloth as a whole.

My solution: find the first element that doesn't overlap with anything.

Sounds simple, right? It took between 15 and 20 minutes, though…

fun findNonOverlappingClaim(claims: List<Claim>): Claim = claims.first { claim ->
    claims.minus(claim).none { it.overlapsWith(claim) }
}

fun Claim.overlapsWith(other: Claim) = square.occupies.any { other.square.occupies.contains(it) }

Thanks again for showing me some new tricks. Please have a look through my solution as I think you might enjoy it :)

GitHub repo

2

u/chicagocode Dec 07 '18

Hi, thank you so much for the comments, you are too kind. I like your parsing expression, that's really slick! :)