Densely packed decimal (DPD) to decimal

26

2

For nandgame fans: Please try out DPD to decimal in logic gates too!

Background

Densely packed decimal (DPD) is a way to efficiently store decimal digits in binary. It stores three decimal digits (000 to 999) in 10 bits, which is much more efficient than naive BCD (which stores one digit in 4 bits).

Notations

  • The lowercase letters a to i are the bits that are copied to the decimal representation.
  • 0 and 1 are the exact bits in the input or output bit patterns.
  • x bits are ignored in the conversion.

Conversion table

The following is the conversion table from 10 bits of DPD to three decimal digits. Each decimal digit is represented as 4-bit binary (BCD). Both sides are written left to right from the most significant digit to the least.

Bits                 =>  Decimal         (Digit range)
a b c d e f 0 g h i  =>  0abc 0def 0ghi  (0-7) (0-7) (0-7)
a b c d e f 1 0 0 i  =>  0abc 0def 100i  (0–7) (0–7) (8–9)
a b c g h f 1 0 1 i  =>  0abc 100f 0ghi  (0–7) (8–9) (0–7)
g h c d e f 1 1 0 i  =>  100c 0def 0ghi  (8–9) (0–7) (0–7)
g h c 0 0 f 1 1 1 i  =>  100c 100f 0ghi  (8–9) (8–9) (0–7)
d e c 0 1 f 1 1 1 i  =>  100c 0def 100i  (8–9) (0–7) (8–9)
a b c 1 0 f 1 1 1 i  =>  0abc 100f 100i  (0–7) (8–9) (8–9)
x x c 1 1 f 1 1 1 i  =>  100c 100f 100i  (8–9) (8–9) (8–9)

Task

Convert 10 bits of DPD to 3 digits of decimal.

Test cases

DPD           Decimal
0000000101    005
0001100011    063
0001111001    079
0000011010    090
0001011110    098
1010111010    592
0011001101    941
1100111111    879
1110001110    986
0011111111    999
1111111111    999  * Output is same regardless of the `x` bits

Input

The default input format is a list of 10 bits. The bits should follow the exact order above, or the reverse of it. You may choose to use an equivalent string or integer representation instead. Unlike my other challenges, reordering or using nested structures is not allowed.

For the input [1, 1, 0, 0, 0, 1, 0, 1, 0, 0], the following formats are allowed:

  • List of bits: [1, 1, 0, 0, 0, 1, 0, 1, 0, 0]
  • String: "1100010100"
  • Binary integer: 788 or 0b1100010100
  • Decimal integer: 1100010100
  • Reversed: [0, 0, 1, 0, 1, 0, 0, 0, 1, 1] and reversed in any other formats above

The following formats are NOT allowed:

  • Arbitrary reordering of bits: [0, 0, 0, 0, 0, 1, 1, 1, 0, 1]
  • Nested structures: [[1, 1, 0], [0, 0, 1], [0, 1, 0, 0]] or [0b110, 0b001, 0b0100]

Output

The default output format is a list of 3 decimal digits. Each digit should be represented as 0 to 9, either an integer or a character. As in input, you can choose string or integer representation. If you choose integer representation, leading zeroes can be omitted.

Scoring & winning criterion

Standard rules apply. The shortest program or function in bytes for each language wins.

Bubbler

Posted 2018-11-22T00:04:42.183

Reputation: 16 616

Answers

12

JavaScript (ES6), 112 bytes

All credit for this shorter version goes to @nwellnhof.

Takes input as an integer. Returns an array of three decimal digits.

n=>[(x=n>>4,y=x>>3,q=n/2&55,p=q%8)>5&&q-39?8|y&1:y,(p&5^5?x&6:q-23?8:y&6)|x&1,(p<5?p*2:p<6?x&6:p%q<7?y&6:8)|n&1]

Try it online!


JavaScript (ES6), 118 117 bytes

Takes input as an integer. Returns an array of three decimal digits.

n=>[(x=n>>4&7,y=n>>7,p=n/2&7)>5&&p<7|x/2^2?8|y&1:y,(p<7?p-5?x:8:x/2^1?8:y&6)|x&1,(p<5?p*2:p<6?x&6:p<7|x<2?y&6:8)|n&1]

Try it online!

How?

Instead of trying to apply the 'official' algorithm, this code is based on some kind of reverse-engineering of the patterns that can be found in the expected results.

Given the input integer \$n\$, we compute:

$$\begin{align}&x=\left\lfloor\frac{n}{16}\right\rfloor \bmod 8\\ &y=\left\lfloor\frac{n}{128}\right\rfloor\\ &p=\left\lfloor\frac{n}{2}\right\rfloor \bmod 8\end{align} $$

Example: first digit (hundreds)

x     | 0                | 1                | 2                | 3               
n & 1 | 0101010101010101 | 0101010101010101 | 0101010101010101 | 0101010101010101
p     | 0011223344556677 | 0011223344556677 | 0011223344556677 | 0011223344556677
------+------------------+------------------+------------------+-----------------
y = 0 | 0000000000008888 | 0000000000008888 | 0000000000008888 | 0000000000008888
y = 1 | 1111111111119999 | 1111111111119999 | 1111111111119999 | 1111111111119999
y = 2 | 2222222222228888 | 2222222222228888 | 2222222222228888 | 2222222222228888
y = 3 | 3333333333339999 | 3333333333339999 | 3333333333339999 | 3333333333339999
y = 4 | 4444444444448888 | 4444444444448888 | 4444444444448888 | 4444444444448888
y = 5 | 5555555555559999 | 5555555555559999 | 5555555555559999 | 5555555555559999
y = 6 | 6666666666668888 | 6666666666668888 | 6666666666668888 | 6666666666668888
y = 7 | 7777777777779999 | 7777777777779999 | 7777777777779999 | 7777777777779999

x     | 4                | 5                | 6                | 7               
n & 1 | 0101010101010101 | 0101010101010101 | 0101010101010101 | 0101010101010101
p     | 0011223344556677 | 0011223344556677 | 0011223344556677 | 0011223344556677
------+------------------+------------------+------------------+-----------------
y = 0 | 0000000000008800 | 0000000000008800 | 0000000000008888 | 0000000000008888
y = 1 | 1111111111119911 | 1111111111119911 | 1111111111119999 | 1111111111119999
y = 2 | 2222222222228822 | 2222222222228822 | 2222222222228888 | 2222222222228888
y = 3 | 3333333333339933 | 3333333333339933 | 3333333333339999 | 3333333333339999
y = 4 | 4444444444448844 | 4444444444448844 | 4444444444448888 | 4444444444448888
y = 5 | 5555555555559955 | 5555555555559955 | 5555555555559999 | 5555555555559999
y = 6 | 6666666666668866 | 6666666666668866 | 6666666666668888 | 6666666666668888
y = 7 | 7777777777779977 | 7777777777779977 | 7777777777779999 | 7777777777779999

Algorithm:

  • If \$p<6\$, we have \$d=y\$
  • If \$p=6\$, we have \$d=8+(y\bmod2)\$
  • If \$p=7\text{ AND }(x<4\text{ OR }x>5)\$, we have \$d=8+(y\bmod2)\$
  • If \$p=7\text{ AND }(x=4\text{ OR }x=5)\$, we have \$d=y\$

As JS code:

p > 5 && p < 7 | x / 2 ^ 2 ? 8 | y & 1 : y

Arnauld

Posted 2018-11-22T00:04:42.183

Reputation: 111 334

1

Your approach is similar to my C answer which uses another temporary variable. After golfing my initial C solution a bit more, a port to JavaScript results in 112 bytes.

– nwellnhof – 2018-11-23T11:19:32.763

10

Python 3, 229 ... 97 96 bytes

lambda a:[[a&6,a>>4&6,a>>7&6,8][b"  eW7B]Oys"[~a&8or~a&6or~6|a>>4]%x&3]|a>>x%9&1for x in[7,4,9]]

Try it online!

-4 bytes by @xnor

-6 bytes by @nwellnhof

Formatted:

h = lambda a:[
    [a&6, a>>4&6, a>>7&6, 8][              List to take high bits from
        b"  eW7B]Oys"[                     10 char string; where to get high bits for
                                             indicator values 1-8. 0th,1st chars not used.
            ~a&8 or ~a&6 or ~6|a>>4]       Compute indicator (by @nwellnhof)
        %x&3]                              High bits of each digit
    | a >> x%9 & 1                         bitwise OR with low bit of each digit
    for x in [7,4,9]]

Explanation

Because I originally wanted to implement this in Jelly, I take a different approach from most answers here, which is simple and perhaps suited to a golfing language. Though the golfed function takes an integer, let the input as a bit list be [a0,a1,...,a9]. Then we can derive three values from the input

  • The low bits [a2,a5,a9]: These will always be the low bits of [d0,d1,d2] respectively.
  • The high bits [2*a0a1,2*a3a4,2*a7a8,8]: The high bits of each digit will be one of these.
  • The indicator bits, [a3,a4,a5,a7,a8], determining how to get the high bits of each digit. We compute the indicator (between 1 and 8) as follows:
    • If a5 == 0, the indicator is 8 (originally 0, but using 8 instead saves a byte)
    • If a3 nand a4, the indicator is 6 - 2*a3a4
    • Otherwise the indicator is 2*a7a8 + 1 (actually calculated as a negative number).

Then the nth digit can be elegantly computed as high_bits[arr[indicator][n]] | low_bits[n] by the table below, which is compressed into a string.

arr = [
    [0,1,2],
    [3,1,2],
    [1,3,2],
    [2,1,3],
    [2,3,3],
    [3,2,3],
    [3,3,2],
    [3,3,3]
]

lirtosiast

Posted 2018-11-22T00:04:42.183

Reputation: 20 331

1You can used a bytestring b"..." to replace converting with ord. – xnor – 2018-11-22T19:43:20.607

@nwellnhof Ha, I just found the same thing! Will credit you anyway. – lirtosiast – 2018-11-23T20:01:10.017

b"$>6;-/'?"[a&8and(~a&6or a>>4&6|1)] saves another four bytes. – nwellnhof – 2018-11-23T21:16:31.177

@nwellnhof I think a modulo chain is the way to go here, but if not yours would certainly work. – lirtosiast – 2018-11-23T21:53:50.523

9

JavaScript (Node.js), 126 119 117 112 111 bytes

(a,b,c,d,e,f,g,h,i,j)=>[(g&h&i+(b+=a*4+b,e+=d*4+e)!=5?8:b)+c,(g&i?h+e-3?8:b:e)+f,(g?h<i?e:h>i*e?b:8:h*4+i*2)+j]

Try it online!

-5 bytes thanks @tsh (and 2 by myself) So l can make more effort than I expected.

-2 more bytes using @tsh's technique!

-5 bytes thanks @Arnauld

-1 byte thanks @Neil

Input as a list of 10 bits (as 10 arguments), output as a list of 3 digits.

Shieru Asakoto

Posted 2018-11-22T00:04:42.183

Reputation: 4 445

1(!i|!d|e) -> i+l!=5; (d|e|!h) -> h+l!=1 – tsh – 2018-11-22T11:15:32.310

1(g?h-i|h&!e?h?b:e:8:h*4+i*2) -> (g?h<i?e:h>i*e?b:8:h*4+i*2) saves another byte. (I checked this time...) – Neil – 2018-11-24T02:04:19.160

8

C (gcc), 138 129 bytes

f(w){int t=w/2&55,s=t%8,v=w/16,u=v/8;w=((s<6|t==39?u:8|u%2)*10+v%2+(s&5^5?v&6:t-23?8:u&6))*10+w%2+(s<5?s*2:s<6?v&6:s%t<7?u&6:8);}

Try it online!

First extracts some bits into variables s and t, so that the eight rows of the conversion table can be identified by:

1.  s < 4              u v w¹
2.  s = 4              u v 8¹
3.  s = 5              u 8 v
4.  s = 6              8 v u
5.  s = 7, t =  7      8 8 u
6.  s = 7, t = 23      8 u 8
7.  s = 7, t = 39      u 8 8
8.  s = 7, t = 55      8 8 8

¹ Can be computed with s*2

Then sets up u and v with divisions (right shifts), so that u, v and the input w contain the lower three BCD bits in positions 0-2. The rest is bit shuffling depending on s and t. Two notable tricks are:

s&5^5  // Rows 1, 2 and 4.
s%t<7  // Rows 1-5.

A port of Shieru Asakoto's Javascript solution is only 124 bytes:

f(a,b,c,d,e,f,g,h,i,j){a=(((g&h&i+(b+=a*4+b,e+=d*4+e)!=5?8:b)+c)*10+(g&i?h+e-3?8:b:e)+f)*10+(g?h-i|h&!e?h?b:e:8:h*4+i*2)+j;}

Try it online!

nwellnhof

Posted 2018-11-22T00:04:42.183

Reputation: 10 037

I think it can be shortened to: f(b){int a=b/2%8,e=b&110,c=b/16,d=c/8;b=10*(10*(d%2|(6>a|78==e?d:8))+c%2+(3<a&a%2?e-46?8:d&6:c&6))+b%2+(4>a?b&6:a-5?a-6&&e-14?8:d&6:c&6)}; – MCCCS – 2018-11-22T13:59:23.447

@MCCCS Your code seems to be 138 bytes as well. – nwellnhof – 2018-11-22T16:36:41.783

5

Retina 0.8.2, 191 181 bytes

(...)(...)
:$1,$2;
..(.),11(.);111
100$1,100$2;100
(10|(..)(.,)01)(.);111
100$3$2$4;100
(..)(.),(00.);111
100$2,1$3;0$1
(..)((.{5});110|(.);101)
100$3$4;$1
1
01
+`10
011
.0+(1*)
$.1

Try it online! Link includes test cases. Edit: Saved 10 byte by not padding digits to 4 bits except where necessary. Explanation:

(...)(...)
:$1,$2;

Insert separators so that each digit can be converted to decimal separately. This effectively handles the first two cases in the conversion table.

..(.),11(.);111
100$1,100$2;100

Handle the last (eighth) case in the conversion table.

(10|(..)(.,)01)(.);111
100$3$2$4;100

Handle the sixth and seventh cases in the conversion table.

(..)(.),(00.);111
100$2,1$3;0$1

Handle the fifth case in the conversion table.

(..)((.{5});110|(.);101)
100$3$4;$1

Handle the third and fourth cases in the conversion table.

1
01
+`10
011
.0+(1*)
$.1

Perform binary to decimal conversion.

Neil

Posted 2018-11-22T00:04:42.183

Reputation: 95 035

5

Ruby, 153 ... 119 117 bytes

->n{n+=n&896;a,b,c=n&1536,n&96,n&14;"%x"%n+=c<9?0:2036+[b/16-b-1918,r=a>>8,[r+126,a/16-26,a-1978,38][b/32]-a][c/2-5]}

Try it online!

How it works:

->n{n+=n&896;

This is the starting point: convert to BCD by shifting 3 bits to the left, which works for most of the patterns.

a,b,c=n&1536,n&96,n&14;

Get the middle bits of each nibble (and one extra bit of the third nibble, but mask the least significant bit).

"%x"%n+=c<9?0

If the third digit is less than 10 (less than 9 because we never cared for the LSB anyway), we're set: this is plain BCD, we can output the hex without changing anything

:2036+[b/16-b-1918,r=a>>8,[r+126,a/16-26,a-1978,38][b/32]-a][c/2-5]}

Otherwise do some black magic by shifting bits around and adding magic numbers until we get the result we want.

G B

Posted 2018-11-22T00:04:42.183

Reputation: 11 099

5

Jelly, 51 48 40 39 bytes

&\‘f4;s3ɓạ4ḅ-œ?µ/Ḥ
“MY-€-Y¤©¡‘Dịs3Ḅç+ƭ/

Try it online!

Algorithm

With the exception of list indices, all integers in this section are written in binary.

Given input \$\alpha\beta\gamma\delta\varepsilon\zeta\eta\theta\iota\kappa\$, we first construct the arrays \$[\eta\eta, \theta\iota, \delta\varepsilon]\$, \$[\alpha\beta, \delta\varepsilon, \theta\iota]\$, and \$[\gamma, \zeta, \kappa]\$.

  1. If \$\eta\eta = 00\$, all three output digits are low (\$0000\$ to \$0111\$).
  2. If \$\eta\eta = 11\$ but \$\theta\iota < 11\$, exactly one output digit is high (\$1000\$ or \$1001\$).
  3. If \$\eta\eta = \theta\iota = 11\$ but \$\delta\varepsilon < 11\$, exactly two output digits are high.
  4. If \$\eta\eta = \theta\iota = \delta\varepsilon = 11\$, all three output digits are high.

If we count the number of leading \$11\$'s \$[\eta\eta, \theta\iota, \delta\varepsilon]\$, create an array of a matching number of \$100\$'s, concatenate it with \$[\alpha\beta, \delta\varepsilon, \theta\iota]\$, and split the result into subarrays of length three, we get the following results in each case.

  1. \$[[\alpha\beta, \delta\varepsilon, \theta\iota]]\$
  2. \$[[100, \alpha\beta, \delta\varepsilon], [\theta\iota]]\$
  3. \$[[100, 100, \alpha\beta], [\delta\varepsilon, \theta\iota]] = [[100, 100, \alpha\beta], [\delta\varepsilon, 11]]\$
  4. \$[[100, 100, 100], [\alpha\beta, \delta\varepsilon, \theta\iota]] = [[100, 100, 100], [\alpha\beta, 11, 11]]\$

In the first and last case, we just have to zip the first array with \$[\gamma, \zeta, \kappa]\$, yielding \$[\alpha\beta\gamma, \delta\varepsilon\zeta, \theta\iota\kappa]\$ in the first case and \$[100\gamma, 100\zeta, 100\kappa]\$ in the last.

The remaining two cases are similar, but the arrays \$[100, \alpha\beta, \delta\varepsilon]\$ and \$[100, 100, \alpha\beta]\$ have to be reordered, according on the values of \$[\theta\iota]\$ and possibly \$\delta\varepsilon\$.

In the second case, the six permutations of \$[100, \alpha\beta, \delta\varepsilon]\$ are \$[100, \alpha\beta, \delta\varepsilon]\$, \$[100, \delta\varepsilon, \alpha\beta]\$, \$[\alpha\beta, 100, \delta\varepsilon]\$, \$[\alpha\beta, \delta\varepsilon, 100]\$, \$[\delta\varepsilon, 100, \alpha\beta]\$, and \$[\delta\varepsilon, \alpha\beta, 100]\$.

By computing \$100-\theta\iota\$, we map \$00\$, \$01\$, and \$10\$ to four, three and two, selecting the permutations \$[\alpha\beta, \delta\varepsilon, 100]\$, \$[\alpha\beta, 100, \delta\varepsilon]\$, and \$[100, \delta\varepsilon, \alpha\beta]\$.

After zipping the result with \$[\gamma, \zeta, \kappa]\$, we get \$[\alpha\beta\gamma, \delta\varepsilon\zeta, 100\kappa]\$, \$[\alpha\beta\gamma, 100\zeta, \delta\varepsilon\kappa]\$, or \$[100\gamma, \delta\varepsilon\zeta, \alpha\beta\kappa]\$.

In the third case, the permutations (with duplicates) of \$[100, 100, \alpha\beta]\$ are \$[100, 100, \alpha\beta]\$, \$[100, \alpha\beta, 100 ]\$, \$[100, 100, \alpha\beta]\$, \$[100, \alpha\beta, 100 ]\$, \$[\alpha\beta, 100, 100]\$ and \$[\alpha\beta, 100, 100]\$.

By computing \$(100 - \theta\iota) - (100 - \delta\varepsilon) = \delta\varepsilon - \theta\iota = \delta\varepsilon - 11\$, we map \$00\$, \$01\$, and \$10\$ to three, four, and five modulo six, selecting the permutations \$[100, 100, \alpha\beta]\$, \$[100, \alpha\beta, 100 ]\$, and \$[\alpha\beta, 100, 100]\$.

After zipping the result with \$[\gamma, \zeta, \kappa]\$, we get \$[100\gamma, 100\zeta, \alpha\beta\kappa]\$, \$[100\gamma, \alpha\beta\zeta, 100\kappa]\$, or \$[\alpha\beta\gamma, 100\zeta, 100\kappa]\$.

Code

“MY-€-Y¤©¡‘Dịs3Ḅç+ƭ/  Main link. Argument: A (array of 10 bits)

“MY-€-Y¤©¡‘           Array literal; yield [77, 89, 45, 12, 45, 89, 3, 6, 0].
           D          Decimal; yield
                      [[7,7], [8,9], [4,5], [1,2], [4,5], [8,9], [3], [6], [0]].
            ị         Retrieve the elements of A at those indices.
                      Indexing is 1-based and modular, so 1 is the first index, while
                      0 is the last.
             s3       Split the results 2D array of bits into chunks of length 3.
               Ḅ      Convert the 9 arrays of bits from binary to integer.
                ç+ƭ/  Reduce the resulting array (length 3) once by the helper link,
                      then by addition.


&\‘f4;s3ɓạ4ḅ-œ?µ/Ḥ    Helper link. Arguments: B, C (arrays of three integers each)

&\                    Cumulatively reduce B by bitwise AND.
  ‘                   Increment the results by 1.
   f4                 Filter; keep only integers equal to 4.
     ;                Concatenate the result with C.
      s3              Split the result into (one or two) chunks of length 3.
        ɓ      µ/     Reduce the array of chunks by the following chain.
         ạ4               Take the absolute difference of the integers in the right
                          chunk and the integer 4.
           ḅ-             Convert the resulting array from base -1 to integer, i.e.,
                          map [x] to n = x and [x, y] to n = y - x.
             œ?           Take the n-th permutation of the left chunk.
                 Ḥ    Unhalve; multiply the resulting integers by 2.

Dennis

Posted 2018-11-22T00:04:42.183

Reputation: 196 637

2

Python 2, 157 bytes

lambda a,b,c,d,e,f,g,h,i,j:[c+[a*4+b*2,8][g*h*~(d*~e*i)],f+[d*4+e*2,8,a*4+b*2][g*i+(d<e)*g*i*h],j+[h*4+i*2,[8,[a*4+b*2,d*4+e*2][h<i]][h^i or(h&i-(d|e))]][g]]

Try it online!

TFeld

Posted 2018-11-22T00:04:42.183

Reputation: 19 246

2

Clean, 238 ... 189 bytes

-2 bytes thanks to Neil

import StdEnv
$a b c d e f g h i j=100*(c+2*b+4*a)+10*(f+2*e+4*d)+j+2*i+4*h-2*(h*(99*b+198*a-394)+i*(9*e+18*d+h*(e+2*d-4+(b+2*a-4)*(1-10*e-100*d+110*e*d))-35)-4)*g+0^(e+d)*(2*b+4*a-8*i*h*g)

Try it online!

Takes a 'list' of 10 bits in the form of 10 arguments, using a direct formula to compute the result.

Οurous

Posted 2018-11-22T00:04:42.183

Reputation: 7 916

In i*(9*e+19*d+i*...), that second i* looks unnecessary. – Neil – 2018-11-23T01:26:17.487

@Neil You're right, it is, thanks. – Οurous – 2018-11-23T01:29:53.350

1

Perl 5, 195 bytes

sub f{$n=shift;@p=((map{($n>>$_&3)*2}(8,5,1)),8);for(16390,28935,29005,227791,29108,225788,226803,228863){return 2*$n&256|$n&17|$p[$_>>4&3]<<8|$p[$_/4&3]<<4|$p[$_&3]if($_>>12&$n/2)==($_>>6&63);}}

Try it online

I know 195 bytes is far too much for this contest, but I had no idea how to further compress the Perl code. Suggestions?

Explanation of the code

In a more readable version, the code intention should become apparent:

sub dpd {
  my $n = shift;
  my $v=2*($n&128)|$n&17;
  my @p=((map{($n>>$_&3)*2}(8,5,1)),8);
  for (16390,28935,29005,227791,29108,225788,226803,228863) {
    return $v |$p[$_>>4&3]<<8|$p[$_>>2&3]<<4|$p[$_&3]
      if(($_>>12&$n/2)==($_>>6&63));
  }
}

In the rules for DPD encoding, each line is encoded into a 18 bit value, segmentation into (6,6,(2,2,2)) bits.

  • The first 6 bits are an appropriate bit mask for the bits 1 (=h) to 6 (=d) of the input (bit 4 = f is redundant, but it simplifies the evaluation code to have it included).
  • The next 6 bits are the value bits for this bit mask. The values are checked on all places where the bit mask has a 1 value.
  • The following 3*2 bits contain the indices for the array @p for the 3-bit sequences which are to spliced into bits 11-9, 7-5 and 3-1 of the result.
  • The array @p is constructed from bits 9-8, 6-5, 3-2 of the input, and the number 8 as fourth member
  • The bits at position 7,4 and 0 of the input are transferred directly into bits 8,4 and 0 of the result.

For example, the first number in the list, 16390, which is 100000000000110 as a bit field, carries the following information:

000100 : bit mask says: only consider bit 3 of the input
000000 : bit values say: bit 3 should be 0
00     : use '0ab' as higher bits of first digit
01     : use '0de' as higher bits of second digit
10     : use '0gh' as higher bits of third digit

rplantiko

Posted 2018-11-22T00:04:42.183

Reputation: 111

1

05AB1E, 84 bytes

Port of KimOyhus' answer to 05AB1E.

•4’7þ2Ô€iΘEuĆΣk4Ѐ:ΘΛs‡CaΔʒì₁3¶rdiMß¡þи иø-˜)Â∍DY—WûQ@—Mā}Γ¤ÒÙ]p•44в2ôvÐyèP≠«}4ôC3.£

Try it online!

Rough explanation:

•yadayada•44в2ô   # encoded list of nand gates
v                 # for each gate
 ÐyèP≠            # compute the output of the gate
      «           # append it to the input
       }          # end of the loop
4ô                # split the list of bits in groups of 4
  C               # convert each from binary to decimal
   3.£            # keep the last 3 numbers
                  # implicit output

Grimmy

Posted 2018-11-22T00:04:42.183

Reputation: 12 521

0

05AB1E, 104 103 101 bytes

•3γã•S£©4èUXтÌ‹XSPVY®2èDˆTQ*~i0®нëт}®1èY¯`*i0®нëY_Xт>Ê*i0¯`ëт]®3èY¯`_*X110Q~i0®нëXт›iYiтë0¯`ëX]®θJ4ôC

Definitely not the right language for this kind of challenge, but ah well..
Input as string, output as list of three digits.

Try it online or verify all test cases.

Explanation:

We have the following eight scenarios to consider:

     1st 2nd 3rd 4th 5th 6th                          1st digit    2nd digit    3rd digit
1.   ab  c   de  f   0gh i   →   0abc 0def 0ghi   →   '0'+1st 2nd  '0'+3rd 4th  5th     6th
2.   ab  c   de  f   100 i   →   0abc 0def 100i   →   '0'+1st 2nd  '0'+3rd 4th  5th     6th
3.   ab  c   gh  f   101 i   →   0abc 100f 0ghi   →   '0'+1st 2nd  '100'   4th  '0'+3rd 6th
4.   gh  c   de  f   110 i   →   100c 0def 0ghi   →   '100'   2nd  '0'+3rd 4th  '0'+1st 6th
5.   gh  c   00  f   111 i   →   100c 100f 0ghi   →   '100'   2nd  '100'   4th  '0'+1st 6th
6.   de  c   01  f   111 i   →   100c 0def 100i   →   '100'   2nd  '0'+1st 4th  '100'   6th
7.   ab  c   10  f   111 i   →   0abc 100f 100i   →   '0'+1st 2nd  '100'   4th  '100'   6th
8.   xx  c   11  f   111 i   →   100c 100f 100i   →   '100'   2nd  '100'   4th  '100'   6th

I first split the (implicit) input into chunks of size [2,1,2,1,3,1] and store that list in the register:

•3γã•     # Push compressed integer 212131
     S    # Convert it to a list of digits
      £   # Split the (implicit) input in chunks of that size
       ©  # Store it in the register (without popping)

See this 05AB1E tip of mine (section How to compress large integers?) to understand why •3γã• is 212131

Now we're first going to built the 0s and 1s for the first digit of the output. Scenarios 1,2,3,7 use '0'+1st+2nd; and scenarios 4,5,6,8 use '100'+2nd:

4è                  # Take the 5th item of the list
  U                 # Pop and store it in variable `X`
XтÌ‹                #  Check if `X` is below 102
                ~   # OR
   XSP              #  `X` is equal to 111
      VY            #  And store that result in variable `Y`
               *    #  and
        ®2è         #  Get the 3rd item from the list of the register
           Dˆ       #  Push it to the global array
             TQ     #  And check if it's equal to 10
i                   # If the combined check above is truthy (exactly 1):
 0                  #  Push 0 to the stack
 ®н                 #  Push the 1st item of the list to the stack
ë                   # Else:
 т                  #  Push 100 to the stack
}                   # Close the if-else
®1è                 # And push the 2nd item of the list to the stack

Then we're going to built the 0s and 1s for the second digit of the output. Scenarios 1,2,4 use '0'+3rd+4th; scenarios 3,5,7,8 use '100'+4th; and scenario 6 uses '0'+1st+4th:

Y                # Push `Y` (check if `X` equals 111)
   *             # and
 ¯`              # Push the item from the global array (3rd item of the list)
i                # If both checks above are truthy (exactly 1):
 0               #  Push 0 to the stack
 ®н              #  Push the 1st item of the list to the stack
ë                # Else:
 Y_              #  Push inverted `Y` (check if `X` does NOT equal 111)
       *         #  and
   Xт>Ê          #  Check if `X` (5th item of the list) does NOT equal 101
 i               #  If both checks above are truthy (exactly 1):
  0              #   Push 0 to the stack
  ¯`             #   Push the item from the global array (3rd item of the list)
 ë               #  Else:
  т              #   Push 100 to the stack
]                # Close both if-else cases
®3è              # And push the 4th item of the list to the stack

Then we're going to built the 0s and 1s for the third digit of the output. Scenarios 1,2 use 5th+6th; scenario 3 uses '0'+3rd+6th; scenarios 4,5 use '0'+1st+6th; and scenarios 6,7,8 use '100'+6th:

Y           #  Push `Y` (check if `X` equals 111)
    *       #  and
 ¯`_        #  Check if the item from the global array (3rd item of the list) is exactly 0
         ~  # OR
    X110Q   #  Check if `X` (5th item of the list) equals 110
i           # If the combined check above is truthy (exactly 1):
 0          #  Push 0 to the stack
 ®н         #  Push the 1st item of the list to the stack
ë           # Else:
 Xт›i       #  If `X` (5th item of the list) is larger than 100 (so 101/110/111):
     Yi     #   If `Y` (if `X` equals 111):
       т    #    Push 100 to the stack
      ë     #   Else:
       0    #    Push 0 to the stack
       ¯`   #    Push the item from the global array (3rd item of the list)
    ë       #  Else:
     X      #   Push `X` (5th item of the list) to the stack
]           # Close all if-else cases
®θ          # And push the last (6th) item of the list to the stack

Now we have all 0s and 1s on the stack, so we can convert it to the three output digits:

J     # Join the entire stack together
 4ô   # Split it into parts of size 4
   C  # Convert each part from binary to an integer (and output implicitly)

Kevin Cruijssen

Posted 2018-11-22T00:04:42.183

Reputation: 67 575