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

1

u/paulvelthuis Dec 03 '18

My solution in Scala. I am wondering if the findElfId can be more effective, since it loops again over the input. Any of your thoughts on how to improve this are welcome :).

import scala.collection.mutable
import scala.io.Source

object Day3 {

  val matrix = Array.ofDim[Int](1000, 1000)
  val suitId = collection.mutable.Map[Int, Boolean]()

  def initialize() = {
    val input = Source.fromFile("src/main/scala/files/inputday3.txt").getLines
    for (line <- input) {
      // replace the double dot to make coordinate split correct
      val claim = line.replace(":", "").split(" ")
      val coord = claim(2).split(",")
      val dimension = claim(3).split("x")
      // all the x dimensions, we go from the starting coordinate until the coordinate + the x dimension
      for (x: Int <- coord(0).toInt until coord(0).toInt + dimension(0).toInt) {
        // all the y dimensions, we go from the starting coordinate until the coordinate + the y dimension
        for (y: Int <- coord(1).toInt until coord(1).toInt + dimension(1).toInt) {
          // every place occupied we add one, so if two people want to have it the matrix value is 2
          matrix(x)(y) += 1
        }
      }
    }
  }

  /**
    * find the elfId that can make the suit, because it has the fabric with no claims
    * we loop once more over the input list
    * This loop is required to verify if
    * @return the elfId that has the fabric without claims make the suit, or 0 if no elf has a fabric that is unclaimed by others
    */
    def findElfId(): Int = {
      val input = Source.fromFile("src/main/scala/files/inputday3.txt").getLines
      for (line <- input) {
        var intact = true
        // replace the double dot to make coordinate split correct
        val claim = line.replace(":", "").split(" ")
        val elfId: Int = claim(0).replace("#", "").toInt
        val coord = claim(2).split(",")
        val dimension = claim(3).split("x")
        // all the x dimensions, we go from the starting coordinate until the coordinate + the x dimension
        for (x: Int <- coord(0).toInt until coord(0).toInt + dimension(0).toInt) {
          // all the y dimensions, we go from the starting coordinate until the coordinate + the y dimension
          for (y: Int <- coord(1).toInt until coord(1).toInt + dimension(1).toInt) {
            if (matrix(x)(y) >= 2) {
              intact = false
            }
          }
        }
        if (intact){
          return elfId
        }
      }
      0
    }

  def findDuplicate(): Int = {
    // find the duplicates
    var duplicate: Int = 0
    for (i: Int <- 0 until 1000) {
      for (j: Int <- 0 until 1000) {
        if (matrix(i)(j) > 1)
          duplicate += 1
      }
    }
    duplicate
  }

  def main(args: Array[String]): Unit = {
    initialize()
    println(findDuplicate())
    println(findElfId())
  }
}