Advertisement
Guest User

Untitled

a guest
Dec 25th, 2021
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.07 KB | None | 0 0
  1. local grid = {} -- grid of the cucumbers
  2. for line in io.lines("25.txt") do
  3.     local row = {}
  4.     for char in line:gmatch(".") do
  5.         table.insert(row, char)
  6.     end
  7.     table.insert(grid, row)
  8. end
  9.  
  10. local gridCopy = {} -- copy of the grid to store the movements to be done
  11. for i = 1, #grid do
  12.     local rowCopy = {}
  13.     for j = 1, #grid[1] do
  14.         table.insert(rowCopy, ".")
  15.     end
  16.     table.insert(gridCopy, rowCopy)
  17. end
  18.  
  19. local canMove = {} -- grid of booleans to see if we can move
  20. for i = 1, #grid do
  21.     local boolRow = {}
  22.     for j = 1, #grid[1] do
  23.         table.insert(boolRow, false)
  24.     end
  25.     table.insert(canMove, boolRow)
  26. end
  27.  
  28. for n = 1, math.huge do
  29.     local movCount = 0 -- count of movements done on a step
  30.     for y = 1, #grid do -- check for ">" movements
  31.         for x = 1, #grid[1] do
  32.             if grid[y][x] == ">" and grid[y][x+1] then
  33.                 if grid[y][x+1] == "." then
  34.                     canMove[y][x] = true
  35.                     gridCopy[y][x+1] = ">"
  36.                     movCount = movCount + 1
  37.                 end
  38.             elseif grid[y][x] == ">" and grid[y][1] == "." then
  39.                 canMove[y][x] = true
  40.                 gridCopy[y][1] = ">"
  41.                 movCount = movCount + 1
  42.             end
  43.         end
  44.     end
  45.     for y = 1, #grid do -- move them
  46.         for x = 1, #grid[1] do
  47.             if canMove[y][x] then
  48.                 grid[y][x] = "."
  49.             end
  50.             if gridCopy[y][x] == ">" then
  51.                 grid[y][x] = ">"
  52.             end
  53.         end
  54.     end
  55.     for y = 1, #gridCopy do -- clear both other tables
  56.         for x = 1, #gridCopy[1] do
  57.             gridCopy[y][x] = "."
  58.         end
  59.     end
  60.     for y = 1, #canMove do
  61.         for x = 1, #canMove[1] do
  62.             canMove[y][x] = false
  63.         end
  64.     end
  65.     for y = 1, #grid do -- check for "v" movements
  66.         for x = 1, #grid[1] do
  67.             if grid[y][x] == "v" and grid[y+1] then
  68.                 if grid[y+1][x] == "." then
  69.                     canMove[y][x] = true
  70.                     gridCopy[y+1][x] = "v"
  71.                     movCount = movCount + 1
  72.                 end
  73.             elseif grid[y][x] == "v" and grid[1][x] == "." then
  74.                 canMove[y][x] = true
  75.                 gridCopy[1][x] = "v"
  76.                 movCount = movCount + 1
  77.             end
  78.         end
  79.     end
  80.     for y = 1, #grid do -- move them
  81.         for x = 1, #grid[1] do
  82.             if canMove[y][x] then
  83.                 grid[y][x] = "."
  84.             end
  85.             if gridCopy[y][x] == "v" then
  86.                 grid[y][x] = "v"
  87.             end
  88.         end
  89.     end
  90.     if movCount == 0 then -- if nothing has moved, break and print the solution
  91.         print("They can't move after " .. n .. " steps")
  92.         break
  93.     end
  94.     for y = 1, #gridCopy do -- clear both other tables for next iteration
  95.         for x = 1, #gridCopy[1] do
  96.             gridCopy[y][x] = "."
  97.         end
  98.     end
  99.     for y = 1, #canMove do
  100.         for x = 1, #canMove[1] do
  101.             canMove[y][x] = false
  102.         end
  103.     end
  104. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement