Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@thefotios
Last active December 11, 2015 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefotios/60669162a4a35769a23e to your computer and use it in GitHub Desktop.
Save thefotios/60669162a4a35769a23e to your computer and use it in GitHub Desktop.
Advent
BEGIN {
FS = ""
}
{
for (i = 1; i <= NF; i++) {
if ($i ~ /\(/) {
floors++
} else {
floors--
}
}
}
END {
print floors
}
BEGIN {
FS = ""
}
{
for (i = 1; i <= NF; i++) {
if ($i ~ /\(/) {
floors++
} else {
floors--
}
if (floors < 0) {
print i
break
}
}
}
END {
print floors
}
# for i in $(cat day2.txt); do echo "$i" | awk -Fx -f day2_1.awk; done | paste -sd+ | bc
BEGIN {
min = 0;
total = 0;
}
{
for(i = 1; i <= NF; i++) {
side2 = i+1;
if (side2 > NF) {
side2 = 1;
}
side = $i * $side2
sides[i] = side
if(min == 0 || side < min) {
min = side;
}
}
}
END {
printf "(%d", min
for(i = 1; i <= NF; i++) {
printf "+(2*%d)",sides[i];
}
print ")"
}
# for i in $(cat day2.txt); do echo "$i" | awk -Fx -f day2_2.awk; done | paste -sd+ | bc
BEGIN {
max = 0;
volume = 1;
}
{
for(i = 1; i <= NF; i++) {
side = $i
volume *= side;
if(max == 1 || side > max) {
max = side;
}
}
}
END {
printf "((2*%d + 2*%d + 2*%d - 2*%d) + %d)\n",$1,$2,$3,max,volume
}
BEGIN {
FS = ""
x = 0
y = 0
}
{
houses[x " " y]++
for (i = 1; i <= NF; i++) {
switch($i) {
case "^":
y++
break
case "v":
y--
break
case ">":
x++
break
case "<":
x--
break
}
houses[x " " y]++
}
}
END {
num_houses=0
for(key in houses) {
num_houses++
}
print num_houses
}
BEGIN {
FS = ""
x[0] = 0
y[0] = 0
x[1] = 0
y[1] = 0
}
{
houses[x[0] " " y[0]]++
for (i = 1; i <= NF; i++) {
person= i % 2
switch($i) {
case "^":
y[person]++
break
case "v":
y[person]--
break
case ">":
x[person]++
break
case "<":
x[person]--
break
}
houses[x[person] " " y[person]]++
}
}
END {
num_houses=0
for(key in houses) {
num_houses++
}
print num_houses
}
#!/usr/bin/env bash
# bash day4_1.sh ckczppom 000000 3938036 3938039
BASE=$1
pattern=${2:-000000}
i=${3:-0}
limit=${4:-10}
run_md5() {
echo -n "${BASE}${i}" | md5sum -
}
until [[ $md5 == ${pattern}* ]] || [ $i -gt $limit ]; do
md5=$(run_md5)
echo "${i} ${md5}";
((i++))
done
#!/usr/bin/env bash
REPEAT='([a-z])\1'
VOWELS='[aeiou].*[aeiou].*[aeiou]'
IGNORE='(ab|cd|pq|xy)'
grep -E "${REPEAT}" | grep -E "${VOWELS}" | grep -Ev "${IGNORE}"
#!/usr/bin/env bash
REPEAT='(?<group>[a-z]{2}).*(?P=group)'
SKIP='([a-z]).\1'
grep -P "${REPEAT}" | grep -E "${SKIP}"
BEGIN {
max_x=0
max_y=0
lights[0][0] = 0
}
match($0, /(.*?) ([[:digit:]]{1,3}),([[:digit:]]{1,3}) through ([[:digit:]]{1,3}),([[:digit:]]{1,3})/, ary) {
action=ary[1]
x1=ary[2]
y1=ary[3]
x2=ary[4]
y2=ary[5]
max_x=(x2>max_x)?x2:max_x
max_y=(y2>max_y)?y2:max_y
for(x = x1; x <= x2; x++) {
for(y = y1; y <= y2; y++) {
if(action == "turn off") {
new_val=0
} else if(action == "turn on") {
new_val=1
} else {
new_val=!lights[x][y]
}
lights[x][y]=new_val
}
}
}
END {
for(x = 0; x <= max_x; x++) {
for(y = 0; y <= max_y; y++) {
sum+=lights[x][y]
}
}
print sum
}
BEGIN {
max_x=0
max_y=0
lights[0][0] = 0
}
match($0, /(.*?) ([[:digit:]]{1,3}),([[:digit:]]{1,3}) through ([[:digit:]]{1,3}),([[:digit:]]{1,3})/, ary) {
action=ary[1]
x1=ary[2]
y1=ary[3]
x2=ary[4]
y2=ary[5]
max_x=(x2>max_x)?x2:max_x
max_y=(y2>max_y)?y2:max_y
#printf "LINE: %s (%d, %d), (%d, %d)\n", action, x1, y1, x2, y2
for(x = x1; x <= x2; x++) {
for(y = y1; y <= y2; y++) {
val=lights[x][y]
if(action == "turn off") {
if(val > 0) {
val--
}
} else if(action == "turn on") {
val++
} else {
val+=2
}
lights[x][y]=val
}
}
}
END {
for(x = 0; x <= max_x; x++) {
for(y = 0; y <= max_y; y++) {
#printf "%3s ", lights[x][y]
sum+=lights[x][y]
}
#print "\n"
}
print sum
}
'use strict';
var PromiseExt = require('./promiseExt.es6');
let functions = {
AND: (a, b) => {
return [a, b, (res) => {
return res[a] & res[b]
}];
},
OR: (a, b) => {
return [a, b, (res) => {
return res[a] | res[b]
}];
},
LSHIFT: (a, shift) => {
return [a, (res) => {
return res[a] << shift;
}];
},
RSHIFT: (a, shift) => {
return [a, (res) => {
return res[a] >> shift;
}];
},
NOT: (a) => {
return [a, (res) => {
return Math.pow(2,16) + ~res[a];
}];
},
SET: (a) => {
let x;
x = Number.parseInt(a);
if(Number.isInteger(x)) {
return Promise.resolve(x);
} else {
return [a, (res) => {
return res[a];
}];
}
}
}
var processLine = (operations, line) => {
let parts = line.split(/\s/);
let target = parts.pop();
// Remove the ->
parts.pop();
let src1, operation, src2;
switch(parts.length) {
case 1:
operation = 'SET';
src1 = parts.shift();
break;
case 2:
operation = parts.shift();
src1 = parts.shift();
break;
case 3:
src1 = parts.shift();
operation = parts.shift();
src2 = parts.shift();
break;
default:
throw new Error("This shouldn't happen", parts);
}
// Explicitly set undefined numbers
[src1, src2].forEach((a) => {
let x;
x = Number.parseInt(a);
if (Number.isInteger(x)) {
operations[x] = functions['SET'](x);
// Set the string version as well in case something requires that one
operations[a] = functions['SET'](x);
}
});
operations[target] = functions[operation](src1, src2);
}
let operations = {};
var rl = require('readline').createInterface({
input: process.stdin
});
rl.on('line', (line) => {
processLine(operations, line);
});
rl.on('close', () => {
PromiseExt.auto(operations).then((res) => {
Object.keys(res).sort().forEach((k) => {
console.log(`${k}: ${res[k]} - ${res[k].toString(2)}`);
})
});
});
'use strict';
var EventEmitter = require('events').EventEmitter;
class PromiseExt extends Promise {
static map(promises) {
let keys = [];
let values = [];
Object.keys(promises).forEach((k) => {
keys.push(k);
values.push(promises[k]);
});
return Promise.all(values).then((results) => {
return results.reduce(function(x, val, i) {
let key = keys[i];
x[key] = val;
return x;
}, {});
});
};
static auto(obj) {
function setPromise(promises, definitions, key) {
// Only do things if the promise is not defined
if (!promises[key]) {
let vals = definitions[key];
// If it's an array, it means that there are dependent steps
if (Array.isArray(vals)) {
// The last one is the promise we care about
let promise = vals.pop();
let _promises = vals.reduce((x, k) => {
x[k] = setPromise(promises, definitions, k);
return x;
}, {});
promises[key] = PromiseExt.map(_promises).then(promise);
} else {
// This is a single promise, we should set and return it
promises[key] = vals;
}
}
return promises[key];
}
let promises = Object.keys(obj).reduce((promises, key) => {
setPromise(promises, obj, key);
return promises;
}, {});
return PromiseExt.map(promises);
}
}
module.exports = PromiseExt;
/**
* Example usage
*/
/*
PromiseExt.auto({
b: ['a', (res) => {
return res.a + 1;
}],
a: Promise.resolve(2),
c: ['a', 'b', (res) => {
return res.a * res.b
}],
}).then((res) => {
console.log(res);
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment