commit c0b5ee3d1055f53ff16474a01e2b7327a8c2b6c6 Author: RĂ¼diger Diedrich Date: Wed Jan 17 15:00:57 2024 +0100 init brc.exs diff --git a/brc.exs b/brc.exs new file mode 100644 index 0000000..9196215 --- /dev/null +++ b/brc.exs @@ -0,0 +1,39 @@ +Mix.install([ + {:flow, "~> 1.2.4"}, + {:ex_cldr_collation, "~> 0.7.2"} +]) + +defmodule Brc do + require Logger + + def run(path) do + for {city, [min, max, mean]} <- parse_file(path) do + "#{city}=#{min}/#{max}/#{Float.round(mean, 1)}" + end + |> Enum.sort(Cldr.Collation) + |> Enum.join(",") + |> then(fn out -> "{" <> out <> "}" end) + |> IO.puts() + end + + def parse_file(path) do + Logger.info("parse file #{path}") + + File.stream!(path) + |> Flow.from_enumerable() + |> Flow.map(&parse_line/1) + |> Flow.partition() + |> Flow.reduce(fn -> %{} end, fn {city, temp}, acc -> + Map.update(acc, city, [temp, temp, temp], fn [prev_min, prev_max, mean] -> + [min(prev_min, temp), max(prev_max, temp), (mean + temp) / 2] + end) + end) + end + + def parse_line(line) do + [city, temp] = String.trim(line) |> String.split(";") + {city, String.to_float(temp)} + end +end + +Brc.run("./priv/data.txt")