From aa4e36309539242fc2bd10f63cce48ec3fb128b2 Mon Sep 17 00:00:00 2001 From: mo khan Date: Tue, 25 Aug 2020 20:58:17 -0600 Subject: Try some bitwise operations --- misc/cards/main.rb | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/misc/cards/main.rb b/misc/cards/main.rb index c2163ec..a8f2c1d 100644 --- a/misc/cards/main.rb +++ b/misc/cards/main.rb @@ -4,6 +4,17 @@ end class Card attr_reader :raw, :prefix, :letter, :number + BITS = { + "+" => 256, + "-" => 128, + "=" => 64, + "A" => 32, + "B" => 16, + "C" => 8, + "1" => 4, + "2" => 2, + "3" => 1, + } def initialize(raw) @raw = raw @@ -12,12 +23,25 @@ class Card @number = raw[1..-1].size end + def <=>(other) + self.score <=> other.score + end + + def score + @score ||= begin + score = 0 + score += BITS[prefix] + score += BITS[letter] + score += BITS[number.to_s] + score + end + end + def match?(*others) others << self - [:prefix, :letter, :number].each do |property| - count = others.map(&property).uniq.count - return false if count != 1 && count != others.size + x = others.map(&property).uniq.count + return false if x != 1 && x != others.size end true @@ -43,6 +67,7 @@ class Solution y = cache[cards[p]] for q in (p+1...cards.size) z = cache[cards[q]] + if x.match?(y, z) return [x, y, z].join(' ') end @@ -50,6 +75,38 @@ class Solution end end + "" + end + +=begin +|4|2|1| +|+|-|=| + +|4|2|1| +|A|B|C| + +123 + +|256|128| 64| 32| 16| 8| 4| 2| 1| +| +| -| =| A| B| C| 1| 2| 3| + +-A: 128 + 32 + 3 +-B: 128 + 16 + 3 + +=end + def run(row) + cards = row.split.map { |x| Card.new(x) } + for i in (0...cards.size) + x = cards[i] + for p in (i+1...cards.size) + y = cards[p] + for q in (p+1...cards.size) + z = cards[q] + return [x, y, z].join(' ') if x.match?(y, z) + end + end + end + "" end end -- cgit v1.2.3