Problem 42
https://projecteuler.net/problem=42
function read_names(path) open(path, "r") do fp for line in eachline(fp) return [ s[2:length(s)-1] for s in split(line, ",") ] end end end function is_square(n) return Int(floor(sqrt(n)))^2 == n end function is_triangle(n) # x^2 + x = 2n # x = (-1 + sqrt(1 + 8n)) / 2 return is_square(1 + 8n) end function word_score(word) return sum(Int(c)-Int('A')+1 for c in word) end function read_words(path) open(path, "r") do fp for line in eachline(fp) return [ replace(s, "\"" => "") for s in split(line, ",") ] end end end function e042(path) words = read_words(path) return sum(1 for word in words if is_triangle(word_score(word))) end path_names = ARGS[1] println(e042(path_names))
Float64をInt64に変換するとき、わざわざ小数点以下が0の浮動小数点数にしてからでないと変換できない。
replaceの引数は、文字列と文字列ではなく、文字列のPair。古いバージョンだと違うので注意。