aboutsummaryrefslogtreecommitdiffhomepage
path: root/problems/13-Mod-3/a.rb
blob: b0e909bc9fc7eed249b91b3e5cb163c60defc693 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def f1(n)
  n = ((n >> 0) & 0b11) +
      ((n >> 2) & 0b11) +
      ((n >> 4) & 0b11) +
      ((n >> 6) & 0b11) +
      0
  n = ((n >> 0) & 0b11) +
      ((n >> 2) & 0b11) +
      ((n >> 4) & 0b11) +
      ((n >> 6) & 0b11) +
      0
  n = ((n >> 0) & 0b11) +
      ((n >> 2) & 0b11) +
      ((n >> 4) & 0b11) +
      ((n >> 6) & 0b11) +
      0
  n == 3 ? 0 : n
end

def f2(n)
  n = ((n >> 0) & 0b11) +
      ((n >> 2) & 0b11) +
      ((n >> 4) & 0b11) +
      ((n >> 6) & 0b11) +
      0
  n = ((n >> 0) & 0b11) +
      ((n >> 2) & 0b11) +
      0
  n = ((n >> 0) & 0b11) +
      ((n >> 2) & 0b11) +
      0
  n == 3 ? 0 : n
end

def f3(n)
  n = ((n >> 0) & 0b1111) +
      ((n >> 4) & 0b1111) +
      0
  n = ((n >> 0) & 0b11) +
      ((n >> 2) & 0b11) +
      ((n >> 4) & 0b11) +
      0
  n = ((n >> 0) & 0b11) +
      ((n >> 2) & 0b11) +
      0
  n == 3 ? 0 : n
end

256.times do |n|
  expected = n % 3
  actual = f3(n)

  if actual != expected
    puts "#{actual} != #{expected} (#{n}; #{"%08b" % n})"
  end
end