From 83d6f897691c22be0632b788edbd274dabd8b296 Mon Sep 17 00:00:00 2001 From: Adam Millerchip Date: Sat, 14 Dec 2024 17:47:15 +0900 Subject: [PATCH] 2024 Day 14 Part 2 - sort of --- 2024/day14.exs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/2024/day14.exs b/2024/day14.exs index ae72a9b..0f163de 100755 --- a/2024/day14.exs +++ b/2024/day14.exs @@ -21,7 +21,72 @@ defmodule Day14 do end def part2({robots, size_x, size_y}) do - :not_done_yet + # The tree looks like this + # XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX + # X X + # X X + # X X + # X X + # X X X + # X XXX X + # X XXXXX X + # X XXXXXXX X + # X XXXXXXXXX X + # X XXXXX X + # X XXXXXXX X + # X XXXXXXXXX X + # X XXXXXXXXXXX X + # X XXXXXXXXXXXXX X + # X XXXXXXXXX X + # X XXXXXXXXXXX X + # X XXXXXXXXXXXXX X + # X XXXXXXXXXXXXXXX X + # X XXXXXXXXXXXXXXXXX X + # X XXXXXXXXXXXXX X + # X XXXXXXXXXXXXXXX X + # X XXXXXXXXXXXXXXXXX X + # X XXXXXXXXXXXXXXXXXXX X + # X XXXXXXXXXXXXXXXXXXXXX X + # X XXX X + # X XXX X + # X XXX X + # X X + # X X + # X X + # X X + # XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX + # Could improve this by searching for that pattern explicitly. In this case I just searched + # manually: noticed things looked suspicious after 27 seconds + # also noticed that repeated every 101 seconds + # Checked each 101 second jump until found the above tree + tick_forever(robots, size_x, size_y) + end + + def tick_forever(robots, size_x, size_y, count \\ 27, acc \\ 27) do + robots = tick(robots, size_x, size_y, count) + robomap = MapSet.new(robots, fn [x, y, _, _] -> {x, y} end) + + for y <- 0..(size_y - 1) do + for x <- 0..(size_x - 1) do + if MapSet.member?(robomap, {x, y}) do + IO.write("X") + else + IO.write(" ") + end + end + + IO.puts("|") + end + + IO.gets("#{acc} tree?") + + tick_forever(robots, size_x, size_y, 101, acc + 101) + end + + def tick(robots, size_x, size_y, times) do + Enum.map(robots, fn [px, py, vx, vy] -> + [Integer.mod(px + vx * times, size_x), Integer.mod(py + vy * times, size_y), vx, vy] + end) end def input do