Commit aa4e363
Changed files (1)
misc
cards
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