From 07c5ebcb1e10928524cd151d95e79e2cadf46753 Mon Sep 17 00:00:00 2001 From: Adam Millerchip Date: Thu, 17 Dec 2020 13:53:47 +0900 Subject: [PATCH] more tweaking --- day16/day16part2-simpler-transpose.exs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/day16/day16part2-simpler-transpose.exs b/day16/day16part2-simpler-transpose.exs index a1bdc7e..f3dd1e6 100644 --- a/day16/day16part2-simpler-transpose.exs +++ b/day16/day16part2-simpler-transpose.exs @@ -19,13 +19,13 @@ defmodule Day16Part2 do end) end - def transpose_tickets(tickets) do - tickets |> Enum.zip() |> Enum.map(&Tuple.to_list/1) |> Enum.with_index() - end + def transpose_tickets(tickets), do: tickets |> Enum.zip() |> Enum.map(&Tuple.to_list/1) # Compare each column against the rules to determine the list of fields each column satisfies def valid_fields_for_columns(transposed_tickets, rules) do - for {column, idx} <- transposed_tickets, do: {idx, valid_fields_for(column, rules)} + for {column, idx} <- Enum.with_index(transposed_tickets) do + {valid_fields_for(column, rules), idx} + end end # Reduce the list of valid fields by elimination. @@ -35,10 +35,10 @@ defmodule Day16Part2 do # Repeat for all columns. def reduce_to_column_by_field_name(valid_fields_by_column) do valid_fields_by_column - |> Enum.sort_by(fn {_k, v} -> length(v) end) - |> Enum.map_reduce([], fn {col, fields}, seen -> + |> Enum.sort_by(fn {fields, _idx} -> length(fields) end) + |> Enum.map_reduce([], fn {fields, idx}, seen -> [field] = fields -- seen - {{field, col}, [field | seen]} + {{field, idx}, [field | seen]} end) |> elem(0) end