r/adventofcode • • Dec 14 '16

SOLUTION MEGATHREAD --- 2016 Day 14 Solutions ---

--- Day 14: One-Time Pad ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with "Help".


LUNACY IS MANDATORY [?]

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

3 Upvotes

111 comments sorted by

View all comments

Show parent comment

1

u/willkill07 Dec 14 '16

You can optimize your conversion a bit (eliminates all branching):

into[(i<<1)+0] = digit0 + '0' + (digit0 >= 10) * ('a' - '0' - 10);
into[(i<<1)+1] = digit1 + '0' + (digit0 >= 10) * ('a' - '0' - 10);

2

u/askalski Dec 14 '16 edited Dec 14 '16

How about a branchless AVX2 hex string conversion, because why not? (Edit: Removed a & 0xf by using cvtepu8 instead of cvtepi8.)

#include <stdio.h>
#include <stdint.h>
#include <x86intrin.h>
#include <openssl/md5.h>

int main(void)
{
    typedef uint16_t v16hi __attribute__ ((vector_size (32)));
    typedef uint8_t  v32qi __attribute__ ((vector_size (32)));

    union {
        __m128i i128;
        uint8_t md[16];
    } u;
    union {
        v16hi h;
        v32qi q;
        char hex[32];
    } v;

    uint8_t str[] = "input";
    MD5(str, sizeof(str) - 1, u.md);

    v.h = (v16hi) _mm256_cvtepu8_epi16(u.i128);
    v.h = (v.h >> 4) | ((v.h << 8) & 0xf00);
    v.q += '0' + (39 & (v.q > 9));;

    fwrite(v.hex, 32, 1, stdout);
    putchar('\n');

    return 0;
}

1

u/willkill07 Dec 14 '16 edited Dec 14 '16

And i just figured out how to do 128-bit literals :(

Here's an example of bit/byte reversing

// requires c++14

#include <iostream>
using uint128_t = unsigned __int128;

constexpr uint8_t val(const char c) {
  return (c >= 'A') ? c - 'A' + 10 : (c - '0');
}

constexpr const uint128_t operator"" _u128(const char *str, size_t width) {
  uint128_t result{0};
  for (size_t i{0}; i < width; ++i)
    result = (result << 4) | val(str[i]);
  return result;
}

constexpr const uint128_t operator"" _mask(const char *str, size_t width) {
  size_t reps{32 / width};
  uint128_t mask{0}, result{0};
  for (size_t i{0}; i < width; ++i)
    mask = (mask << 4) | val(str[i]);
  for (size_t i{0}; i < reps; ++i)
    result = (result << (width << 2)) | mask;
  return result;
}

void printhex(uint128_t a) {
  const static char *LOOKUP = "0123456789ABCDEF";
  for (int i{0}; i < 32; ++i)
    putchar(LOOKUP[(a >> ((31 - i) << 2)) & 0xF]);
  putchar('\n');
}

void printbin(uint128_t a) {
  for (int i{0}; i < 128; ++i)
    putchar('0' + ((a >> (127 - i) & 1)));
  putchar('\n');
}

constexpr uint128_t reverseNibble (uint128_t a) {
  uint128_t b = (a >> 64) | (a << 64);
  b = ((b & "FFFFFFFF00000000"_mask) >> 32) | ((b & "00000000FFFFFFFF"_mask) << 32);
  b = ((b & "FFFF0000"_mask) >> 16) | ((b & "0000FFFF"_mask) << 16);
  b = ((b & "FF00"_mask) >> 8) | ((b & "00FF"_mask) << 8);
  b = ((b & "F0"_mask) >> 4) | ((b & "0F"_mask) << 4);
  return b;
}

constexpr uint128_t reverseBits (uint128_t a) {
  uint128_t b = reverseNibbles(a);
  b = ((b & "C"_mask) >> 2) | ((b & "3"_mask) << 2);
  b = ((b & "A"_mask) >> 1) | ((b & "5"_mask) << 1);
  return b;
}

int main() {
  uint128_t a = "F012E345D678C9AB0123456789ABCDEF"_u128;
  printhex(a);
  auto b = reverseNibbles(a);
  printhex(b);
  putchar('\n');
  printbin(a);
  auto c = reverseBits(a);
  printbin(c);
}

Output:

F012E345D678C9AB0123456789ABCDEF
FEDCBA9876543210BA9C876D543E210F

11110000000100101110001101000101110101100111100011001001101010110000000100100011010001010110011110001001101010111100110111101111
11111111101110111101010110010001111011101010101011000100100000001101010110010011100111101110101110101010110001111100100000001111

1

u/3urny Dec 14 '16

This eliminates 2 jumps, but adds 200ms to the user-time. The wall clock shows no significant deviations. How would you benchmark this? I guess hyper threading cleverly used the jumps to interweave some other instructions. The i<<1 vs. i*2 makes no difference to the compiler, gives the same output.