Project Euler 220
しょうもないコードばかりなのにrankingを独占して申し訳ない
♥0 |
Line 87 |
Modified 2009-08-16 02:16:40 |
MIT License
archived:2017-03-30 04:50:45
ActionScript3 source code
/**
* Copyright uwi ( http://wonderfl.net/user/uwi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/uOzp
*/
// しょうもないコードばかりなのにrankingを独占して申し訳ない
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.utils.getTimer;
import mx.utils.ObjectUtil;
// @see http://projecteuler.net/index.php?section=problems&id=220
public class Euler220 extends Sprite {
private var _tf : TextField;
public function Euler220() {
_tf = new TextField();
_tf.width = 465;
_tf.height = 465;
addChild(_tf);
var s : int = getTimer();
t(solve());
var g : int = getTimer();
t((g - s) + " ms");
}
private var _poscache : Object;
private function solve() : int
{
_poscache = {};
rec("Fa", 0, {dir : 0, x : 0, y : 0, step : 0});
return 0;
}
private static var DIR : Array = [[0, 1], [1, 0], [0, -1], [-1, 0]];
private static var LIMDEPTH : int = 50;
private static var LIMSTEP : Number = 1000000000000;
// private static var LIMDEPTH : int = 10;
// private static var LIMSTEP : Number = 500;
// pos = {dir : int, x : int, y : int, step : Number}
private function rec(str : String, depth : int, pos : Object) : void
{
if(pos.step >= LIMSTEP)return;
if(!_poscache[str])_poscache[str] = new Array(LIMDEPTH);
if(_poscache[str][depth] && pos.step + _poscache[str][depth].step < LIMSTEP){
var c : Object = _poscache[str][depth];
pos.x += c.x * DIR[(pos.dir + 1) % 4][0] + c.y * DIR[pos.dir][0];
pos.y += c.x * DIR[(pos.dir + 1) % 4][1] + c.y * DIR[pos.dir][1];
pos.dir = (pos.dir + c.dir) % 4;
pos.step += c.step;
return;
}
var base : Object = ObjectUtil.copy(pos);
for(var i : int = 0;i < str.length;i++){
switch(str.charAt(i)){
case "F":
pos.x += DIR[pos.dir][0];
pos.y += DIR[pos.dir][1];
pos.step++;
if(pos.step == LIMSTEP){
t(pos.x + "\t" + pos.y);
return;
}
break;
case "a":
if(depth < LIMDEPTH){
rec("aRbFR", depth + 1, pos);
}
break;
case "b":
if(depth < LIMDEPTH){
rec("LFaLb", depth + 1, pos);
}
break;
case "R":
pos.dir++;
if(pos.dir == 4)pos.dir = 0;
break;
case "L":
pos.dir--;
if(pos.dir == -1)pos.dir = 3;
break;
default:
t("invalid!");
break;
}
}
_poscache[str][depth] = {
dir : (pos.dir - base.dir + 4) % 4,
x : (pos.x - base.x) * DIR[(-base.dir + 5) % 4][0] + (pos.y - base.y) * DIR[(-base.dir + 4) % 4][0],
y : (pos.x - base.x) * DIR[(-base.dir + 5) % 4][1] + (pos.y - base.y) * DIR[(-base.dir + 4) % 4][1],
step : pos.step - base.step
};
// t(str + "\t" + depth + "\t" + ObjectUtil.toString(_poscache[str][depth]));
}
private function t(o : *) : void
{
_tf.appendText("" + o + "\n");
}
}
}