Code Golf Measurer © 2019

20

1

Code Golf Measurer © 2019

Hexdumps used with xxd look something like this:

00000000: 666f 6f20 6261 7220 7370 616d 2065 6767  foo bar spam egg
00000010: 730a                                     s.

Your task is to convert a hexdump in this form in to the number of bytes used.

Rules:

  • Usual loopholes forbidden.
  • This is , so shortest valid answer in bytes wins.
  • You may or may not include the newline at the end of text (0a). This means that if the hexdump ends in a newline (0a), that input may have it's output reduced by one.
  • An empty input (literally nothing: empty list/string/etc.) must output 0.
  • Input can be taken in any form
  • Input will be valid ASCII with no control characters
  • The input must contain the whole hexdump

Test cases:

00000000: 4865 6c6c 6f2c 2077 6f72 6c64 2120 4865  Hello, world! He
00000010: 6c6c 6f2c 2077 6f72 6c64 210a            llo, world!.

returns 28 or 27

00000000: 0a                                       .

returns 1 or 0

00000000: 6368 616c 6c65 6e67 650a                 challenge.

returns 10 or 9

00000000: 4865 6c6c 6f2c 2077 6f72 6c64 21         Hello, world!

returns 13


returns 0 (This is literally nothing: empty list/string/etc.)

Explanations would be nice for non-standard languages.

gadzooks02

Posted 2019-10-07T16:58:35.300

Reputation: 527

May we assume the input is ascii? Can it contain control characters? What are the input rules? Some languages can't handle input over multiple lines. Can we pad the last line so that all lines are equally long? Why include the blank input? – Stewie Griffin – 2019-10-07T17:03:08.467

@StewieGriffin Yes; no; yes; because if xxd is fed an empty string, it outputs nothing. – gadzooks02 – 2019-10-07T17:05:57.883

1@StewieGriffin You edited your comment while I was answering, so here's an expansion: Yes; No; it can be input however you want, provided the whole dump is included; See last answer; Yes; Because if xxd is fed an empty string, it outputs nothing – gadzooks02 – 2019-10-07T17:24:47.497

1@JonathanAllan Oh yes, well spotted. – gadzooks02 – 2019-10-07T17:41:12.277

1Counting a hexdump format that didn't include the ASCIIfied data at the right might be interesting. Everyone's going with just stripping the hex part and byte-counting the rest. If the challenge was to do this given only the last line of hexdump, that would force parsing the hex number (the position) as well as counting the number of hex digits on that line. (Like I do by hand when looking at objdump disassembly or nasm listings for machine-code answers.) I guess I should post that in the sandbox... – Peter Cordes – 2019-10-08T18:29:25.450

@PeterCordes Interesting idea! When you do, link it here; I'd definitely upvote it :). – gadzooks02 – 2019-10-08T18:34:24.743

May I assume the xxd dump use UNIX style line ending (LF)? An Windows version xxd (xxd V1.10 27oct98 by Juergen Weigert (Win32), comes with Vim) does output with CRLF line endings. – tsh – 2019-10-10T06:41:00.833

@tsh Any consistent specification. (So yes, but it could be either) – gadzooks02 – 2019-10-10T16:06:38.763

Answers

12

Retina 0.8.2, 8 bytes

.{51}

.

Try it online! Explanation:

.{51}

Delete the first 51 characters of each line. (Lines can only have between 52 and 67 characters, so this always matches once per line.)

.

Count the remaining non-newline characters.

7 bytes if empty input did not have to be supported:

.{52}


Try it online! Explanation:

.{52}

Delete the first 52 characters of each line. (Lines can only have between 52 and 67 characters, so this always matches once per line.)


Count 1 more than the number of remaining characters (including newlines).

Neil

Posted 2019-10-07T16:58:35.300

Reputation: 95 035

7 with &`.{52}.

– Grimmy – 2019-10-08T14:35:51.043

@Grimy That's just... awesome... you must post that as your own answer. – Neil – 2019-10-08T14:40:47.237

I didn't notice it at first but actually there's already an almost identical answer.

– Grimmy – 2019-10-08T14:54:35.653

11

tcsh, 12 bytes

xxd -r|wc -c

Try it online!

Krzysztof Szewczyk

Posted 2019-10-07T16:58:35.300

Reputation: 3 819

1Very robust and do exactly what OP want. – tsh – 2019-10-10T06:44:25.277

7

V (vim), 7 bytes

Î51x
Ø.

Try it online!

Explanation:

Î       " On every line...
 51x    "   Delete the first 51 characters
Ø.      " Count the number of remaining characters on any line

Hexdump:

00000000: ce35 3178 0dd8 2e                        .51x...

James

Posted 2019-10-07T16:58:35.300

Reputation: 54 537

6

Retina, 7 bytes

This counts the total number of single-line strings of length 52. It might be possible to do something similar to %52,`., but I failed to find a way to fix that.

w`.{52}

Try it online!

my pronoun is monicareinstate

Posted 2019-10-07T16:58:35.300

Reputation: 3 111

1Also: &`.{52} for Retina 0.8.2. – Grimmy – 2019-10-08T14:56:23.207

5

APL (Dyalog Extended), 18 bytes

Full program. Prompts for list of strings (i.e. of lists of characters).

2÷⍨≢∊(1↓≠⊆⊢)¨49↑¨⎕

Try it online!

 prompt

49↑¨ take the first 49 characters from each

( apply the following tacit function to each:

 the argument

 chop into runs of characters that are

 different from the padding character (space)

1↓ drop the first "word"

ϵnlist (flatten)

 tally

2÷⍨ divide by two

Adám

Posted 2019-10-07T16:58:35.300

Reputation: 37 779

4

Jelly, 5 bytes

Ẉ_51S

A monadic Link accepting a list of lines which yield the integer byte count.

Try it online!

How?

Ẉ_51S - Link: list of lists of characters, H
Ẉ     - length of each (line in H)
  51  - literal fifty-one
 _    - subtract (vectorises)
    S - sum

Jonathan Allan

Posted 2019-10-07T16:58:35.300

Reputation: 67 804

You can accept a list of lines. Also, would you care to add an explanation? – gadzooks02 – 2019-10-07T17:20:07.920

Ah cool, so is the empty input an empty list OR a list with a single empty line? (This should be made clear in the post since it's an edge-case.) – Jonathan Allan – 2019-10-07T17:26:55.810

It's and empty list/string/etc. I've just clarified this. – gadzooks02 – 2019-10-07T17:31:14.100

Thanks, explanation added now too. – Jonathan Allan – 2019-10-07T17:34:36.137

3

C (gcc), 64 55 bytes

r;s[];f(l){while(*s=0,gets(s),l=strlen(s))r+=l-51;l=r;}

Try it online!

9 bytes shaved off thanks to YSC!

Here is a more fragile version inspired by Arnauld's JavaScript solution which probably fails for long inputs:

C (gcc), 50 bytes

s[];f(l){l=read(0,s,1<<31);l=l?l/68*16+l%68-51:0;}

Try it online!

G. Sliepen

Posted 2019-10-07T16:58:35.300

Reputation: 580

You could use for. – Jonathan Frech – 2019-10-07T21:22:54.657

Also, s[] seems to me to be unstable undefined behaviour. – Jonathan Frech – 2019-10-07T21:24:38.913

159 bytes by changing return r to l=r – girobuz – 2019-10-08T03:40:54.477

@jonathan-french using a for loop doesn't seem to save any bytes. – girobuz – 2019-10-08T03:49:40.593

-3 bytes by making r a global: s[];r;f(l){while(*s=0,gets(s),l=strlen(s))r+=l-51;return r;} – YSC – 2019-10-08T08:48:32.357

2@JonathanFrech undefined behaviour is perfectly fine for code golf! – G. Sliepen – 2019-10-08T15:53:50.393

1@YSC Hm, but I think making r global violates the rule that if you define a function to do something (and it's not main), it should be possible to call it multiple times. – G. Sliepen – 2019-10-08T15:54:57.953

It doesn't matter here since f consumes all the input. Calling it a second time has no meaning (and as-is, returns 0 the second time).

– YSC – 2019-10-08T15:59:51.070

1

Anyway, r;s[];f(l){while(*s=0,gets(s),l=strlen(s))r+=l-51;l=r;}: 55 bytes :)

– YSC – 2019-10-08T16:04:00.930

@G.Sliepen I would disagree in the case of unstable undefined behavior, as it violates the general relaunchability requirement of your program. – Jonathan Frech – 2019-10-08T21:23:46.970

2@girobuz while() has the same number of bytes as for(;;). Thus ;while() is one byte longer than for(;;). – Jonathan Frech – 2019-10-08T21:25:51.137

3

Python 3, 48 46 bytes

lambda s:(len(s)or 51)+1-52*len(s.split('\n'))

Input is passed as a string to the function. The function increments the length of the input (including newlines), then subtracts 52 for each line.

Try it online

IFcoltransG

Posted 2019-10-07T16:58:35.300

Reputation: 191

2

From what I've seen, the f= can be moved in to the header as f=\, leaving just the lambda as code and saving two bytes: Try it online!

– gadzooks02 – 2019-10-08T16:03:01.680

@gadzooks02 Thanks, will edit. The f= is a habit I picked up from the Python discord server. – IFcoltransG – 2019-10-09T01:39:17.897

2

05AB1E, 8 6 bytes

€g51-O

Try it online!

Input as a list of strings.

€g     get lengths of each line
51-    subtract 51 from each
O      push the sum of the resulting list
       implicitly print

Dorian

Posted 2019-10-07T16:58:35.300

Reputation: 1 521

1Sorry, the input must contain the whole hexdump. (Yours skips the letters at the end of each line) – gadzooks02 – 2019-10-07T17:27:19.640

Oh, my bad. I made this on my smartphone. Didn't see there's something behind the hex codes. I made a correction. – Dorian – 2019-10-07T18:02:29.843

2

Japt -x, 5 bytes

Input as an array of lines.

®Ê-51

Try it

®Ê-51     :Implicit input of array
®         :Map
 Ê        :  Length
  -51     :  Subtract 51
          :Implicit output of sum of resulting array

Shaggy

Posted 2019-10-07T16:58:35.300

Reputation: 24 623

2

Perl 6, 18 bytes

{.join.comb-51*$_}

Try it online!

Anonymous Whatever lambda that takes a list of lines and returns the sum of the number of characters, subtracting 51 for each line

Jo King

Posted 2019-10-07T16:58:35.300

Reputation: 38 234

2

IBM/Lotus Notes Formula Language, 53 bytes

@Sum(@Length(@Explode(@Right(@Left(i;"  ");": ")))/2)

There is no TIO for Formula so here are screenshots of the test cases:

enter image description here enter image description here enter image description here enter image description here enter image description here

The formula is in the computed field that provides the value after "Returns".

Explanation

This is a good demonstration of the way that Formula will recursively apply a function to a list without needing a loop. The formula is in a computed field on the same form as editable input field `i'.

  1. Start in the middle. @Left and @Right allow a string delimiter or a number of characters to be used. We therefore search to the right of : and then to the left of the first occurrence of two spaces. Since Formula sees the newline as a list separator it will apply this to each line in the input.
  2. @Explode is Formula's equivalent of a split function and defaults to space, , or ;. Again it is applied to each line in the field but this time the results are combined into a single list.
  3. @Length will then be applied to each member of the list. In each case we divide it's return value by 2.
  4. @Sum the whole list and output the result.

ElPedro

Posted 2019-10-07T16:58:35.300

Reputation: 5 301

1Do you have a link to an interpreter (either online or downloaded)? – gadzooks02 – 2019-10-08T16:05:06.620

Unfortunately not. Formula is a proprietary language that is tied in to the IBM product (I did ask on Meta some time ago if this is allowed on this site and the answer was "yes" but not for Cops & Robbers challenges). Last time I looked, Domino Designer (which supports Formula) was still available as a free download from IBM. Unfortunately Windows only and as I run Linux at home I can't confirm. This answer was written during my lunch break as I am unfortunate enough to still support some legacy Notes apps and still have Notes on my machine at work :-) – ElPedro – 2019-10-08T18:15:50.870

1BTW, if you search Lotus Notes on this site you will find that I am the only guy who is stupid enough to try to golf in this language :-) – ElPedro – 2019-10-08T19:34:18.960

1

Red, 81 55 bytes

func[s][n: 0 foreach l s[n: n - 51 + length? l]max 0 n]

Try it online!

Takes the input as a list of strings.

Galen Ivanov

Posted 2019-10-07T16:58:35.300

Reputation: 13 815

1

JavaScript (ES6), 34 bytes

s=>(n=s.length)&&(n/68<<4)+n%68-51

Try it online!

Commented

s =>                // s = input string
  (n = s.length) && // n = length of s; return 0 right away if n = 0 (special case)
  (n / 68 << 4) +   // otherwise compute the number of full lines and multiply it by 16
  n % 68 - 51       // add the length of the last line minus 51

Arnauld

Posted 2019-10-07T16:58:35.300

Reputation: 111 334

1

JavaScript, 33 32 bytes

a=>a.map(x=>a=~~a+x.length-51)|a

Try it Online!

Saved 1 byte thanks to Arnauld.

Shaggy

Posted 2019-10-07T16:58:35.300

Reputation: 24 623

0

Befunge-98 (FBBI), 16 bytes

"c- cw|r- dxx"=@

Try it online!

Krzysztof Szewczyk

Posted 2019-10-07T16:58:35.300

Reputation: 3 819

0

Stax, 5 bytes

ôφß→ê

Run and debug it

recursive

Posted 2019-10-07T16:58:35.300

Reputation: 8 616

0

Zsh, 36 bytes

With zsh's default flags:

for l (${(f)1})((c+=$#l-52))
<<<$[c]

Try it online!

${(f)1} splits $1 on newlines, and discards empty lines. The $[ arithmetic expansion ] guards against the empty case, when the loop never sets $c.

Zsh, 28 bytes

With -o extendedglob:

<<<${#${(F)${(f)1}#?(#c52)}}

Try it online!

(f) Split on newlines, ${ #?(#c52)} remove leading 52 characters, (F) join on newlines so that counting is characterwise instead of listwise, ${# } count characters.

GammaFunction

Posted 2019-10-07T16:58:35.300

Reputation: 2 838

0

asm2bf, 135 bytes

Golfed version:

lbl 3
mov r2,51
lbl 1
in_ r1
dec r2
jz_ r1,4
jnz r2,1
lbl 2
in_ r1
jz_ r1,4
sub r1,10
jz_ r1,3
inc r3
jmp 2
lbl 4
out r3

Commented version:

lbl 3               ; Main loop - the kinda entry point

    mov r2, 51      ; Loop 51 times.
    lbl 1           ; Loop start.
        in_ r1      ; Read character
        dec r2      ; Decrement the loop accumulator.
        jz_ r1, 4   ; If zero was read, end.
        jnz r2, 1   ; If we still loop, loop again.

    lbl 2           ; Second loop, accumulating the result.
        in_ r1      ; Read a character.
        jz_ r1, 4   ; If character is zero, end the loop and print result.
        sub r1, 10  ; Decrement r1 by 10 for next check.
        jz_ r1, 3   ; If the character - 10 (the newline) is zero, jump to 3
        inc r3      ; Increment character read amount.
        jmp 2
lbl 4
    out r3          ; Print out the result as an ASCII character.

Krzysztof Szewczyk

Posted 2019-10-07T16:58:35.300

Reputation: 3 819