|
| 1 | +import gleam/dict.{type Dict} |
| 2 | +import gleam/list |
| 3 | +import gleam/result |
| 4 | +import gleam/string |
| 5 | +import grid.{type Pos} |
| 6 | +import parse |
| 7 | + |
| 8 | +pub fn part1(input: String) -> Int { |
| 9 | + let #(antennas, width, height) = parse_input(input) |
| 10 | + |
| 11 | + antennas |
| 12 | + |> dict.keys |
| 13 | + |> list.flat_map(fn(label) { antinodes_for(antennas, label, width, height) }) |
| 14 | + |> list.unique |
| 15 | + |> list.length |
| 16 | +} |
| 17 | + |
| 18 | +fn antinodes_for( |
| 19 | + antennas: Dict(String, List(Pos)), |
| 20 | + label: String, |
| 21 | + width: Int, |
| 22 | + height: Int, |
| 23 | +) -> List(Pos) { |
| 24 | + antennas |
| 25 | + |> dict.get(label) |
| 26 | + |> result.unwrap([]) |
| 27 | + |> list.combination_pairs |
| 28 | + |> list.flat_map(fn(pair) { |
| 29 | + [individual_antinode(pair.0, pair.1), individual_antinode(pair.1, pair.0)] |
| 30 | + }) |
| 31 | + |> list.filter(fn(pos) { |
| 32 | + pos.0 >= 0 && pos.0 < width && pos.1 >= 0 && pos.1 < height |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +fn individual_antinode(a: Pos, b: Pos) -> Pos { |
| 37 | + let dx = a.0 - b.0 |
| 38 | + let dy = a.1 - b.1 |
| 39 | + #(a.0 + dx, a.1 + dy) |
| 40 | +} |
| 41 | + |
| 42 | +fn parse_input(input: String) -> #(Dict(String, List(Pos)), Int, Int) { |
| 43 | + let lines = parse.lines(input) |
| 44 | + |
| 45 | + let pos_to_char = |
| 46 | + lines |
| 47 | + |> list.index_map(fn(line, y) { |
| 48 | + line |
| 49 | + |> string.to_graphemes |
| 50 | + |> list.index_map(fn(char, x) { #(#(x, y), char) }) |
| 51 | + |> list.filter(fn(poschar) { poschar.1 != "." }) |
| 52 | + }) |
| 53 | + |> list.flatten |
| 54 | + |> dict.from_list |
| 55 | + |
| 56 | + let width = lines |> list.first |> result.unwrap("") |> string.length |
| 57 | + let height = lines |> list.length |
| 58 | + |
| 59 | + #( |
| 60 | + pos_to_char |
| 61 | + |> dict.values |
| 62 | + |> list.unique |
| 63 | + |> list.map(fn(char) { |
| 64 | + #( |
| 65 | + char, |
| 66 | + pos_to_char |
| 67 | + |> dict.to_list |
| 68 | + |> list.filter_map(fn(poschar) { |
| 69 | + case poschar.1 == char { |
| 70 | + True -> Ok(poschar.0) |
| 71 | + False -> Error(Nil) |
| 72 | + } |
| 73 | + }), |
| 74 | + ) |
| 75 | + }) |
| 76 | + |> dict.from_list, |
| 77 | + width, |
| 78 | + height, |
| 79 | + ) |
| 80 | +} |
0 commit comments