Advertisement
fatboychummy

AdventOfCodeYeet2

Dec 1st, 2018
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.32 KB | None | 0 0
  1. local loc = "/advent/"
  2. local inp = loc .. "INPUT.txt"
  3. local doti = 0
  4. local data = {}
  5. local points = {}
  6. local view = {}
  7. local best = {min = 100}
  8. do
  9.   local h = fs.open(inp,"r")
  10.   local line = h.readLine()
  11.   local i = 0
  12.   repeat
  13.     i = i + 1
  14.     data[i] = line
  15.     line = h.readLine()
  16.     --if line == "" then line = false end
  17.   until not line
  18.     h.close()
  19. end
  20.  
  21. local function dots()
  22.   while true do
  23.     os.sleep(1)
  24.     io.write(".")
  25.     doti = doti + 1
  26.   end
  27. end
  28.  
  29.  
  30. local function createData()
  31.   for i = 1,#data do
  32.     --position=<-41933,  10711> velocity=< 4, -1>
  33.     local a = data[i]:gmatch("-?%d+")
  34.     local x = a()
  35.     local y = a()
  36.     local vX = a()
  37.     local vY = a()
  38.     if x ~= nil then
  39.       points[i] = {["x"] = x,["y"] = y, ["vX"] = vX, ["vY"] = vY}
  40.     else
  41.       print("nil value on",i)
  42.     end
  43.   end
  44. end
  45.  
  46. local function step()
  47.   for i = 1,#points do
  48.     local cur = points[i]
  49.     cur.x = cur.x + cur.vX
  50.     cur.y = cur.y + cur.vY
  51.   end
  52. end
  53.  
  54. local function stepBack()
  55.   for i = 1,#points do
  56.     local cur = points[i]
  57.     cur.x = cur.x - cur.vX
  58.     cur.y = cur.y - cur.vY
  59.   end
  60. end
  61.  
  62.  
  63.  
  64. local function stepMultiple()
  65.   for i = 1,10000 do
  66.     step()
  67.     if i%100 == 0 then
  68.       doti = doti + 1
  69.       io.write("!")
  70.       os.sleep()
  71.     end
  72.   end
  73. end
  74.  
  75. local function stepWatch()
  76.   local minX = 10000
  77.   local minY = 10000
  78.   local minXI = 10701
  79.   local minYI = 10701
  80.   for o = 1,100 do
  81.     step()
  82.     local minX1 = 10000
  83.     local maxX1 = -10000
  84.     local minY1 = 10000
  85.     local maxY1 = -10000
  86.     for i = 1,#points do
  87.       local cur = points[i]
  88.       if minX1 > cur.x then
  89.         minX = cur.x
  90.       end
  91.       if maxX1 < cur.x then
  92.         maxX1 = cur.x
  93.       end
  94.       if minY1 > cur.y then
  95.         minY = cur.y
  96.       end
  97.       if maxY1 < cur.y then
  98.         maxY1 = cur.y
  99.       end
  100.  
  101.     end
  102.     minX1 = minX1 + 10000
  103.     maxX1 = maxX1 + 10000
  104.     minY1 = minY1 + 10000
  105.     maxY1 = maxY1 + 10000
  106.     local dX = maxX1 - minX1
  107.     local dY = maxY1 - minY1
  108.     if minX > dX then
  109.       minX = dX
  110.       minXI = o
  111.     end
  112.     if minY > dY then
  113.       minY = dY
  114.       minYI = o
  115.     end
  116.     if o % 100 == 0 then os.sleep() end
  117.   end
  118.   print()
  119.   print("MINX:",minX)
  120.   print("MINY:",minY)
  121.   print("MINXI:",minXI)
  122.   print("MINYI:",minYI)
  123.   return minX,minY,minXI,minYI
  124. end
  125.  
  126. local function makeView()
  127.   view = {}
  128.   print("Adding X's")
  129.   for i = 1,#points do
  130.     local cur = points[i]
  131.     if view[cur.y] then
  132.       view[cur.y][cur.x] = "X"
  133.     else
  134.       view[cur.y] = {[cur.x] = "X"}
  135.     end
  136.   end
  137.   os.sleep()
  138.   print("Adding O's")
  139.   for i = -6000,6000 do
  140.     for o = -6000,6000 do
  141.       if view[i] then
  142.         if not view[i][o] then
  143.           view[i][o] = "O"
  144.         end
  145.       else
  146.         view[i] = {[o] = "O"}
  147.       end
  148.     end
  149.     if i % 100 == 0 then
  150.       print("+100",i)
  151.       os.sleep()
  152.     end
  153.   end
  154.   os.sleep()
  155.   print("Done.")
  156. end
  157.  
  158. local function printFrom(x,y)
  159.   local mX,mY = term.getSize()
  160.   for i = y,mY+y do
  161.     for o = x,mX+x do
  162.       term.write(view[i][o])
  163.     end
  164.     print()
  165.   end
  166. end
  167.  
  168. print("Building data.")
  169. parallel.waitForAny(dots,createData)
  170. print()
  171. doti = 0
  172. print("Completed.")
  173. --[[print("Dumping to file. Yeet.")
  174. local h = fs.open(loc .. "OUTPUT","w")
  175. h.write(textutils.serialize(points))
  176. h.close()]]
  177.  
  178. print("Stepping 10000 times.")
  179. parallel.waitForAny(dots,stepMultiple)
  180. print()
  181. doti = 0
  182. print("Completed.")
  183. local count = 0
  184. while true do
  185.   print("Stepping 100 times and finding the smallest size.")
  186.   local x,y,xI,yI = stepWatch()
  187.   if xI ~= 100 and yI ~= 100 then
  188.     best = {x=x,y=y,xI=xI,yI=yI}
  189.     break
  190.   end
  191.   count = count + 100
  192. end
  193. print("Steps before found:",10000+count)
  194. print("Found lowest.")
  195. print(textutils.serialize(best))
  196.  
  197. for i = 1,82 do
  198.   stepBack()
  199. end
  200.  
  201. print("At 10518")
  202. print("Creating View.")
  203.  
  204. makeView()
  205. for i = 5,1,-1 do
  206.   print("Displaying preview in",i,"second(s).")
  207.   os.sleep(1)
  208. end
  209.   local coords = {x=0,y=0}
  210. while true do
  211.  
  212.   printFrom(coords.x,coords.y)
  213.   local ev = {os.pullEvent("key")}
  214.   if ev[2] == keys.up then
  215.     coords.y = coords.y + 1
  216.   elseif ev[2] == keys.down then
  217.     coords.y = coords.y - 1
  218.   elseif ev[2] == keys.left then
  219.     coords.x = coords.x - 1
  220.   elseif ev[2] == keys.right then
  221.     coords.x = coords.x + 1
  222.   end
  223. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement