Skip to content

Instantly share code, notes, and snippets.

@brv00
Created May 12, 2020 07:47
Show Gist options
  • Save brv00/c5f48f8205fd912f20cdafc4021d055b to your computer and use it in GitHub Desktop.
Save brv00/c5f48f8205fd912f20cdafc4021d055b to your computer and use it in GitHub Desktop.
Traffic jam.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "function updatepath!(path)\n inrange(x) = x ≤ lastindex(path) ? x : x - lastindex(path)\n\n ilastcar = 1\n while !path[ilastcar]; ilastcar += 1; end\n ilastcar -= 1\n distance = 0\n for i = lastindex(path):-1:1\n distance += 1\n j = inrange(i + ilastcar)\n if path[j]\n rand(1:distance^2) == 1 || (path[j] = false; path[inrange(j + 1)] = true)\n distance = 0\n end\n end\nend\n\nfunction updatestat!(path, entryrec)\n prev = path[1]\n updatepath!(path)\n push!(entryrec[2:lastindex(entryrec)], path[1] && !prev)\nend",
"execution_count": 20,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 20,
"data": {
"text/plain": "updatestat! (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = BitArray(0 for _ in 1:50)\nx[1:13] .= true\n\ny = BitArray(0 for _ in 1:lastindex(x))",
"execution_count": 38,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 38,
"data": {
"text/plain": "50-element BitArray{1}:\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n ⋮\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "for _ in 1:40\n for __ in x\n y = updatestat!(x, y)\n end\n println(\"$(sum(y))\")\nend",
"execution_count": 39,
"outputs": [
{
"output_type": "stream",
"text": "3\n12\n12\n12\n12\n11\n12\n13\n11\n12\n12\n12\n11\n12\n12\n12\n11\n13\n11\n12\n12\n11\n11\n13\n11\n12\n12\n11\n12\n12\n11\n11\n13\n12\n11\n12\n12\n11\n13\n10\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(x)",
"execution_count": 40,
"outputs": [
{
"output_type": "stream",
"text": "Bool[0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0]",
"name": "stdout"
}
]
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.4",
"display_name": "Julia 1.4.1",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.4.1"
},
"gist": {
"id": "",
"data": {
"description": "Traffic jam.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment