Skip to content

Instantly share code, notes, and snippets.

View feugy's full-sized avatar

Damien Simonin Feugas feugy

View GitHub Profile
@feugy
feugy / index.html
Last active August 25, 2023 08:04
nested ternary or hash? (https://jsbench.github.io/#5b2d242b25c9eebd7007c64c529f13b8) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>nested ternary or hash?</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
@feugy
feugy / bundle.js
Created January 12, 2020 16:57
Bangle.js - Activity - dynamic requires
const storage = require("Storage")
const appDesc = "+activit";
storage.write(appDesc, { "name": "activities", "src": "-activit" });
const chunkCount = storage.list().filter(n => n.startsWith("activi")).length
for (let i = 1; i <= chunkCount; i++) {
storage.erase("activi" + i)
}
storage.write("-activit", `var e=require("activi1");var r=require("activi2");var t=require("activi3");var i="Start";function a(){var t=g.getWidth();var a=g.getHeight();var n=r.buildMetric();var o=e.initLayout({draw:function(){g.clear();var r=.2*a;g.setColor.apply(g,e.green);var o=.17*a;var s=.32*t;var c=.87*t;var l=.5*a-o;var v=.5*a+o;g.drawCircle(s,.5*a,o);g.drawCircle(c,.5*a,o);g.clearRect(s,l,c,v);g.drawLine(s,l,c,l);g.drawLine(s,v,c,v);g.drawPoly([.86*t,.44*a,.92*t,.5*a,.86*t,.56*a],1);g.setColor.apply(g,e.white);e.drawCenteredString(i,r,t,a);r=.1*a;e.drawCenteredString(n.curr(),r,t,a,.5*r);var u=e.positionService.value.hasFix?e.green:e.white;g.setColor.apply(g,u);g.drawCircle(.5*t,.85*a,10);if(e.positionService.value
@feugy
feugy / bundle.js
Last active January 12, 2020 16:53
Bangle.js - Activities - Modules.removeAllCached()
const storage = require("Storage")
const appDesc = "+activit";
storage.write(appDesc, { "name": "activities", "src": "-activit" });
const chunkCount = storage.list().filter(n => n.startsWith("activi")).length
for (let i = 1; i <= chunkCount; i++) {
storage.erase("activi" + i)
}
storage.write("-activit", `var e=require("activi1");var r=require("activi2");var t=require("activi3");var i="Start";function a(){var t=g.getWidth();var a=g.getHeight();var n=r.buildMetric();var o=e.initLayout({draw:function(){g.clear();var r=.2*a;g.setColor.apply(g,e.green);var o=.17*a;var s=.32*t;var l=.87*t;var c=.5*a-o;var v=.5*a+o;g.drawCircle(s,.5*a,o);g.drawCircle(l,.5*a,o);g.clearRect(s,c,l,v);g.drawLine(s,c,l,c);g.drawLine(s,v,l,v);g.drawPoly([.86*t,.44*a,.92*t,.5*a,.86*t,.56*a],1);g.setColor.apply(g,e.white);e.drawCenteredString(i,r,t,a);r=.1*a;e.drawCenteredString(n.curr(),r,t,a,.5*r);var u=e.positionService.value.hasFix?e.green:e.white;g.setColor.apply(g,u);g.drawCircle(.5*t,.85*a,10);if(e.positionService.value
const [trainCars, numTickets, numRoutes] = readline().split(' ').map(n => +n)
const colors = ['red', 'yellow', 'green', 'blue', 'white', 'black', 'orange', 'pink', 'engine']
// Player's hand: for each color type, a number of cards
const hand = {}
readline().split(' ').forEach((num, i) => {
hand[colors[i]] = +num
})
// Player's tickets: two cities and a number of earned points
@feugy
feugy / turing-machine.js
Created April 23, 2017 11:17
Solution for coding game "Turing machine" (https://www.codingame.com/ide/puzzle/turing-machine)
let [symbols, length, head] = readline().split(' ').map(n => +n)
// current state
let current = readline()
// current tape, filled with 0
const tape = Array.from({length}, () => 0)
// All states stored by name.
// Contains array of actions; each match symbol of the same rank, and contains:
// - symbol [Integer] to write
// - isLeft [Boolean] to move tape on left or right side
@feugy
feugy / elevator.js
Last active April 23, 2017 09:48
Solution for coding game "Elevator" - https://www.codingame.com/ide/puzzle/elevator
const [floors, up, down, start, target] = readline().split(' ').map(n => +n)
const move = (start, target, up, down, floors) => {
if (target === start) {
return 0
}
let count = 0
let current = start
const stack = []
while(current !== target) {
@feugy
feugy / ascii-to-chars.js
Created January 1, 2016 12:10
[Clash of Code] Convert an incoming string of ASCII codes into characters. ASCII codes are on 3 digits, padded with 0. If input length is not a multiple of 3, print 'ERROR'
const code = [...readline()]
if (code.length % 3) {
print('ERROR')
} else {
const chars = []
for (let i = 0; i< code.length; i += 3) {
chars.push(String.fromCharCode(code[i] + code[i + 1] + code[i + 2]))
}
print(chars.join(''));
@feugy
feugy / blender-the-money-machine.js
Last active December 31, 2015 12:02
Solution for coding game "Blender - The money machine" - https://www.codingame.com/games/puzzles/32 (longest path, topological sorting)
// Freely inspired by http://www.geeksforgeeks.org/find-longest-path-directed-acyclic-graph/
// get the building maps, considering that exist's ids are -1
let n = +readline()
const rooms = []
while(n--) {
const [i, money, next1, next2] = readline().split(' ').map(n => n === 'E' ? -1 : +n)
rooms[i] = {
id: i,
@feugy
feugy / surface.js
Last active December 31, 2015 12:02
Solution for coding game "Surface" - https://www.codingame.com/games/puzzles/31 (flooding, perf optimization)
let [width, height] = [+readline(), +readline()]
let n = height
let grid = []
// use a boolean grid to make comparison faster
while(n--) {
// O is water and thus true. false is land
grid.push(readline().split('').map(c => c === 'O'))
}
n = +readline()
@feugy
feugy / tan-network.js
Last active December 30, 2015 14:48
Solution for coding game "TAN Network" - https://www.codingame.com/games/puzzles/29 (weighten pathfinding)
/**
* Return a stop by its id
* @param {String} id - stop's id
* @return {Object} the full stop object
*/
const getById = id => stops.find(n => n.id === id)
/**
* Compute distance between two nodes
* @param {Object} a - first node, with `lat` and `lng` attributes