diff options
Diffstat (limited to '11')
| -rw-r--r-- | 11/1.lua | 49 | ||||
| -rw-r--r-- | 11/2.lua | 54 | ||||
| -rwxr-xr-x | 11/ext.lua | 62 | ||||
| -rwxr-xr-x | 11/input.txt | 55 | ||||
| -rw-r--r-- | 11/sample.txt | 27 |
5 files changed, 247 insertions, 0 deletions
diff --git a/11/1.lua b/11/1.lua new file mode 100644 index 0000000..7204e49 --- /dev/null +++ b/11/1.lua @@ -0,0 +1,49 @@ +local monkeys = {} + +for line in io.lines() do + if line:match("^Monkey") then + local id = line:match("Monkey (%d+)") + table.insert(monkeys,{items={},inspected= 0, id=id}) + elseif line:match("Starting items:") then + for item in line:gmatch("(%d+)") do + table.insert(monkeys[#monkeys].items,tonumber(item)) + end + elseif line:match("Operation:") then + op, val = line:match("new = old (.) (%S+)") + monkeys[#monkeys].op = op + monkeys[#monkeys].val = val + elseif line:match("Test:") then + monkeys[#monkeys].test = tonumber(line:match("Test: divisible by (%d+)")) + elseif line:match("If true:") then + monkeys[#monkeys].t = tonumber(line:match("throw to monkey (%d+)")) + 1 + elseif line:match("If false:") then + monkeys[#monkeys].f = tonumber(line:match("throw to monkey (%d+)")) + 1 + end +end + +for round = 1,20 do + for turn, monkey in ipairs(monkeys) do + monkey.inspected = monkey.inspected + #monkey.items + for _,item in ipairs(monkey.items) do + local worry = item + if monkey.op == "+" and monkey.val == "old" then + worry = worry * 2 + elseif monkey.op == "*" and monkey.val == "old" then + worry = worry * worry + elseif monkey.op == "+" and tonumber(monkey.val) then + worry = worry + tonumber(monkey.val) + elseif monkey.op == "*" and tonumber(monkey.val) then + worry = worry * tonumber(monkey.val) + end + local newworry = math.floor(worry / 3) + if newworry % monkey.test == 0 then + table.insert(monkeys[monkey.t].items,newworry) + else + table.insert(monkeys[monkey.f].items,newworry) + end + end + monkey.items = {} + end +end +table.sort(monkeys, function(a,b) return a.inspected > b.inspected end) +print(monkeys[1].inspected * monkeys[2].inspected) diff --git a/11/2.lua b/11/2.lua new file mode 100644 index 0000000..e26ddd2 --- /dev/null +++ b/11/2.lua @@ -0,0 +1,54 @@ +local monkeys = {} + +for line in io.lines() do + if line:match("^Monkey") then + local id = line:match("Monkey (%d+)") + table.insert(monkeys,{items={},inspected= 0, id=id}) + elseif line:match("Starting items:") then + for item in line:gmatch("(%d+)") do + table.insert(monkeys[#monkeys].items,tonumber(item)) + end + elseif line:match("Operation:") then + op, val = line:match("new = old (.) (%S+)") + monkeys[#monkeys].op = op + monkeys[#monkeys].val = val + elseif line:match("Test:") then + monkeys[#monkeys].test = tonumber(line:match("Test: divisible by (%d+)")) + elseif line:match("If true:") then + monkeys[#monkeys].t = tonumber(line:match("throw to monkey (%d+)")) + 1 + elseif line:match("If false:") then + monkeys[#monkeys].f = tonumber(line:match("throw to monkey (%d+)")) + 1 + end +end +local divisor = 1 +for _, monkey in pairs(monkeys) do + divisor = divisor * monkey.test +end + + +for round = 1,10000 do + for turn, monkey in ipairs(monkeys) do + monkey.inspected = monkey.inspected + #monkey.items + for _,item in ipairs(monkey.items) do + local worry = item + if monkey.op == "+" and monkey.val == "old" then + worry = worry * 2 + elseif monkey.op == "*" and monkey.val == "old" then + worry = worry * worry + elseif monkey.op == "+" and tonumber(monkey.val) then + worry = worry + tonumber(monkey.val) + elseif monkey.op == "*" and tonumber(monkey.val) then + worry = worry * tonumber(monkey.val) + end + local newworry = worry % divisor + if newworry % monkey.test == 0 then + table.insert(monkeys[monkey.t].items,newworry) + else + table.insert(monkeys[monkey.f].items,newworry) + end + end + monkey.items = {} + end +end +table.sort(monkeys, function(a,b) return a.inspected > b.inspected end) +print(monkeys[1].inspected * monkeys[2].inspected) diff --git a/11/ext.lua b/11/ext.lua new file mode 100755 index 0000000..c1eb1cc --- /dev/null +++ b/11/ext.lua @@ -0,0 +1,62 @@ +-- Override tostring to display more info about the table
+local old_tostring = tostring
+local numtabs = 0
+local printed_tables = {}
+
+local function tostring_helper(el)
+ assert(type(el) == "table", "Tried to call helper with something that was not a table, it was a " .. type(el))
+ local mt = getmetatable(el)
+ if mt and mt.__tostring then
+ return mt.__tostring(el)
+ elseif printed_tables[el] == true then
+ return old_tostring(el)
+ else
+ printed_tables[el] = true
+ numtabs = numtabs + 1
+ local strbuilder = setmetatable({"{"},{__index = table})
+ for k,v in pairs(el) do
+ local key,value
+ if type(k) == "table" then
+ key = tostring_helper(k)
+ else
+ key = old_tostring(k)
+ end
+ if type(v) == "table" then
+ value = tostring_helper(v)
+ else
+ value = old_tostring(v)
+ end
+ strbuilder:insert(string.format("%s%s : %s", string.rep("\t",numtabs), key, value))
+ end
+ strbuilder:insert(string.rep("\t",numtabs - 1) .. "}")
+ numtabs = numtabs - 1
+ return strbuilder:concat("\n")
+ end
+
+end
+function tostring(el)
+ printed_tables = {}
+ if type(el) == "table" then
+ return tostring_helper(el)
+ end
+ return old_tostring(el)
+end
+
+-- Functions to save my hands
+function printf(fmt, ...)
+ print(string.format(fmt,...))
+end
+function errorf(fmt, ...)
+ --Our error isn't actually in this function, it's 1 above us (1) = 2
+ error(string.format(fmt,...),2)
+end
+function assertf(bool, fmt, ...)
+ assert(type(fmt) == "string", "Assertf arg #2 was \"" .. type(fmt) .. "\", expected string")
+ if not bool then
+ args = {fmt}
+ for k,v in ipairs({...}) do
+ table.insert(args,tostring(v))
+ end
+ error(string.format(unpack(args)),2)
+ end
+end
diff --git a/11/input.txt b/11/input.txt new file mode 100755 index 0000000..b928712 --- /dev/null +++ b/11/input.txt @@ -0,0 +1,55 @@ +Monkey 0:
+ Starting items: 71, 56, 50, 73
+ Operation: new = old * 11
+ Test: divisible by 13
+ If true: throw to monkey 1
+ If false: throw to monkey 7
+
+Monkey 1:
+ Starting items: 70, 89, 82
+ Operation: new = old + 1
+ Test: divisible by 7
+ If true: throw to monkey 3
+ If false: throw to monkey 6
+
+Monkey 2:
+ Starting items: 52, 95
+ Operation: new = old * old
+ Test: divisible by 3
+ If true: throw to monkey 5
+ If false: throw to monkey 4
+
+Monkey 3:
+ Starting items: 94, 64, 69, 87, 70
+ Operation: new = old + 2
+ Test: divisible by 19
+ If true: throw to monkey 2
+ If false: throw to monkey 6
+
+Monkey 4:
+ Starting items: 98, 72, 98, 53, 97, 51
+ Operation: new = old + 6
+ Test: divisible by 5
+ If true: throw to monkey 0
+ If false: throw to monkey 5
+
+Monkey 5:
+ Starting items: 79
+ Operation: new = old + 7
+ Test: divisible by 2
+ If true: throw to monkey 7
+ If false: throw to monkey 0
+
+Monkey 6:
+ Starting items: 77, 55, 63, 93, 66, 90, 88, 71
+ Operation: new = old * 7
+ Test: divisible by 11
+ If true: throw to monkey 2
+ If false: throw to monkey 4
+
+Monkey 7:
+ Starting items: 54, 97, 87, 70, 59, 82, 59
+ Operation: new = old + 8
+ Test: divisible by 17
+ If true: throw to monkey 1
+ If false: throw to monkey 3
diff --git a/11/sample.txt b/11/sample.txt new file mode 100644 index 0000000..aa6f21a --- /dev/null +++ b/11/sample.txt @@ -0,0 +1,27 @@ +Monkey 0:
+ Starting items: 79, 98
+ Operation: new = old * 19
+ Test: divisible by 23
+ If true: throw to monkey 2
+ If false: throw to monkey 3
+
+Monkey 1:
+ Starting items: 54, 65, 75, 74
+ Operation: new = old + 6
+ Test: divisible by 19
+ If true: throw to monkey 2
+ If false: throw to monkey 0
+
+Monkey 2:
+ Starting items: 79, 60, 97
+ Operation: new = old * old
+ Test: divisible by 13
+ If true: throw to monkey 1
+ If false: throw to monkey 3
+
+Monkey 3:
+ Starting items: 74
+ Operation: new = old + 3
+ Test: divisible by 17
+ If true: throw to monkey 0
+ If false: throw to monkey 1
\ No newline at end of file |
