Instantly share code, notes, and snippets.

Embed
What would you like to do?
require_relative 'check'
ADD2 = %r/
^ 0 \+ (?<ZeroPlusB> [01]++) \= \k<ZeroPlusB> $
| ^ (?<APlusZero> [01]++) \+ 0 \= \k<APlusZero> $
|
(?<B0> (?= (?<a> \d \g<a> \d | \+ \d*? 0 | \d*+ \+ ) \= )){0}
(?<B1> (?= (?<b> \d \g<b> \d | \+ \d*? 1 ) \= )){0}
(?<C0> (?= (?<c> \d \g<c> \d | \+ \d++ \= \d*? 0 ) $ )){0}
(?<C1> (?= (?<d> \d \g<d> \d | \+ \d++ \= \d*? 1 ) $ )){0}
(?<Carry0> (?= (?: 1 \g<B0> | 0 \g<B1> )*+ (?: 0 \g<B0> | \+ ))){0}
(?<Carry1> (?= (?: 1 \g<B0> | 0 \g<B1> )*+ 1 \g<B1> )){0}
(?<YleX> (?= (?<e> \d \g<e> \d | \d*+ \+ ) \= )){0}
(?<ZeqX> (?= (?<f> \d \g<f> \d | \+ \d++ \= ) $ )){0}
(?<ZeqSX> (?= (?<g> \d \g<g> \d | \+ \d++ \= 1 ) $ )){0}
(?<ZeqY> (?= (?<h> \d \g<h> \d | \= ) $ )){0}
(?<ZeqSY> (?= (?<i> \d \g<i> \d | \= 1 ) $ )){0}
(?<GetD> (?= (?<j> \d \g<j> \d | \+ (?<D> \d+ ) (?= (?<F> \d*+))) \= )){0}
(?<GetE> (?= (?<k> \d \g<k> \d | \+ \d++ \= (?<E> \d+ ) (?= (?<G> \d*+))) $ )){0}
(?<EeqD> \g<GetD> (?= (?<l> \d \g<l> \d | \+ \d++ \= \k<D> ) $ )){0}
(?<EeqSD> \g<GetD> \g<GetE>
(?= (?<m> \d \g<m> \d | \+ (?= # increment
\g<ZeqSY>
(?<n> 1 \g<n> 0 | \k<F> \= 1 ) \k<G> $
| \g<ZeqY>
(?<DECommon> (?: (?<DI> \d) (?=
(?<o> \d \g<o> \d | \k<F> \= \d*? \k<DI> ) \k<G> $
))*+)
0 (?<p> 1 \g<p> 0 | \k<F> \= \k<DECommon> 1 ) \k<G> $
) \d*? ) \= )
){0}
^
(?: \g<YleX> \g<ZeqX> \g<Carry0> # I
| \g<YleX> \g<ZeqSX> \g<Carry1> # II
| \g<EeqD> \g<Carry0> # III
| \g<EeqSD> \g<Carry1> # IV
)
(?: 0 \g<B0> \g<Carry0> \g<C0>
| 0 \g<B0> \g<Carry1> \g<C1>
| 0 \g<B1> \g<Carry0> \g<C1>
| 0 \g<B1> \g<Carry1> \g<C0>
| 1 \g<B0> \g<Carry0> \g<C1>
| 1 \g<B0> \g<Carry1> \g<C0>
| 1 \g<B1> \g<Carry0> \g<C0>
| 1 \g<B1> \g<Carry1> \g<C1>
)++
\+ [01]++ \= [01]++ $
/x
check(
ADD2: [
R1.repeated_permutation(2).map{|i,j|
"%b+%b=%b" % [i, j, i + j]
},
[*1..32].repeated_permutation(3).select{|i,j,k| i + j != k }.map{|i,j,k|
"%b+%b=%b" % [i, j, k]
},
],
)
R1 = *0..127
def check t
t.each do |name, tests|
reg = eval name.to_s
tests[0].each do |t|
if (m = reg =~ t) != 0
raise "#{name} =~ #{t.inspect} was #{m.inspect}"
end
end
tests[1].each do |t|
if m = reg =~ t
raise "#{name} =~ #{t.inspect} was #{m.inspect}"
end
end
puts "succeed: #{name}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment