Make split into a prototype function on string
This commit is contained in:
parent
a7fd5b212a
commit
5e96c6a965
@ -27,9 +27,10 @@ function Stack:reverse()
|
|||||||
return newStack
|
return newStack
|
||||||
end
|
end
|
||||||
|
|
||||||
function Split(s, delimiter)
|
function string:split(delimiter)
|
||||||
|
delimiter = delimiter or " "
|
||||||
result = {};
|
result = {};
|
||||||
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
|
for match in (self..delimiter):gmatch("(.-)"..delimiter) do
|
||||||
table.insert(result, match);
|
table.insert(result, match);
|
||||||
end
|
end
|
||||||
return result;
|
return result;
|
||||||
@ -71,9 +72,9 @@ end
|
|||||||
inputLines = file:read("all")
|
inputLines = file:read("all")
|
||||||
file:close()
|
file:close()
|
||||||
|
|
||||||
parts = Split(inputLines, "\n\n")
|
parts = inputLines:split("\n\n")
|
||||||
initialState = Split(parts[1], "\n")
|
initialState = parts[1]:split("\n")
|
||||||
moves = Split(parts[2], "\n")
|
moves = parts[2]:split("\n")
|
||||||
|
|
||||||
-- Initialize stacks
|
-- Initialize stacks
|
||||||
stackCount = (#initialState[1] + 1) // 4
|
stackCount = (#initialState[1] + 1) // 4
|
||||||
@ -102,13 +103,14 @@ print("Initial state:")
|
|||||||
for i = 1, stackCount do
|
for i = 1, stackCount do
|
||||||
print("Stack "..i..": "..table.concat(stacks[i], " "))
|
print("Stack "..i..": "..table.concat(stacks[i], " "))
|
||||||
end
|
end
|
||||||
|
print("")
|
||||||
|
|
||||||
-- Perform moves
|
-- Perform moves
|
||||||
|
|
||||||
-- Part 1
|
-- Part 1
|
||||||
|
|
||||||
for i = 1, #moves do
|
for i = 1, #moves do
|
||||||
move = Split(moves[i], " ")
|
move = moves[i]:split()
|
||||||
count = tonumber(move[2])
|
count = tonumber(move[2])
|
||||||
from = tonumber(move[4])
|
from = tonumber(move[4])
|
||||||
to = tonumber(move[6])
|
to = tonumber(move[6])
|
||||||
@ -132,7 +134,7 @@ print("Part 1: "..part1)
|
|||||||
|
|
||||||
-- Part 2
|
-- Part 2
|
||||||
for i = 1, #moves do
|
for i = 1, #moves do
|
||||||
move = Split(moves[i], " ")
|
move = moves[i]:split()
|
||||||
count = tonumber(move[2])
|
count = tonumber(move[2])
|
||||||
from = tonumber(move[4])
|
from = tonumber(move[4])
|
||||||
to = tonumber(move[6])
|
to = tonumber(move[6])
|
||||||
|
Loading…
Reference in New Issue
Block a user