Skip to content

Instantly share code, notes, and snippets.

@andkon
Created December 3, 2017 08:17
Show Gist options
  • Save andkon/0e4cba0f4e43f55d2eabe48c62e85307 to your computer and use it in GitHub Desktop.
Save andkon/0e4cba0f4e43f55d2eabe48c62e85307 to your computer and use it in GitHub Desktop.
# The spreadsheet consists of rows of apparently-random numbers.
# To make sure the recovery process is on the right track, they need you to calculate the spreadsheet's checksum.
# For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences.
# For example, given the following spreadsheet:
# 5 1 9 5
# 7 5 3
# 2 4 6 8
# The first row's largest and smallest values are 9 and 1, and their difference is 8.
# The second row's largest and smallest values are 7 and 3, and their difference is 4.
# The third row's difference is 6.
# In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18.
s1="""
PUT YOUR SPREADSHEET HERE
"""
defmodule Second do
def checksum_for_spreadsheet(spreadsheet) do
# spreadsheet is a list of lists
# it's pass
prep_spreadsheet(spreadsheet) |> checksum
end
defp prep_spreadsheet(spreadsheet_string) do
# ok split this into rows by newline
String.split(spreadsheet_string, "\n")
end
defp checksum([]) do
# empty list, so return 0
0
end
defp checksum(rows) do
# the first call on a list with at least one thing
IO.puts("There are " <> Integer.to_string(length(rows)) <> " rows")
checksum(rows, 0)
end
defp checksum([], sum) do
# the last call - the list is now empty, so return the sum. this is the sum of all lists
sum
end
defp checksum([head | tail], sum) do
# where we do all the hard work
# first let's get a list of integers
row_str = String.split(head)
row_int = Enum.map(row_str, fn(x) -> String.to_integer(x) end)
# now, you get the highest and lowest values in the head, which is the row
# then you get the difference
# ok maybe we'll do that together
diff = row_difference(row_int)
IO.puts("Now the sum is " <> Integer.to_string(sum + diff))
checksum(tail, sum+diff) # less ugly by far
end
defp row_difference([]) do
0
end
defp row_difference(list) do
# the way in
row_difference(list, 0)
end
defp row_difference([head | tail], high) when head > high do
row_difference(tail, head)
end
defp row_difference([head | tail], high) when head < high do
row_difference(tail, high, head)
end
defp row_difference([head | tail], high, low) when head > high do
# replace the top value
row_difference(tail, head, low)
end
defp row_difference([head | tail], high, low) when head < low do
# replace lowest value
row_difference(tail, high, head)
end
defp row_difference([head | tail], high, low) do
row_difference(tail, high, low)
end
defp row_difference([], high, low) do
IO.puts("The high is " <> Integer.to_string(high) <> " and the low is " <> Integer.to_string(low) <> " with a diff of " <> Integer.to_string(high-low))
high - low
end
end
IO.puts(Second.checksum_for_spreadsheet(s1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment