Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function readLinesFromFile(file)
- local fs = assert(io.open(file, "rb"))
- local content = fs:read("*all")
- local lines = {}
- fs:close()
- for line in content:gmatch("[^\r\n]+") do
- table.insert(lines, line)
- end
- return lines
- end
- function tablelength(table)
- local count = 0
- for _ in pairs(table) do
- count = count + 1
- end
- return count
- end
- function groupByCharacter(word)
- local temp = {}
- for j = 1, string.len(word) do
- local character = string.sub(word, j, j)
- if temp[character] == nil then
- temp[character] = 0
- end
- temp[character] = temp[character] + 1
- end
- return temp
- end
- function containsValue(dict, value)
- for k, v in pairs(dict) do
- if v == value then
- return true
- end
- end
- return false
- end
- function main()
- local words = readLinesFromFile("input.txt")
- local twos = 0
- local threes = 0
- for i = 1, tablelength(words) do
- local word = words[i]
- local occurences = groupByCharacter(word)
- if containsValue(occurences, 2) then
- twos = twos + 1
- end
- if containsValue(occurences, 3) then
- threes = threes + 1
- end
- end
- local checksum = twos * threes
- print(checksum)
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement