Skip to content

Instantly share code, notes, and snippets.

@tom-galvin
tom-galvin / c140.rb
Created March 10, 2014 17:06
DailyProgrammer Challenge #140 Solution
# reddit.com/r/DailyProgrammer
# Solution to Challenge #140: Adjacency Matrix
nodes, lines = gets.chomp.split(' ').map {|s| s.to_i}
mat = Array.new(nodes) {Array.new(nodes) {0}}
(1..lines).step 1 do |x|
line = gets.chomp.split('->').map {|s| s.split(' ').map {|t| t.to_i}.compact}
combos = line.first.product line.last
@tom-galvin
tom-galvin / c145.rb
Created March 10, 2014 17:07
DailyProgrammer Challenge #145 Solution
# reddit.com/r/DailyProgrammer
# Solution to Challenge #145: Tree Generation
def print_tree(n, trunk, leaves)
(1..n).step(2) do |a|
side_spaces = " " * ((n - a) / 2)
puts side_spaces + leaves * a
end
puts " " * ((n - 3) / 2) + trunk * 3
end
@tom-galvin
tom-galvin / c-mst.rb
Created March 10, 2014 17:09
DailyProgrammer Challenge #??? Solution (Minimum Spanning Tree)
# reddit.com/r/DailyProgrammer
# Solution to Challenge #???: Minimum Spanning Tree
vertices = gets.chomp.to_i
adjacency = Array.new(vertices) { gets.chomp.split(',').map {|n| n.to_i } }.transpose # matrix input one liner
traversed_vertices = [0]
edges = []
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
@tom-galvin
tom-galvin / c130.c
Created March 10, 2014 17:11
DailyProgrammer Challenge #130 Solution
/*
reddit.com/r/DailyProgrammer
Solution to Challenge #130: Roll the Dies
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
@tom-galvin
tom-galvin / c131.c
Created March 10, 2014 17:13
DailyProgrammer Challenge #131 Solution
/*
reddit.com/r/DailyProgrammer
Solution to Challenge #131: Who Tests the Tests?
*/
#include <stdio.h>
#include <string.h>
#define UPPER(X) ((X) >= 97 && (X) <= 122 ? (X) - 32 : (X))
#define BUFFER_LENGTH 1024
@tom-galvin
tom-galvin / c-pascal.rb
Created March 16, 2014 02:36
DailyProgrammer Challenge #??? Solution (Pascal's Pyramid)
# reddit.com/r/DailyProgrammer
# Solution to Challenge #???: Pascal's Pyramid
def fac(n)
n < 2 ? 1 : n * fac(n - 1)
end
def tri(n)
(0..n).to_a
.map {|x| fac(n) / (fac(x) * fac(n - x))}
@tom-galvin
tom-galvin / c-rect.rb
Created March 23, 2014 19:08
DailyProgrammer Challenge #??? Solution (Intersecting Rectangles)
# reddit.com/r/DailyProgrammer
# Solution to Challenge #???: Intersecting Rectangles
class Rectangle
attr_accessor :top, :left, :width, :height
def initialize(p1, p2)
x_coords = [p1[0], p2[0]].sort
y_coords = [p1[1], p2[1]].sort
@left = x_coords[0]; @width = x_coords[1] - @left
@tom-galvin
tom-galvin / c158i.rb
Last active August 29, 2015 13:59
DailyProgrammer Challenge #158i Solution (The ASCII Architect, pt. 1)
# reddit.com/r/DailyProgrammer
# Solution to Challenge #158 (Intermediate): The ASCII Architect, pt. 1
# I herd u liek spaghetti
input, output, longest = gets.chomp.split(''), [], 0
while input.any?
output.unshift input[0] =~ /\d/ ?
(' ' * input.shift.to_i + '++__***---'[0, input.shift.ord - 'a'.ord + 1]) :
'++__***--- '[0, input.shift.ord - 'a'.ord + 1]
longest = output[0].length if output[0].length > longest
@tom-galvin
tom-galvin / c162e.rb
Created May 12, 2014 10:46
DailyProgrammer Challenge #162e Solution (Novel Compression, pt. 1: Unpacking the Data)
def decompress(chunks, dict)
delimiter, next_delimiter = '', ' '
output = ''
chunks.each do |ch|
case ch
when /[0-9]\^/
output += delimiter + dict[ch.to_i].capitalize
when /[0-9]!/
output += delimiter + dict[ch.to_i].upcase
when /[0-9]/
@tom-galvin
tom-galvin / c162i.rb
Created May 13, 2014 22:18
DailyProgrammer Challenge #162i Solution (Novel Compression, pt. 2: Compressing the Data)
def tokenize(word, dict)
return word[0] if ['-', 'R*'].include? word
token = (dict.find_index word.downcase.gsub(/[.,\?!;:]/, '')).to_s
(dict << word.downcase.gsub(/[.,\?!;:]/, '');
token = (dict.length - 1).to_s) unless token.length > 0
case word
when /^[a-z]*[.,\?!;:]?$/; # nothing
when /^[A-Z][a-z]*[.,\?!;:]?$/; token += '^'
when /^[A-Z]*[.,\?!;:]?$/; token += '!'
else; puts "Error! Invalid case or punctuation in word #{word}."; abort