r/adventofcode • • Dec 24 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 24 Solutions -๐ŸŽ„-

--- Day 24: Electromagnetic Moat ---


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


[Update @ 00:18] 62 gold, silver cap

  • Been watching Bright on Netflix. I dunno why reviewers are dissing it because it's actually pretty cool. It's got Will Smith being grumpy jaded old man Will Smith, for the love of FSM...

[Update @ 00:21] Leaderboard cap!

  • One more day to go in Advent of Code 2017... y'all ready to see Santa?

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

9 Upvotes

108 comments sorted by

View all comments

Show parent comment

1

u/flup12 Dec 24 '17

This is turning into the year of the Iterator!

type Component = List[Int]

case class Bridge(components: List[Component] = Nil, endPoint: Int = 0) {
  def strength: Int = components.flatten.sum
  def length: Int = components.length
  def connect(c: Component): Option[Bridge] = c match {
    case List(p1, p2) if p1 == endPoint => Some(Bridge(c :: components, p2))
    case List(p1, p2) if p2 == endPoint => Some(Bridge(c :: components, p1))
    case _ => None
  }
}

def getBridges(soFar: Bridge, components: Set[Component]): Iterator[Bridge] = {
  val bridges = components.toIterator.flatMap(soFar.connect)
  if (bridges.isEmpty) Iterator(soFar)
  else bridges.flatMap(connected => getBridges(connected, components - connected.components.head))
}

val components = input.map(_.split("/").map(_.toInt).toList).toSet
val part1 = getBridges(Bridge(), components).map(_.strength).max
val part2 = getBridges(Bridge(), components).map(bridge => (bridge.length, bridge.strength)).max._2