Advertisement
Guest User

Untitled

a guest
Dec 14th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. fun main() {
  2. var input = getInputLines(2020, 14)
  3. ;{
  4. val mem = mutableMapOf<Long, Long>()
  5. var (maskAnd, maskOr) = listOf(0L, 0L)
  6. for(s in input) {
  7. if (s.take(7) == "mask = ") {
  8. val mask = s.drop(7)
  9. maskAnd = mask.replace("X", "1").toLong(2)
  10. maskOr = mask.replace("X", "0").toLong(2)
  11. }
  12. if (s.take(4) == "mem[") {
  13. var (loc, value) = "\\d+".toRegex().findAll(s).map { it.value.toLong() }.toList()
  14. mem[loc] = (value and maskAnd) or maskOr
  15. }
  16. }
  17. println(mem.values.sum())
  18. }()
  19.  
  20. ;{
  21. val mem = mutableMapOf<Long, Long>()
  22. var (maskOr) = listOf(0L, 0L)
  23. var mask = ""
  24. for(s in input) {
  25. if (s.take(7) == "mask = ") {
  26. mask = s.drop(7)
  27. maskOr = mask.replace("X", "0").toLong(2)
  28. }
  29. if (s.take(4) == "mem[") {
  30. var (loc, value) = "\\d+".toRegex().findAll(s).map { it.value.toLong() }.toList()
  31. val floatingBits = mask.mapIndexed { index, c -> index to c }.filter { it.second == 'X' }
  32. loc = loc or maskOr
  33. for(i in (0 until (1 shl floatingBits.size))) {
  34. val bits = i.toString(2).padStart(floatingBits.size, '0')
  35. val locArray = loc.toString(2).padStart(36, '0').toCharArray()
  36. bits.forEachIndexed { index, c -> locArray[floatingBits[index].first] = bits[index] }
  37. val address = locArray.joinToString("").toLong(2)
  38. mem[address] = value
  39. }
  40. }
  41. }
  42. println(mem.values.sum())
  43. }()
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement