Skip to content

Instantly share code, notes, and snippets.

@zetashift
zetashift / bencode.nim
Created November 20, 2020 15:46
Bencode from day #2 of Nim days
# A simple Bencode parser following Nim Days by xmonader
import strformat, tables, json, strutils, hashes
type
BencodeKind* = enum
btString, btInt, btList, btDict
BencodeType* = ref object
case kind*: BencodeKind
of BencodeKind.btString: s* : string
of BencodeKind.btInt: i* : int
@zetashift
zetashift / shoddygenerator.nim
Last active March 7, 2019 02:21
Implementation of the business card generator in Nim
# A quick Nim translation of https://gist.github.com/munificent/b1bcd969063da3e6c298be070a22b604
# With some help of the JS version
import random
const
HEIGHT = 40
WIDTH = 80
PLAYER = '@'
TREASURE = '$'
@zetashift
zetashift / genmap.nim
Last active December 31, 2018 00:08
Bare dungeon map generation
proc generateMap*(map: GameMap, maxRooms, roomMinSize, roomMaxSize: int,
mapWidth, mapHeight: int, player: Entity,
entities: seq[Entity], maxMonsterPerRoom: int): GameMap =
var
rooms: seq[Rect] = @[]
numRooms = 0
result = map
randomize()
@zetashift
zetashift / quicksort.scala
Created December 7, 2018 02:53
QuickSort in Scala
object QS {
def quickSort(xs: Array[Int]): Array[Int] = {
if (xs.length < 2) xs
else {
val pivot = xs(xs.length/2)
val less = xs.filter(n => n < pivot)
val greater = xs.filter(n => n > pivot)
quickSort(less) ++ Array(pivot) ++ quickSort(greater)
}
}
@zetashift
zetashift / toy_robot.ex
Created November 26, 2018 02:11
ToyRobot - A snipper
defmodule ToyRobot do
@directions [:north, :east, :south, :west]
@directions_to_the_right %{north: :east, east: :south, south: :west, west: :north}
@directions_to_the_left Enum.map(@directions_to_the_right, fn {from, to} -> {to, from} end)
@table_top_x 4
@table_top_y 4
def place do
{:ok, %ToyRobot.Position{}}
@zetashift
zetashift / motif_finder.scala
Created November 5, 2018 13:55
Solution to rosalind exercise 'Motif Finder'
object MotifFinder {
def find(text: String, motif: String) = {
var result = Seq[Int]()
for (i <- 0 until text.length - motif.length) {
var aheadSS = text.slice(i, i + motif.length)
if (aheadSS == motif) {
result = result :+ (i + 1)
}
}
@zetashift
zetashift / rosalind_prot1.scala
Created November 4, 2018 19:15
Solution to: Translating RNA into Protein from rosalind
object Translator {
def translate(rna: String) = {
val splittedRna = rna.grouped(3).toList
println(splittedRna)
for (codon <- splittedRna) {
codon match {
case "AUG" => print("M")
case "GCC" | "GCG" | "GCU" | "GCA" => print("A")
@zetashift
zetashift / essential_scala_classes.scala
Created September 12, 2018 00:34
Classes in Scala example #1
class Director(val firstName: String, val lastName: String, val yearOfBirth: Int) {
def name(): String = firstName + " " + lastName
}
class Film(val name: String, val yearOfRelease: Int, val imdbRating: Double, val director: Director) {
def directorsAge(): Int = this.yearOfRelease - director.yearOfBirth
def isDirectedBy(d: Director) = if (d.name == this.director.name) true else false
def copy(name: String = this.name, yearOfRelease: Int = this.yearOfRelease, imdbRating: Double = this.imdbRating, director: Director = this.director): Film = {
new Film(name, yearOfRelease, imdbRating, director)
}
@zetashift
zetashift / simple_godot.nim
Created August 13, 2018 00:30
Simple Nim godot example
# taken from: https://github.com/pragmagic/godot-nim/issues/17#issuecomment-399192271
import godot, node, input
gdobj MyNode of Node:
method init*() =
addUserSignal("my_signal") # Declare on init. If need to connect this signal to other nodes, should be done in code
method ready*() =
connect("my_signal", self, "_on_my_signal_emitted")
@zetashift
zetashift / wabbits.nim
Last active August 7, 2018 03:29
Mortal Fibonacci Rabbits
import strutils
import math
const
input = "95 17".split(" ")
n = input[0].parseInt
m = input[1].parseInt
proc mortalRabbits(n: int, m: int): uint64 =