59 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			Lua
		
	
	
		
			Executable file
		
	
	
	
	
			
		
		
	
	
			59 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			Lua
		
	
	
		
			Executable file
		
	
	
	
	
| #!/usr/bin/env luajit
 | |
| require("utils")
 | |
| require("set")
 | |
| 
 | |
| local file = io.open(arg[3])
 | |
| local coords = {}
 | |
| local maxr, maxc = 0, 0
 | |
| local line = file:read("*l")
 | |
| while line ~= "" do
 | |
| 	local c, r = unpack(split(line, ","))
 | |
| 	r, c = tonumber(r), tonumber(c)
 | |
| 	if r > maxr then maxr = r end
 | |
| 	if c > maxc then maxc = c end
 | |
| 	table.insert(coords, { r, c })
 | |
| 	line = file:read("*l")
 | |
| end
 | |
| 
 | |
| local width, height = maxc + 1, maxr + 1
 | |
| foreach(coords, function(p) return p[1] * width + p[2] end)
 | |
| coords = set(coords)
 | |
| 
 | |
| local folds = {}
 | |
| line = file:read("*l")
 | |
| while line ~= nil do
 | |
| 	local dir, v = unpack(split(line, "="))
 | |
| 	table.insert(folds, { dir == "fold along y", tonumber(v) })
 | |
| 	line = file:read("*l")
 | |
| end
 | |
| 
 | |
| 
 | |
| for i, fold in pairs(folds) do
 | |
| 	local dir, v = unpack(fold)
 | |
| 	local ncoords = set()
 | |
| 	for p in coords:iter() do
 | |
| 		local r, c = math.floor(p / width), p % width
 | |
| 		if dir and r > v then
 | |
| 			r = 2 * v - r
 | |
| 		elseif not dir and c > v then
 | |
| 			c = 2 * v - c
 | |
| 		end
 | |
| 		ncoords:add(r * width + c)
 | |
| 	end
 | |
| 	coords = ncoords
 | |
| 	break
 | |
| end
 | |
| 
 | |
| for r = 0, height - 1 do
 | |
| 	for c = 0, width - 1 do
 | |
| 		if coords:has(r * width + c) then
 | |
| 			io.write("#")
 | |
| 		else
 | |
| 			io.write(".")
 | |
| 		end
 | |
| 	end
 | |
| 	io.write("\n")
 | |
| end
 | |
| io.write("\n")
 | |
| 
 | |
| print(coords.size)
 | 
