local list1, list2 = {}, {} for line in io.lines("input.txt") do list1[#list1 + 1] = line:match("^(%d+)%s") list2[#list2 + 1] = line:match("%s(%d+)$") end table.sort(list1) table.sort(list2) local counts = {} for q = 1, #list1 do if counts[list1[q]] == nil then counts[list1[q]] = 0 end if counts[list2[q]] == nil then counts[list2[q]] = 0 end counts[list2[q]] = counts[list2[q]] + 1 end local distance, similarity = 0, 0 for q = 1, #list1 do distance = distance + math.abs(list1[q] - list2[q]) similarity = similarity + (list1[q] * counts[list1[q]]) end print(distance) print(similarity)