diff options
| author | mo khan <mo.khan@gmail.com> | 2020-08-25 20:58:17 -0600 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-08-25 20:58:17 -0600 |
| commit | aa4e36309539242fc2bd10f63cce48ec3fb128b2 (patch) | |
| tree | 941875f294fe45d4956b6cd8d4febd415e971acd | |
| parent | 816f53a1fabf144f43da16f51ee9e27b612621aa (diff) | |
Try some bitwise operations
| -rw-r--r-- | misc/cards/main.rb | 63 |
1 files 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 @@ -52,6 +77,38 @@ class Solution "" 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 assert_equal(true, Card.new("+C").match?(Card.new("-CC"), Card.new("=CCC"))) |
