This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Project Euler - Problem 14 | |
# http://projecteuler.net/index.php?section=problems&id=14 | |
# | |
# The following iterative sequence is defined for the set of positive | |
# integers: | |
# | |
# n → n/2 (n is even) | |
# n → 3n + 1 (n is odd) | |
# | |
# Using the rule above and starting with 13, we generate the following |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Grid | |
def initialize(str) | |
@grid = str.lines.collect do |line| | |
line.split.collect { |n| n.to_i } | |
end | |
end | |
def width | |
# Assume all grid rows are equal width | |
@grid[0].length |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public delegate bool Predicate<T, U> (T arg1, U arg2); | |
public static class EventHelper | |
{ | |
public static EventHandler<T> Filter<T> (Predicate<object, T> predicate, | |
EventHandler<T> handler) where T: EventArgs | |
{ | |
EventHandler<T> filtered = (object sender, T args) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enemy.Moved += EventHelper.Filter ( | |
(sender, new_pos) => my_pos.DistanceTo (new_pos) > weapon_range, | |
(sender, new_pos) => ChangeStateTo (State.MoveCloser)); | |
enemy.Moved += EventHelper.Filter ( | |
(sender, new_pos) => my_pos.DistanceTo (new_pos) == 1, | |
(sender, new_pos) => ChangeStateTo (State.MoveAway)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
counter.Increased += EventHelper.Filter<CounterEventArgs> ( | |
(sender, args) => args.Count % 2 == 0, | |
(sender, args) => Console.WriteLine (args.Count)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
counter.Increased += (sender, args) => { | |
if (args.Count % 2 != 0) { | |
Console.WriteLine (args.Count); | |
} | |
}; |
NewerOlder