1. val C = io.Source.fromFile(today + "input").getLines.toList
  2. def parseMask(m: String) = m.zipWithIndex.filter(_._1 != 'X').map(x => (x._1 - '0', x._2)).map(_.swap).map(x => (35L - x._1.toLong, x._2.toLong)).toMap
  3. def flipBitAtIndex(cur: Long, mIndexBit: (Long, Long)): Long = {
  4. val (mIdx, bitFlip) = mIndexBit
  5. bitFlip match {
  6. case 1L cur | (1L << mIdx)
  7. case 0L cur & ~(1L << mIdx)
  8. }
  9. }
  10. var mask = Map[Long, Long]()
  11. var memory = Map[Long, Long]()
  12. C.foreach { line =>
  13. val command = line.split(" = ")
  14. if (command(0) == "mask") {
  15. mask = parseMask(command(1))
  16. } else {
  17. val index = command(0).substring(4, command(0).length - 1).toLong
  18. val value = command(1).toLong
  19. val maskedValue = mask.foldLeft(value)(flipBitAtIndex)
  20. memory = memory + (index -> maskedValue)
  21. }
  22. }
  23. println(memory.values.sum)

14 1