Skip to content

Instantly share code, notes, and snippets.

using LinearAlgebra
###########################################
# Types and basis
abstract type AbstractCrystalGroup end
struct CrystalGroup{K} <: AbstractCrystalGroup
R::Array{K, 2}
T::Array{K, 1}
end
## Part 1
struct Planet
pos::Vector{Int}
vel::Vector{Int}
end
Planet(s::String) = Planet(parse.(Int, match(r"x=([\-0-9]+).*y=([\-0-9]+).*z=([\-0-9]+)", s).captures), zeros(Int, 3))
veldiff!(p1::Planet, p2::Planet) = (p1.vel .+= sign.(p2.pos .- p1.pos))
shift!(p::Planet) = (p.pos .+= p.vel)
energy(p::Planet) = sum(abs.(p.pos))*sum(abs.(p.vel))
mutable struct Prog
code::Vector{Int}
cur::Int
input::Vector{Int}
output::Vector{Int}
relative_base::Int
eop::Bool
end
str2prog(s) = parse.(Int, split(s, ","))
@Arkoniak
Arkoniak / permutations.jl
Last active December 8, 2019 20:06
Permutations iterator
struct PermIter{T}
v::Vector{T}
infinite::Bool
end
function Base.iterate(iter::PermIter{T}, state = ones(Int, length(iter.v))) where T
if state[end] == 2 return nothing end
v = iter.v
elem = Vector{T}(undef, length(iter.v))
## Part 1
function conv2graph(data)
graph = Dict{String, Vector{String}}() # x is being orbited by [y, z]
for orbit in data
m = match(r"^([^\)]+)\)(.*)$", orbit)
if m[1] in keys(graph)
push!(graph[m[1]], m[2])
else
graph[m[1]] = [m[2]]
end
mutable struct Code
code::Vector{Int}
cur::Int
input::Int
output::Int
eop::Bool
end
str2prog(s) = parse.(Int, split(s, ","))
@Arkoniak
Arkoniak / dygraph_with_percents.R
Last active February 21, 2017 20:26
Example of dygraph with percent formatted y axis.
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
@Arkoniak
Arkoniak / mxnet_exp_activation_custom_loss.jl
Created January 12, 2017 12:10
Julia MXNet with custom loss and exponential activation
# https://github.com/dmlc/MXNet.jl/issues/167
using MXNet
# Custom eval metric
import MXNet.mx: get, reset!, _update_single_output
type CustomMetric <: mx.AbstractEvalMetric
loss::Float64
n::Int
@Arkoniak
Arkoniak / compare_dict_map.jl
Created January 11, 2017 21:16
Compare performance of list comprehension and map conversion
using BenchmarkTools
# Prepare random dictionary
d = Dict{Symbol, Real}()
for _ in 1:1000
d[Symbol(randstring())] = rand()
end
function test_dict()
Dict(k => v for (k, v) in d)
@Arkoniak
Arkoniak / float_to_string.jl
Created January 11, 2017 20:49
Test format of Float types conversions
# There is no default parse(Float16), this one is taken from
# https://github.com/JuliaLang/julia/issues/16411#issuecomment-256835385
import Base: parse
function parse(::Type{Float16}, str::String)
fp = 0.0
try
fp = parse(Float64, str)
catch
throw(ArgumentError(string("invalid number format \"",str,"\" for Float16")))