Refactor day 2 awk

This commit is contained in:
kageru 2019-12-02 11:59:35 +01:00
parent 2dd93d776e
commit 87551c682f

View File

@ -1,15 +1,19 @@
{
len = split($1, arr, ",");
# There are no scopes, so we just write to `arr` here and use it later.
# Returning the array does not seem to be legal either. Only scalars allowed.
function prepareInput(raw) {
len = split(raw, arr, ",");
for (i=0; i<=len; i++) {
arr[i-1] = arr[i];
arr[i-1] = int(arr[i]);
}
arr[1] = 12;
arr[2] = 2;
return len
}
function process(arr, len) {
for (i=0; i<len; i+=4) {
cmd = int(arr[i]);
e1 = int(arr[i+1]);
e2 = int(arr[i+2]);
target = int(arr[i+3]);
cmd = arr[i];
e1 = arr[i+1];
e2 = arr[i+2];
target = arr[i+3];
if (cmd == 1)
arr[target] = arr[e1] + arr[e2];
else if (cmd == 2)
@ -19,5 +23,12 @@
else
print "Error, invalid command";
}
}
{
len = prepareInput($1);
arr[1] = 12;
arr[2] = 2;
process(arr, len)
print arr[0]
}