Advertisement
Guest User

Untitled

a guest
Dec 14th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. List := StrSplit(FileOpen("day14_input.txt","R").Read(),"`r`n")
  2.  
  3. memory := []
  4. for k, v in List {
  5.     if InStr(v,"mask")
  6.         mask := StrSplit(SubStr(v,8))
  7.     else {
  8.         s := ""
  9.         RegexMatch(v,"O)mem\[(\d+)\] = (\d+)$",o)
  10.         for location, bit in StrSplit(Format("{:036}",Bin(o[1]))) {
  11.             t := mask[location]
  12.             s .= (t == "X")?"X":(bit || t)
  13.         }
  14.         ;memory[o[1]] := s
  15.         GetAddresses(L := [],s)
  16.         for a, b in L
  17.             memory[Dec(b)] := o[2]
  18.     }
  19. }
  20.  
  21. sum := 0
  22. for k, v in memory
  23.     sum += v
  24.  
  25. msgbox % clipboard := sum
  26.  
  27. GetAddresses(List, address) {
  28.   if !InStr(address,"X")
  29.     List.push(address)
  30.   else {
  31.     GetAddresses(List,StrReplace(address, "X", 1,, 1))
  32.     GetAddresses(List,StrReplace(address, "X", 0,, 1))
  33.   }
  34. }
  35.  
  36. Bin(x) {
  37.     while x
  38.         r:=1&x r,x>>=1
  39.     return r
  40. }
  41.  
  42. Dec(x) {
  43.     b:=StrLen(x),r:=0
  44.     loop,parse,x
  45.         r|=A_LoopField<<--b
  46.     return r
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement