Day 08
Mix.install([:kino_aoc])
Section
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2024", "8", System.fetch_env!("LB_ADVENT_OF_CODE_SESSION"))
{:ok,
"..........M..........j.............y.....O........\n...B...............q......m........lGO............\n....................q......2.l.GQ...O.............\n.....X.......................................4....\n.....................q............................\n....M......P...............xl.K.............2.....\n....F.........L.......C.K..............m..........\n..........FM......P....jy......m..........o...r...\n..X.......P.....RL..............G..x..........4...\n............L..........NC.....q...................\n.....C.X...............K....y..........4..........\n........S...R.............j.x.....V...4...........\n.....................R..x.....V..i......m.........\n...........................R.V......N.......X.....\n.....F.........M......N......E....................\n................v................T.......F......O.\n.............................N...V.......Q........\n...v.....................C.....i..................\n......c.....W..n.w........................E.......\n3...................c.....................Q..6....\n...........h......................j...............\n.......n.0......h.................E..............2\n.v.............7.......120.....c..................\n......n.0............w...........D.t.........E...r\n....8..3......0.w.hP....z...D..T...............r..\n.................f........T........G......eQ......\n......f.n.....7..p................................\n.....Y..7.......f......I......D......K............\n............Uf....T..W.....D..r...i...............\n......I...............................Z...........\n....5....B.......b..............s..............Z..\n..........d...W..Uwh.............c..........i.....\n..I.3..Y......................e...................\n.....p.b..........k......7........................\np...........k....I..b..........s..................\n.....k.......o...........W........................\n.A..Y..........U.................a........6.......\n..A...Y.p...................................6.....\nB......k..........................Z............u..\n...3.....................s..............a.........\n......A.........................g.....a...........\n.......A....8...b.U......H....sS..................\n.........................S1.............t.........\n.....................9z..e.....5..1.g.u...........\n.......................z....d....g....H.J....o.6..\n........B................d.....u....9.J.H.........\n.8........S.................u9.............J.....H\n.....................Z5.............t1...........a\n.....................e..v...................o..t..\n.....8...............L.....z.............J........"}
# puzzle_input =
"""
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
"""
freqs =
puzzle_input
|> String.split("\n", trim: true)
|> Enum.with_index()
|> Enum.flat_map(fn {row, y} ->
row
|> String.to_charlist()
|> Enum.with_index()
|> Enum.filter(fn {freq, _} -> freq != ?. end)
|> Enum.map(fn {freq, x} -> {{x, y}, freq} end)
end)
|> Map.new()
|> Enum.group_by(&elem(&1, 1), &elem(&1, 0))
warning: code block contains unused literal "............\n........0...\n.....0......\n.......0....\n....0.......\n......A.....\n............\n............\n........A...\n.........A..\n............\n............\n" (remove the literal or assign it to _ to avoid warnings)
└─ Workspace/hauleth/advent-of-code/2024/day08.livemd#cell:y5uv5vxll4fey6gh:1
%{
117 => [{28, 46}, {31, 45}, {38, 43}, {47, 38}],
73 => [{2, 32}, {17, 34}, {23, 27}, {6, 29}],
89 => [{4, 36}, {7, 32}, {6, 37}, {5, 27}],
48 => [{25, 22}, {9, 21}, {8, 23}, {14, 24}],
57 => [{29, 46}, {21, 43}, {36, 45}],
113 => [{30, 9}, {20, 2}, {21, 4}, {19, 1}],
69 => [{42, 18}, {34, 21}, {45, 23}, {29, 14}],
88 => [{7, 10}, {5, 3}, {44, 13}, {2, 8}],
100 => [{25, 45}, {28, 44}, {10, 31}],
71 => [{36, 1}, {35, 25}, {31, 2}, {32, 8}],
98 => [{16, 41}, {7, 33}, {20, 34}, {17, 30}],
81 => [{42, 19}, {41, 16}, {43, 25}, {32, 2}],
76 => [{12, 9}, {14, 6}, {17, 8}, {21, 49}],
85 => [{15, 36}, {12, 28}, {17, 31}, {18, 41}],
120 => [{27, 5}, {35, 8}, {28, 11}, {24, 12}],
109 => [{26, 1}, {39, 6}, {40, 12}, {31, 7}],
72 => [{38, 44}, {40, 45}, {49, 46}, {25, 41}],
110 => [{8, 26}, {7, 21}, {15, 18}, {6, 23}],
68 => [{30, 27}, {28, 24}, {27, 28}, {33, 23}],
86 => [{29, 13}, {33, 16}, {30, 12}, {34, 11}],
116 => [{35, 23}, {36, 47}, {47, 48}, {40, 42}],
51 => [{7, 24}, {0, 19}, {3, 39}, {4, 32}],
80 => [{18, 7}, {19, 24}, {10, 8}, {11, 5}],
77 => [{11, 7}, {15, 14}, {10, 0}, {4, 5}],
78 => [{22, 14}, {29, 16}, {36, 13}, {23, 9}],
108 => [{29, 2}, {28, 5}, {35, 1}],
103 => [{32, 40}, {36, 43}, {33, 44}],
122 => [{27, 49}, {23, 44}, {24, 24}, {22, 43}],
83 => [{10, 46}, {25, 42}, {31, 41}, {8, 11}],
74 => [{38, 45}, {41, 49}, {43, 46}, {40, 44}],
99 => [{31, 22}, {33, 31}, {20, 19}, {6, 18}],
112 => [{5, 33}, {17, 26}, {8, 37}, {0, 34}],
97 => [{38, 40}, {33, 36}, {49, 47}, {40, 39}],
104 => [{16, 21}, {19, 31}, {11, 20}, {18, 24}],
75 => [{30, 5}, {24, 6}, {23, 10}, {37, 27}],
90 => [{21, 47}, {47, 30}, {34, 38}, {38, 29}],
55 => [{15, 22}, {14, 26}, {8, 27}, {25, 33}],
52 => [{45, 3}, {38, 11}, {39, 10}, {46, 8}],
111 => [{13, 35}, {42, 7}, {45, 44}, {44, 48}],
87 => [{25, 35}, {12, 18}, {14, 31}, {21, 28}],
119 => [{21, 23}, {18, 31}, {17, 18}, {16, 24}],
82 => [{12, 11}, {27, 13}, {21, 12}, {16, 8}],
53 => [{22, 47}, {31, 43}, {4, 30}],
50 => [{27, 2}, {24, 22}, {44, 5}, {49, 21}],
66 => [{8, 45}, {9, 30}, {3, 1}, {0, ...}],
70 => [{5, 14}, {41, 15}, {10, ...}, {...}],
56 => [{12, 41}, {4, ...}, {...}, ...],
102 => [{17, ...}, {...}, ...],
105 => [{...}, ...],
121 => [...],
...
}
{w, h} =
puzzle_input
|> String.split("\n", trim: true)
|> then(fn [a | _] = lst ->
{byte_size(a), length(lst)}
end)
{50, 50}
defmodule Antennas do
def each_pair([]), do: []
def each_pair([a | rest]) do
for(b <- rest, do: {a, b}) ++ each_pair(rest)
end
def antinodes({xa, ya}, {xb, yb}) do
dx = xa - xb
dy = ya - yb
Stream.from_index()
|> Stream.flat_map(fn n ->
dx = dx * n
dy = dy * n
[{xa + dx, ya + dy}, {xb - dx, yb - dy}]
end)
end
end
{:module, Antennas, <<70, 79, 82, 49, 0, 0, 10, ...>>, {:antinodes, 2}}
Part 1
antinodes =
for {_, antennas} <- freqs,
{a, b} <- Antennas.each_pair(antennas),
{x, y} = node <- Antennas.antinodes(a, b) |> Stream.drop(2) |> Enum.take(2),
x in 0..(w - 1) and y in 0..(h - 1),
into: MapSet.new() do
node
end
MapSet.new([
{20, 41},
{20, 3},
{16, 10},
{26, 21},
{6, 18},
{10, 32},
{20, 30},
{33, 17},
{17, 30},
{29, 28},
{40, 35},
{28, 37},
{25, 49},
{10, 19},
{14, 17},
{35, 7},
{29, 48},
{1, 34},
{23, 8},
{35, 6},
{49, 17},
{46, 38},
{5, 39},
{37, 29},
{16, 15},
{48, 41},
{4, 18},
{24, 15},
{33, 44},
{39, 18},
{40, 25},
{35, 21},
{41, 17},
{20, 37},
{45, 24},
{0, 35},
{18, 3},
{1, 36},
{35, 41},
{37, 28},
{28, 15},
{29, 38},
{46, 43},
{31, 19},
{19, 39},
{43, 10},
{38, 10},
{21, 26},
{28, ...},
{...},
...
])
MapSet.size(antinodes)
398
Part 2
antinodes_res =
for {_, antennas} <- freqs,
{a, b} <- Antennas.each_pair(antennas),
{x, y} = node <- Enum.take(Antennas.antinodes(a, b), 100),
x in 0..(w - 1) and y in 0..(h - 1),
into: MapSet.new() do
node
end
MapSet.new([
{37, 47},
{35, 30},
{4, 5},
{20, 41},
{31, 42},
{9, 34},
{47, 38},
{20, 3},
{47, 44},
{22, 36},
{45, 7},
{19, 38},
{27, 21},
{9, 33},
{16, 10},
{21, 45},
{26, 21},
{21, 48},
{16, 38},
{35, 26},
{28, 20},
{45, 19},
{27, 22},
{16, 24},
{6, 18},
{10, 32},
{33, 15},
{36, 14},
{20, 30},
{33, 17},
{41, 1},
{36, 25},
{28, 48},
{5, 9},
{14, 16},
{17, 30},
{30, 49},
{32, 18},
{29, 28},
{40, 35},
{6, 23},
{17, 39},
{28, 37},
{6, 29},
{39, 23},
{49, 16},
{34, 0},
{11, 37},
{25, ...},
{...},
...
])
MapSet.size(antinodes_res)
1333