Guest User

Untitled

a guest
Dec 14th, 2020
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SetWorkingDir %A_ScriptDir%
  2.  
  3. FileRead, instStr, % A_ScriptDir "\d14.txt"
  4.  
  5. instA := StrSplit(instStr, "mask = ")
  6. instA.RemoveAt(1)
  7. for i, j in instA {
  8.     instA[i] := StrSplit(j, "`r`n")
  9.     for k, l in instA[i] {
  10.         if (SubStr(l, 1, 3) = "mem") and (l != "") {
  11.             ta := StrSplit(l, "] = ")
  12.             instA[i][k] := [SubStr(ta[1], 5), ta[2]]
  13.         }
  14.     }
  15.     instA[i].Pop()
  16. }
  17.  
  18. memory := {}
  19.  
  20. for i, j in instA {
  21.     for k, l in j {
  22.         if (k = 1)
  23.             Continue
  24.         binAddress := Bin(l[1])
  25.         outStr := ApplyMask(j[1], binAddress)
  26.         AddressA := process([outStr])
  27.         for m, n in AddressA {
  28.             memory[Dec(n)] := l[2]
  29.         }
  30.     }
  31. }
  32.  
  33. sum := 0
  34. for i, j in memory {
  35.     sum += j
  36. }
  37. Clipboard := sum
  38.  
  39. process(a) {
  40.     noX := 0
  41.     b := []
  42.     for i, j in a {
  43.         if !InStr(j, "X") {
  44.             noX += 1
  45.         }
  46.         if (noX = a.MaxIndex())
  47.             Return a
  48.         arr := StrSplit(j, "X",, 2)
  49.         b.Push(arr[1] 0 arr[2])
  50.         b.Push(arr[1] 1 arr[2])
  51.     }
  52.     Return process(b)
  53. }
  54.  
  55. ApplyMask(mask, bin) {
  56.     maskA := StrSplit(mask)
  57.  
  58.     Loop, % maskA.MaxIndex() - StrLen(bin)
  59.     {
  60.         bin := "0" bin
  61.     }
  62.     valA := StrSplit(bin)
  63.     for m, n in valA {
  64.         if (maskA[m] = 1)
  65.             valA[m] := 1
  66.         else if (maskA[m] = "X") {
  67.             valA[m] := "X"
  68.         }
  69.     }
  70.     outStr := ""
  71.     for m, n in valA {
  72.         outStr .= n
  73.     }
  74.     Return outStr
  75. }
  76.  
  77. Bin(x){
  78.     return (x>>1 ? Bin(x>>1):"")x&1
  79. }
  80. Dec(x){
  81.     return (StrLen(x)>1 ? Dec(SubStr(x,1,-1))<<1:0)|SubStr(x,0)
  82. }
Add Comment
Please, Sign In to add comment