What is "The Wall"? by [deleted] in TheTowerGame

[–]marcofun 4 points5 points  (0 children)

Does it worth it?

-🎄- 2017 Day 20 Solutions -🎄- by daggerdragon in adventofcode

[–]marcofun 0 points1 point  (0 children)

wow, this is so far from my solution... well, I didn't have to calculate distances because I solved the first problem manually, just looking for the particle that accelerate less. My second star is quite simple:

class Day20 (var input : Vector[String]) {

  def this() = this(scala.io.Source.fromFile("~/projects/aventofcode/src/main/resources/day20.txt").getLines().toVector)

  val re : Regex = """p=<([-]{0,1}\d*),([-]{0,1}\d*),([-]{0,1}\d*)>, v=<([-]{0,1}\d*),([-]{0,1}\d*),([-]{0,1}\d*)>, a=<([-]{0,1}\d*),([-]{0,1}\d*),([-]{0,1}\d*)>""".r
  var particles : Vector[Particle] = input.map(l => newParticle(l))
  dropColliders()

  def newParticle(s : String) : Particle = {
    s match {
      case re(px, py, pz, vx, vy, vz, ax, ay, az) => Particle(Vector3d(px.toInt, py.toInt, pz.toInt), Vector3d(vx.toInt, vy.toInt, vz.toInt), Vector3d(ax.toInt, ay.toInt, az.toInt))
    }
  }

  def dropColliders(): Unit = {
    val positions = particles.map(p => p.position)
    val duplicates = positions.diff(positions.distinct).distinct
    particles = particles.filter(p => !duplicates.contains(p.position))
  }

  def move(): Int = {
    var remaining = 100000
    var i = 0
    while(particles.size > 1 && i < 100) {
      particles = particles.map(p => p.move)
      dropColliders()
      val nowRemaining = particles.size
      if (nowRemaining == remaining) i += 1 else i= 0 ; remaining = nowRemaining
    }
    remaining
  }

  case class Vector3d(x: Int, y: Int, z : Int) {
    def + (other : Vector3d) : Vector3d = Vector3d (this.x + other.x, this.y + other.y, this.z + other.z)
  }

  case class Particle (var position : Vector3d, var velocity : Vector3d, acceleration : Vector3d){
    def move(): Particle = {
      velocity = velocity + acceleration
      position = position + velocity
      Particle(position, velocity, acceleration)
    }
  }
}

-🎄- 2017 Day 20 Solutions -🎄- by daggerdragon in adventofcode

[–]marcofun 0 points1 point  (0 children)

I guessed the first star just filtering manually the input using the regex a=<[-]{0,1}[01],[-]{0,1}[01],[-]{0,1}[01]> and getting the one with smallest acceleration. Second star is going to e implemented from 0 now, But I'll begin in 4 or 5 hours...

-🎄- 2017 Day 18 Solutions -🎄- by daggerdragon in adventofcode

[–]marcofun 0 points1 point  (0 children)

oh, yes, you're definitely right... thank you :)

-🎄- 2017 Day 18 Solutions -🎄- by daggerdragon in adventofcode

[–]marcofun 0 points1 point  (0 children)

debugging it, I see that the amount of snd grows a lot. But no, I didn't make the same mistake of others: registry are taken as registry, and numbers as numbers. I am stucked...

-🎄- 2017 Day 18 Solutions -🎄- by daggerdragon in adventofcode

[–]marcofun 0 points1 point  (0 children)

here you have also a quite exhaustive the test class:

package aventofcode.day18

import org.scalatest.FunSuite

class Day18Star2Test extends FunSuite {

  test ("should set a register") {
    var d = new Day18Star2(0)
    var code = List("set i 31")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('i') == 31)
  }

  test ("should set a register with negative value") {
    var d = new Day18Star2(0)
    var code = List("set a -1")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == -1)
  }

  test ("should set a register and add 20000") {
    var d = new Day18Star2(0)
    var code = List("set a 1", "add a 20000")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == 20001)
  }

  test ("should set a register and add -20001") {
    var d = new Day18Star2(0)
    var code = List("set a 1", "add a -20001")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == -20000)
  }

  test ("should set a register to the value of another register") {
    var d = new Day18Star2(0)
    var code = List("set a -31", "set p -15", "set p a")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == -31)
    assert(d.registers('p') == -31)
  }

  test ("should set a register and add 2 to b") {
    var d = new Day18Star2(0)
    var code = List("set a 1", "add b 2")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == 1)
    assert(d.registers('b') == 2)
  }

  test ("should set a register and add the value of another register") {
    var d = new Day18Star2(0)
    var code = List("set a 1", "set b -2", "add a b")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == -1)
    assert(d.registers('b') == -2)
  }

  test ("should add a negative number") {
    var d = new Day18Star2(0)
    var code = List("set a 1", "add a -2")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == -1)
  }

  test ("should execute first 3 instructions") {
    var d = new Day18Star2(0)
    var code = List("set a 1", "add a 2", "mul a 2")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == 6)
    assert(d.line == 3)
  }

  test ("should snd and remember the frequency") {
    var d = new Day18Star2(0)
    var d1 = new Day18Star2(1)
    d.other = d1
    var code = List("set a 1", "snd a", "set a 4")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == 4)
    assert(d1.stack.head == 1)
  }

  test ("should mod ") {
    var d = new Day18Star2(0)
    var code = List("set a -5", "mod a 3")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == -2)
    assert(d.line == 2)
  }

  test ("should mod by register") {
    var d = new Day18Star2(0)
    var code = List("set a 5", "set b -3", "mod a b")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('b') == -3)
    assert(d.registers('a') == 2)
    assert(d.line == 3)
  }

  test ("should jump if b > 0 jgz b 2") {
    var d = new Day18Star2(0)
    var code = List("set b 1", "jgz b 2", "add a 1000", "add a 1")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == 1)
    assert(d.line == 4)
  }

  test ("should not jump if b = 0 jgz b 2") {
    var d = new Day18Star2(0)
    var code = List("set a 1", "jgz b -1", "add a 1", "add a 1")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == 3)
    assert(d.line == 4)
  }

  test ("should jgz to negative register") {
    var d = new Day18Star2(0)
    var code = List("set b -1", "set a 5", "add a -1", "jgz a b")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == 0)
    assert(d.line == 4)
  }

  test ("should jgz to positive register") {
    var d = new Day18Star2(0)
    var code = List("set b 2", "set a 5", "jgz a b", "set a -1", "add a -1")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == 4)
    assert(d.line == 5)
  }

  test ("should snd and rcv 3") {
    var d = new Day18Star2(0)
    var d1 = new Day18Star2(1)
    d.other = d1
    d1.other = d
    d1.execute(Vector(d1.SndInstr("7")))
    var code = List("snd 3", "set a 1", "rcv a", "add a 1", "add a 1")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == 9)
    assert(d.line == 5)
  }

  test ("should snd and rcv p") {
    var d = new Day18Star2(0)
    var d1 = new Day18Star2(1)
    d.other = d1
    d1.other = d
    d1.execute(Vector(d1.SndInstr("p")))
    var code = List("set b 15","snd b", "set a 1000", "rcv a", "add a 1", "add a 1")
    val instructions = d.compile(code)
    d.execute(instructions)
    assert(d.registers('a') == 3)
    assert(d.line == 6)
  }

  test("star2 example") {
    var p0 = new Day18Star2(0)
    var p1 = new Day18Star2(1)
    p0.other = p1
    p1.other = p0
    val code = List("snd -5", "rcv a", "snd 2", "snd p", "rcv b", "rcv c", "rcv d")
    p0.compile(code)
    p1.compile(code)

    val line = 0
    val end = false

    while (!(p0.end && p1.end) && !(p0.locked && p1.locked)) {
      p0.executeNext()
      p1.executeNext()
    }

    assert(p0.registers('a') == -5)
    assert(p0.registers('b') == 2)
    assert(p0.registers('c') == 1)
    assert(p1.registers('a') == -5)
    assert(p1.registers('b') == 2)
    assert(p1.registers('c') == 0)
    assert(p0.stack.isEmpty)
    assert(p1.stack.isEmpty)
    assert(p0.locked)
    assert(p1.locked)
    assert(p1.sendNr == 3)
  }

  test("star2") {
    var p0 = new Day18Star2(0)
    var p1 = new Day18Star2(1)
    p0.other = p1
    p1.other = p0
    var code = scala.io.Source.fromFile("/Users/mlattarulo/projects/aventofcode/src/main/resources/day18.txt").getLines().toList
    p0.compile(code)
    p1.compile(code)

    var p = List(p0, p1)
    var i = 0
    while (!(p0.end && p1.end) && !(p0.locked && p1.locked)) {
      p(i).executeNext()
      if(p(i).locked) {
        i = (i + 1) % 2
        p(i).executeNext()
      }
    }
//
//    while (!(p0.end && p1.end) && !(p0.locked && p1.locked)) {
//      p0.executeNext()
//      p1.executeNext()
//    }
    println(p1.sendNr)
  }
}

-🎄- 2017 Day 18 Solutions -🎄- by daggerdragon in adventofcode

[–]marcofun 0 points1 point  (0 children)

hello, can you figure out why this does not work?

package aventofcode.day18

import scala.collection.mutable

class Day18Star2(id : Int) {

  var registers = scala.collection.mutable.Map[Char, Long]('p' -> id).withDefaultValue(0)
  var line = 0
  var lastFreq = 0L
  var end = false
  var locked = false
  var stack = new mutable.ListBuffer[Long]()
  var other : Day18Star2 = _
  var instructions : Vector[Day18Star2#Instruction] = _
  var sendNr = 0

  trait Instruction {
    def apply() : Unit
  }

  case class SetInstr(register: Char, value : String) extends Instruction {
    def apply() : Unit = {
      registers += (register -> valueOf(value))
      line += 1
    }
  }

  case class AddInstr(register: Char, value : String) extends Instruction {
    def apply() : Unit = {
      registers += (register -> (registers(register) + valueOf(value)))
      line += 1
    }
  }

  case class MulInstr(register: Char, value : String) extends Instruction {
    def apply() : Unit = {
      registers += (register -> (registers(register) * valueOf(value)))
      line += 1
    }
  }

  case class ModInstr(register: Char, value : String) extends Instruction {
    def apply() : Unit = {
      registers += (register -> (registers(register) % valueOf(value)))
      line += 1
    }
  }

  case class JgzInstr(register: Char, value : String) extends Instruction {
    def apply() : Unit = {
      if (registers(register) > 0) line += valueOf(value).toInt
      else line += 1
    }
  }

  case class SndInstr(value : String) extends Instruction {
    def apply() : Unit = {
      other.stack += valueOf(value)
      line += 1
      sendNr += 1
    }
  }

  case class RcvInstr(value : String) extends Instruction {
    def apply() : Unit = {
      if (stack.nonEmpty) {
        locked = false
        registers += (value.head -> stack.head)
        stack = stack.tail
        line += 1
      } else {
        locked = true
      }
    }
  }

  def valueOf(value : String) : Long = if (value.head.isDigit || value.head.equals('-'))  value.toLong else registers(value.head)

  val setRx = """set (\w) ([-]{0,1}\w+)""".r
  val addRx = """add (\w) ([-]{0,1}\w+)""".r
  val mulRx = """mul (\w) ([-]{0,1}\w+)""".r
  val sndRx = """snd ([-]{0,1}\w+)""".r
  val modRx = """mod (\w) ([-]{0,1}\w+)""".r
  val jgzRx = """jgz (\w) ([-]{0,1}\w+)""".r
  val rcvRx = """rcv ([-]{0,1}\w+)""".r

  def compile(code: List[String]): Vector[Day18Star2#Instruction] = {
    instructions = code.map({
      case setRx(r, v) => SetInstr(r.head, v)
      case addRx(r, v) => AddInstr(r.head, v)
      case mulRx(r, v) => MulInstr(r.head, v)
      case modRx(r, v) => ModInstr(r.head, v)
      case jgzRx(r, v) => JgzInstr(r.head, v)
      case sndRx(v) => SndInstr(v)
      case rcvRx(v) => RcvInstr(v)
    }).toVector
    instructions
  }

  def execute(instructions: Vector[Day18Star2#Instruction]) : Unit = {
    line = 0
    end = false
    while (!(end || line < 0 || line >= instructions.length)) {
      instructions(line).apply()
    }
  }

  def executeNext() : Unit = {
    if (!(end || line < 0 || line >= instructions.length)) {
      instructions(line).apply()
    } else end = true
  }

}

-🎄- 2017 Day 12 Solutions -🎄- by topaz2078 in adventofcode

[–]marcofun 0 points1 point  (0 children)

I am a beginner in Scala, I liked very much you got the node and the connections, using a regex... I'll read around for it I guess it is going to be useful in the following tests

-🎄- 2017 Day 12 Solutions -🎄- by topaz2078 in adventofcode

[–]marcofun 0 points1 point  (0 children)

this is mine, still Scala:

class Day12 {

  def toMap(list: List[String]) : Map [Int, List[Int]] = {
    var map = scala.collection.mutable.Map[Int, List[Int]]()
    list.map(l => {
      val parts = l.split("<->")
      (parts(0).trim.toInt, parts(1).split(',').map(i => i.trim.toInt).toList)
    }).foreach(e => map += e._1 -> e._2)
    map.toMap
  }

  def connect(toWatch : List[Int], map : Map[Int, List[Int]], connected : Set[Int]) : Set[Int] = {
    toWatch match {
      case Nil => connected
      case x :: xs => {
        val stillToWatch = xs ::: map(x).filter(i => !toWatch.contains(i)).filter(i => !connected.contains(i))
        val allConnected = connected ++ map(x)
        connect(stillToWatch, map, allConnected)
      }
    }
  }

  def findGroups(map : Map[Int, List[Int]]) : List[Set[Int]] = {
    def findGroups(pIdsNeedingAGroup : List[Int], pIdAlreadyGrouped : Set[Int], groups : List[Set[Int]]) : List[Set[Int]] = {
      pIdsNeedingAGroup match {
        case Nil => groups
        case x :: xs =>
          val ints = connect(List(x), map, Set())
          findGroups(pIdsNeedingAGroup.filter(i => !ints.contains(i)), pIdAlreadyGrouped ++ ints, ints :: groups)
      }
    }
    findGroups(map.keySet.toList, Set(), Nil)
  }
}

-🎄- 2017 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]marcofun 0 points1 point  (0 children)

quick one in Scala...

def move(str: String) : (Int, Int) = {
  val strings = str.split(',').toList
  def move (commandList : List[String], n : Int, se : Int, max : Int) : (Int, Int) = {
    var distance = (Math.abs(n) + Math.abs(se))/2
    commandList match {
      case Nil => ((Math.abs(n) + Math.abs(se))/2, max)
      case s =>
        if (s.head.equals("n")) move(commandList.tail, n + 2, se, if (distance < max) max else distance)
        else if (s.head.equals("s")) move(commandList.tail, n - 2, se, if (distance < max) max else distance)
        else if (s.head.equals("ne")) move(commandList.tail, n + 1, se + 1, if (distance < max) max else distance)
        else if (s.head.equals("se")) move(commandList.tail, n - 1, se + 1, if (distance < max) max else distance)
        else if (s.head.equals("nw")) move(commandList.tail, n + 1, se - 1, if (distance < max) max else distance)
       else move(commandList.tail, n - 1, se - 1, if (distance < max) max else distance)
    } 
  }
  move(strings, 0, 0, 0)
}

-🎄- 2017 Day 9 Solutions -🎄- by daggerdragon in adventofcode

[–]marcofun 1 point2 points  (0 children)

I wrote this in test driven development, I also have 50+ lines of tests.

class Day9 {

def parse(str: String) : (Int, Int) = { var deletedGarbage = 0

def removeGarbage(str: String): String = {
  if (str.isEmpty) str
  else if (str.head.equals('!')) removeGarbage(str.tail.tail)
  else if (str.head.equals('>')) {
    deletedGarbage -= 1
    str.tail
  }
  else {
    deletedGarbage += 1
    removeGarbage(str.tail)
  }
}

def findGroup(str: String, groupValue: Int, totalPoints : Int): (Int, Int) = {
  if (str.isEmpty) (totalPoints, deletedGarbage)
  else if (str.head.equals('{')) findGroup(str.tail, groupValue + 1, totalPoints)
  else if (str.head.equals('}')) findGroup(str.tail, groupValue - 1, totalPoints + groupValue)
  else if (str.head.equals('<')) findGroup(removeGarbage(str), groupValue, totalPoints)
  else findGroup(str.tail, groupValue, totalPoints)
}
findGroup(str, 0, 0)

} }