this repo has no description
at master 2.1 kB view raw
1<!-- vim:set ft=markdown: --> 2 3<!-- livebook:{"persist_outputs":true} --> 4 5# Day 13 6 7```elixir 8[input, folds] = 9 File.read!("day13.txt") 10 |> String.trim() 11 |> String.split("\n\n") 12 13input = 14 input 15 |> String.split("\n") 16 |> Enum.map(fn line -> 17 [x, y] = String.split(line, ",") 18 19 {String.to_integer(x), String.to_integer(y)} 20 end) 21 |> MapSet.new() 22 23folds = 24 folds 25 |> String.split("\n") 26 |> Enum.map(fn 27 "fold along " <> <<c>> <> "=" <> rest -> 28 {String.to_atom(<<c>>), String.to_integer(rest)} 29 end) 30 31defmodule Day13 do 32 def fold({orientation, pos}, set) do 33 Enum.reduce(set, MapSet.new(), fn point, acc -> 34 new_point = folded_coords(orientation, pos, point) 35 36 MapSet.put(acc, new_point) 37 end) 38 end 39 40 defp folded_coords(:x, col, {x, y}) when x > col, do: {abs(2 * col - x), y} 41 defp folded_coords(:y, row, {x, y}) when y > row, do: {x, abs(2 * row - y)} 42 defp folded_coords(_, _, point), do: point 43 44 def draw(set) do 45 set 46 |> Enum.group_by(&elem(&1, 1)) 47 |> Enum.sort() 48 |> Enum.map(fn {_, points} -> 49 points 50 |> Enum.map(&elem(&1, 0)) 51 |> Enum.sort() 52 |> Enum.chunk_every(2, 1) 53 |> Enum.map(fn 54 [a, b] -> b - a 55 _ -> 0 56 end) 57 |> Enum.map(&String.pad_trailing("█", &1, " ")) 58 end) 59 |> Enum.join("\n") 60 end 61end 62``` 63 64```output 65{:module, Day13, <<70, 79, 82, 49, 0, 0, 13, ...>>, {:draw, 1}} 66``` 67 68## Task 1 69 70```elixir 71Day13.fold(hd(folds), input) |> MapSet.size() 72``` 73 74```output 75802 76``` 77 78## Task 2 79 80```elixir 81Enum.reduce(folds, input, &Day13.fold/2) 82|> Day13.draw() 83|> IO.puts() 84``` 85 86```output 87███ █ █ █ █ ████ ████ ██ █ █ ███ 88█ █ █ █ █ █ █ █ █ █ █ █ █ █ 89█ █ ██ ████ ███ █ █ █ █ ███ 90███ █ █ █ █ █ █ █ ██ █ █ █ █ 91█ █ █ █ █ █ █ █ █ █ █ █ █ █ 92█ █ █ █ █ █ █ ████ ███ ██ ███ 93``` 94 95```output 96:ok 97``` 98