Skip to content

Instantly share code, notes, and snippets.

@jorinvo
Last active January 1, 2018 14:41
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorinvo/3d7f6a60fcede1863fa9f0788b8cc1b4 to your computer and use it in GitHub Desktop.
Save jorinvo/3d7f6a60fcede1863fa9f0788b8cc1b4 to your computer and use it in GitHub Desktop.
Dirty Money: Code Challenge https://jorin.me/dirtymoney

Follow the dirty money

A shady Internet business has been discovered.

The website has been made public by a whistle blower. We have enough evidence about the dirty deals they did. But to charge them we need to get hands on precise numbers about the transactions that happened on their platform.

Unfortunately no record of the transactions could be seized so far. The only hint we have is this one transaction:

fd0d929f-966f-4d1a-89cd-feee5a1c5347.json

What we need is the total of all transactions in Dollar. Can you trace down all other transactions and get the total?

Be careful to count each transaction only once, even if it is linked multiple times. You can use whatever tool works best.

Please share the total and your solution below!

Cheers, Jorin

@jorinvo

(For 2015 challenge see https://jorin.me/csv-challenge)

@chocolateboy
Copy link

chocolateboy commented Feb 6, 2017

@jorinvo No, it's still up (it's GitHub's own link shortener). It's git.io rather than git.to, though :-)

@matthew-hallsworth
Copy link

matthew-hallsworth commented Feb 7, 2017

Working PHP solution

<?php

$start = 'https://gist.githubusercontent.com/jorinvo/6f68380dd07e5db3cf5fd48b2465bb04/raw/c02b1e0b45ecb2e54b36e4410d0631a66d474323/fd0d929f-966f-4d1a-89cd-feee5a1c5347.json';
$original = fetch_and_decode($start);
$keyed_array = [];

$keyed_array = process_links($original, $keyed_array);

echo "transactions: " . count($keyed_array) . ", total: " . array_sum($keyed_array) . "\n";

//--

function process_links($payload, $keyed_array) {

  if (!array_key_exists($payload->id, $keyed_array)) {
    $keyed_array[$payload->id] = find_transaction($payload);
  }

  if (count($payload->links) == 0) return $keyed_array;

  foreach($payload->links as $link) {
    $link_fetched = fetch_and_decode($link);
    $keyed_array = process_links($link_fetched, $keyed_array);
  }

  return $keyed_array;
}


function fetch_and_decode($url) {
  return json_decode(file_get_contents($url));
}

function find_transaction($decoded_transaction) {
  preg_match('/\$(\d*(\.|,)\d*)/', $decoded_transaction->content, $matches);
  return(str_replace(',', '.', $matches[1]));
}

tested on php 5.6

transactions: 50, total: 9064.79

@jorinvo
Copy link
Author

jorinvo commented Feb 7, 2017

@chocolateboy, I see, cool :) But I won't change the links for this post anymore. Next time!

@aptinio
Copy link

aptinio commented Feb 14, 2017

In Elixir (not concurrent yet):

defmodule DirtyMoney do
  def total(link) do
    {:ok, agent} = Agent.start_link(fn -> %{} end)

    follow(link, agent)

    Agent.get agent, fn transactions ->
      transactions
      |> Map.values
      |> Enum.sum
      |> Kernel./(100)
    end
  end

  def follow(link, agent) do
    %{"id" => id, "content" => content, "links" => links} =
      link
      |> HTTPoison.get!([], follow_redirect: true)
      |> Map.fetch!(:body)
      |> Poison.decode!

    count(agent, id, content)

    Enum.each(links, fn link ->
      follow(link, agent)
    end)
  end

  def count(agent, id, content) do
    Agent.update agent, fn transactions ->
      Map.put_new_lazy transactions, id, fn ->
        parse(content)
      end
    end
  end

  def parse(content) do
    [_, whole, part] = Regex.run(~r/\$(\d+)[,.](\d+)/, content)

    cents =
      part
      |> String.pad_trailing(2, "0")
      |> String.to_integer

    cents + String.to_integer(whole) * 100
  end
end

DirtyMoney.total("https://git.io/vDCxb")

@jorinvo
Copy link
Author

jorinvo commented Nov 9, 2017

There are more amazing answers over at dev.to!

@assafmo
Copy link

assafmo commented Jan 1, 2018

bash, jq, wget, grep, sed, awk:

#!/bin/bash
mkdir xyz && cd xyz
echo -e '0\n1' > /tmp/count
wget https://gist.githubusercontent.com/jorinvo/6f68380dd07e5db3cf5fd48b2465bb04/raw/c02b1e0b45ecb2e54b36e4410d0631a66d474323/fd0d929f-966f-4d1a-89cd-feee5a1c5347.json > /dev/null 2> /dev/null
while [[ $(tail -1 /tmp/count) -ne $(tail -2 /tmp/count | head -1) ]]; do
  jq -r '.links[]' *.json | xargs wget -nc > /dev/null 2> /dev/null
  ls | wc -l >> /tmp/count
done
jq .content *.json | grep -Eo '\$[0-9,.]+[0-9]' | tr -d '$' | sed 's/,/./g' | awk '{sum = sum + $1} END{print sum}'
cd ..
rm -rf ./xyz /tmp/count

9064.79 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment