Guest User

AoC 2025 Day 8 Part 2

a guest
Dec 10th, 2025
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.35 KB | Source Code | 0 0
  1. import core.time;
  2. import std.stdio;
  3. import std.typecons;
  4.  
  5. struct Box
  6. {
  7.     long x, y, z;
  8.  
  9.     long distance_to(const ref Box other) const
  10.     {
  11.         return
  12.             (x - other.x)^^2 +
  13.             (y - other.y)^^2 +
  14.             (z - other.z)^^2;
  15.     }
  16.  
  17.     /*
  18.     string toString() const
  19.     {
  20.         return format("%d,%d,%d", x, y, z);
  21.     }
  22.     */
  23. }
  24.  
  25. int main()
  26. {
  27.     MonoTime t1 = MonoTime.currTime;
  28.  
  29.     Box[] boxes;
  30.     for (auto read_result = read_box();
  31.          read_result.result;
  32.          read_result = read_box())
  33.     {
  34.         boxes ~= read_result.box;
  35.  
  36.         //
  37.         // writefln("%d, %d, %d, %d", read_result.box.circuit, read_result.box.x, read_result.box.y, read_result.box.z);
  38.         //
  39.     }
  40.  
  41.     MonoTime t2 = MonoTime.currTime;
  42.     writefln("Reading:\t%u", (t2 - t1).total!"usecs");
  43.  
  44.     const int boxes_length = cast(int) boxes.length;
  45.  
  46.     t1 = MonoTime.currTime;
  47.  
  48.     long[] cheapestCost = new long[boxes_length];
  49.     int[] cheapestEdge = new int[boxes_length];
  50.     bool[] explored = new bool[boxes_length];
  51.  
  52.     for (int i = 0; i < boxes_length; ++i)
  53.     {
  54.         cheapestCost[i] = long.max;
  55.     }
  56.  
  57.     cheapestCost[0] = 0;
  58.  
  59.     t2 = MonoTime.currTime;
  60.     writefln("Assign:\t\t%u", (t2 - t1).total!"usecs");
  61.  
  62.     t1 = MonoTime.currTime;
  63.  
  64.     for (int step = 0; step < boxes_length; ++step)
  65.     {
  66.         int current_vertex;
  67.         long min_cost = long.max;
  68.         for (int i = 0; i < boxes_length; ++i)
  69.         {
  70.             long cheapestCost_i = cheapestCost[i];
  71.             if (!explored[i] && (cheapestCost_i < min_cost))
  72.             {
  73.                 min_cost = cheapestCost_i;
  74.                 current_vertex = i;
  75.             }
  76.         }
  77.  
  78.         explored[current_vertex] = true;
  79.         const ref current_vertex_box = boxes[current_vertex];
  80.         const int current_vertex_edge = current_vertex << 16;
  81.  
  82.         for (int neighbor = 0; neighbor < boxes_length; ++neighbor)
  83.         {
  84.             if (neighbor == current_vertex)
  85.             {
  86.                 continue;
  87.             }
  88.  
  89.             if (!explored[neighbor])
  90.             {
  91.                 long distance = current_vertex_box.distance_to(boxes[neighbor]);
  92.                 if (distance < cheapestCost[neighbor])
  93.                 {
  94.                     cheapestCost[neighbor] = distance;
  95.                     cheapestEdge[neighbor] = current_vertex_edge | neighbor;
  96.                 }
  97.             }
  98.         }
  99.     }
  100.  
  101.     t2 = MonoTime.currTime;
  102.     writefln("Prim:\t\t%u", (t2 - t1).total!"usecs");
  103.  
  104.     t1 = MonoTime.currTime;
  105.  
  106.     long max_distance = 0;
  107.     int longest_edge;
  108.     for (int i = 0; i < boxes_length; ++i)
  109.     {
  110.         int cheapestEdge_i = cheapestEdge[i];
  111.         if (cheapestEdge_i != 0)
  112.         {
  113.             long cheapestCost_i = cheapestCost[i];
  114.             if (cheapestCost_i > max_distance)
  115.             {
  116.                 max_distance = cheapestCost_i;
  117.                 longest_edge = cheapestEdge_i;
  118.             }
  119.         }
  120.     }
  121.  
  122.     t2 = MonoTime.currTime;
  123.     writefln("Result:\t\t%u", (t2 - t1).total!"usecs");
  124.  
  125.     long result = boxes[longest_edge >> 16].x * boxes[longest_edge & 0xffff].x;
  126.     writeln(result);
  127.     return 0;
  128. }
  129.  
  130. Tuple!(bool, "result", Box, "box") read_box()
  131. {
  132.     Box box;
  133.     return tuple!("result", "box")(3 == readf("%d,%d,%d\n", box.x, box.y, box.z), box);
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment