[2024][ruby][12.1]
change uvppmvvyqrwyvwqzlovlpqyzxktxkzlm
commit 5400e3eaf11f708ef2530e22ffe3e834ece5ebe0
author Alpha Chen <alpha@kejadlen.dev>
date
parent kqprlrpq
diff --git a/2024/ruby/day_12.rb b/2024/ruby/day_12.rb
new file mode 100644
index 0000000..4d53774
--- /dev/null
+++ b/2024/ruby/day_12.rb
@@ -0,0 +1,45 @@
+garden = DATA.readlines(chomp: true).flat_map.with_index {|row, y|
+  row.chars.map.with_index {|id, x| [[y,x], id] }
+}.to_h
+
+DELTAS = [[-1, 0], [1, 0], [0, -1], [0, 1]]
+
+queue = Set[*garden.keys]
+plots = [Set[queue.first]]
+until queue.empty?
+  yx = plots.last.select { queue.include?(_1) }.first
+  if yx.nil?
+    plots << Set[queue.first] if queue.first
+    next
+  end
+
+  queue.delete(yx)
+
+  id = garden.fetch(yx)
+  neighbors = DELTAS
+    .map {|d| yx.zip(d).map { _1 + _2 }}
+    .select { garden.fetch(_1, nil) == id }
+  if neighbors.empty?
+    plots << Set[queue.first] if queue.first
+  else
+    plots.last.merge(neighbors)
+  end
+end
+
+perimeter = ->(plot) {
+  plot.flat_map {|yx|
+    DELTAS
+      .map {|delta| yx.zip(delta).map { _1 + _2 }}
+      .reject { garden.fetch(_1, nil) == garden[plot.first] }
+  }
+}
+
+pp plots
+  .map { [garden.fetch(_1.first), _1.size, perimeter[_1].size] }
+  .sum { _2 * _3 }
+
+__END__
+AAAA
+BBCD
+BBCC
+EEEC