From e12436c99bb04897d6344b6351d0336e4c627573 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Thu, 28 Nov 2019 17:38:10 +0100 Subject: add rest --- day25.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 day25.rb (limited to 'day25.rb') diff --git a/day25.rb b/day25.rb new file mode 100644 index 0000000..5b51602 --- /dev/null +++ b/day25.rb @@ -0,0 +1,34 @@ +d = File.readlines("day25").map { |x| x.split(",").map(&:to_i) } + +class DSU + + def initialize(x) + @array = Array.new(x) {|i| i } + end + + def find(x) + return x if @array[x] == x + @array[x] = find(@array[x]) + end + + def union(a, b) + @array[find(a)] = @array[find(b)] + end + + def sets + @array.collect{ |i| find(i) }.uniq.length + end + +end + +dsu = DSU.new(d.size) + +(0...d.size).each { |i| + (i+1...d.size).each { |j| + if d[i].zip(d[j]).map { |(a,b)| (a-b).abs }.sum <= 3 + dsu.union(i, j) + end + } +} + +p dsu.sets -- cgit 1.4.1