knight 100%

This commit is contained in:
weiss
2020-04-09 05:54:07 +02:00
parent 0495ea5b01
commit 1e44a29547
4 changed files with 25 additions and 483 deletions

1
.gitignore vendored
View File

@@ -6,3 +6,4 @@ node_modules
# Generated files # Generated files
.psci .psci
output output
/shawdows_of_the_knight_episode_1/index.js

View File

@@ -1,441 +0,0 @@
// Generated by purs bundle 0.13.6
var PS = {};
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Control.Semigroupoid"] = $PS["Control.Semigroupoid"] || {};
var exports = $PS["Control.Semigroupoid"];
var Semigroupoid = function (compose) {
this.compose = compose;
};
var semigroupoidFn = new Semigroupoid(function (f) {
return function (g) {
return function (x) {
return f(g(x));
};
};
});
exports["semigroupoidFn"] = semigroupoidFn;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Control.Category"] = $PS["Control.Category"] || {};
var exports = $PS["Control.Category"];
var Control_Semigroupoid = $PS["Control.Semigroupoid"];
var Category = function (Semigroupoid0, identity) {
this.Semigroupoid0 = Semigroupoid0;
this.identity = identity;
};
var identity = function (dict) {
return dict.identity;
};
var categoryFn = new Category(function () {
return Control_Semigroupoid.semigroupoidFn;
}, function (x) {
return x;
});
exports["identity"] = identity;
exports["categoryFn"] = categoryFn;
})(PS);
(function(exports) {
"use strict";
exports.intSub = function (x) {
return function (y) {
/* jshint bitwise: false */
return x - y | 0;
};
};
})(PS["Data.Ring"] = PS["Data.Ring"] || {});
(function(exports) {
"use strict";
exports.intAdd = function (x) {
return function (y) {
/* jshint bitwise: false */
return x + y | 0;
};
};
exports.intMul = function (x) {
return function (y) {
/* jshint bitwise: false */
return x * y | 0;
};
};
})(PS["Data.Semiring"] = PS["Data.Semiring"] || {});
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Data.Semiring"] = $PS["Data.Semiring"] || {};
var exports = $PS["Data.Semiring"];
var $foreign = $PS["Data.Semiring"];
var Semiring = function (add, mul, one, zero) {
this.add = add;
this.mul = mul;
this.one = one;
this.zero = zero;
};
var semiringInt = new Semiring($foreign.intAdd, $foreign.intMul, 1, 0);
exports["semiringInt"] = semiringInt;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Data.Ring"] = $PS["Data.Ring"] || {};
var exports = $PS["Data.Ring"];
var $foreign = $PS["Data.Ring"];
var Data_Semiring = $PS["Data.Semiring"];
var Ring = function (Semiring0, sub) {
this.Semiring0 = Semiring0;
this.sub = sub;
};
var ringInt = new Ring(function () {
return Data_Semiring.semiringInt;
}, $foreign.intSub);
exports["ringInt"] = ringInt;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Data.CommutativeRing"] = $PS["Data.CommutativeRing"] || {};
var exports = $PS["Data.CommutativeRing"];
var Data_Ring = $PS["Data.Ring"];
var CommutativeRing = function (Ring0) {
this.Ring0 = Ring0;
};
var commutativeRingInt = new CommutativeRing(function () {
return Data_Ring.ringInt;
});
exports["commutativeRingInt"] = commutativeRingInt;
})(PS);
(function(exports) {
"use strict";
exports.intDegree = function (x) {
return Math.min(Math.abs(x), 2147483647);
};
// See the Euclidean definition in
// https://en.m.wikipedia.org/wiki/Modulo_operation.
exports.intDiv = function (x) {
return function (y) {
if (y === 0) return 0;
return y > 0 ? Math.floor(x / y) : -Math.floor(x / -y);
};
};
exports.intMod = function (x) {
return function (y) {
if (y === 0) return 0;
var yy = Math.abs(y);
return ((x % yy) + yy) % yy;
};
};
})(PS["Data.EuclideanRing"] = PS["Data.EuclideanRing"] || {});
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Data.EuclideanRing"] = $PS["Data.EuclideanRing"] || {};
var exports = $PS["Data.EuclideanRing"];
var $foreign = $PS["Data.EuclideanRing"];
var Data_CommutativeRing = $PS["Data.CommutativeRing"];
var EuclideanRing = function (CommutativeRing0, degree, div, mod) {
this.CommutativeRing0 = CommutativeRing0;
this.degree = degree;
this.div = div;
this.mod = mod;
};
var euclideanRingInt = new EuclideanRing(function () {
return Data_CommutativeRing.commutativeRingInt;
}, $foreign.intDegree, $foreign.intDiv, $foreign.intMod);
var div = function (dict) {
return dict.div;
};
exports["div"] = div;
exports["euclideanRingInt"] = euclideanRingInt;
})(PS);
(function(exports) {
"use strict";
exports.fromNumberImpl = function (just) {
return function (nothing) {
return function (n) {
/* jshint bitwise: false */
return (n | 0) === n ? just(n) : nothing;
};
};
};
exports.toNumber = function (n) {
return n;
};
})(PS["Data.Int"] = PS["Data.Int"] || {});
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Data.Maybe"] = $PS["Data.Maybe"] || {};
var exports = $PS["Data.Maybe"];
var Control_Category = $PS["Control.Category"];
var Nothing = (function () {
function Nothing() {
};
Nothing.value = new Nothing();
return Nothing;
})();
var Just = (function () {
function Just(value0) {
this.value0 = value0;
};
Just.create = function (value0) {
return new Just(value0);
};
return Just;
})();
var maybe = function (v) {
return function (v1) {
return function (v2) {
if (v2 instanceof Nothing) {
return v;
};
if (v2 instanceof Just) {
return v1(v2.value0);
};
throw new Error("Failed pattern match at Data.Maybe (line 217, column 1 - line 217, column 51): " + [ v.constructor.name, v1.constructor.name, v2.constructor.name ]);
};
};
};
var fromMaybe = function (a) {
return maybe(a)(Control_Category.identity(Control_Category.categoryFn));
};
exports["Nothing"] = Nothing;
exports["Just"] = Just;
exports["fromMaybe"] = fromMaybe;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Data.Int"] = $PS["Data.Int"] || {};
var exports = $PS["Data.Int"];
var $foreign = $PS["Data.Int"];
var Data_Maybe = $PS["Data.Maybe"];
var fromNumber = $foreign.fromNumberImpl(Data_Maybe.Just.create)(Data_Maybe.Nothing.value);
exports["fromNumber"] = fromNumber;
exports["toNumber"] = $foreign.toNumber;
})(PS);
(function(exports) {
"use strict";
exports.showIntImpl = function (n) {
return n.toString();
};
})(PS["Data.Show"] = PS["Data.Show"] || {});
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Data.Show"] = $PS["Data.Show"] || {};
var exports = $PS["Data.Show"];
var $foreign = $PS["Data.Show"];
var Show = function (show) {
this.show = show;
};
var showInt = new Show($foreign.showIntImpl);
var show = function (dict) {
return dict.show;
};
exports["Show"] = Show;
exports["show"] = show;
exports["showInt"] = showInt;
})(PS);
(function(exports) {
"use strict";
exports.log = function (s) {
return function () {
console.log(s);
return {};
};
};
})(PS["Effect.Console"] = PS["Effect.Console"] || {});
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Effect.Console"] = $PS["Effect.Console"] || {};
var exports = $PS["Effect.Console"];
var $foreign = $PS["Effect.Console"];
exports["log"] = $foreign.log;
})(PS);
(function(exports) {
"use strict";
// module Math
exports.abs = Math.abs;
})(PS["Math"] = PS["Math"] || {});
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Math"] = $PS["Math"] || {};
var exports = $PS["Math"];
var $foreign = $PS["Math"];
exports["abs"] = $foreign.abs;
})(PS);
(function(exports) {
"use strict";
exports.readline = readline
exports.parseInput = function() {
var inputs = readline().split(' ');
var W = parseInt(inputs[0]); // width of the building.
var H = parseInt(inputs[1]); // height of the building.
var N = parseInt(readline()); // maximum number of turns before game over.
var inputs = readline().split(' ');
var X0 = parseInt(inputs[0]);
var Y0 = parseInt(inputs[1]);
return {
width: W,
height: H,
turns: N,
x: X0,
y: Y0,
}
};
})(PS["Reader"] = PS["Reader"] || {});
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Reader"] = $PS["Reader"] || {};
var exports = $PS["Reader"];
var $foreign = $PS["Reader"];
exports["parseInput"] = $foreign.parseInput;
exports["readline"] = $foreign.readline;
})(PS);
(function($PS) {
// Generated by purs version 0.13.6
"use strict";
$PS["Main"] = $PS["Main"] || {};
var exports = $PS["Main"];
var Data_EuclideanRing = $PS["Data.EuclideanRing"];
var Data_Int = $PS["Data.Int"];
var Data_Maybe = $PS["Data.Maybe"];
var Data_Show = $PS["Data.Show"];
var Effect_Console = $PS["Effect.Console"];
var $$Math = $PS["Math"];
var Reader = $PS["Reader"];
var Pos = (function () {
function Pos(value0, value1) {
this.value0 = value0;
this.value1 = value1;
};
Pos.create = function (value0) {
return function (value1) {
return new Pos(value0, value1);
};
};
return Pos;
})();
var Area = (function () {
function Area(value0, value1) {
this.value0 = value0;
this.value1 = value1;
};
Area.create = function (value0) {
return function (value1) {
return new Area(value0, value1);
};
};
return Area;
})();
var showPos = new Data_Show.Show(function (v) {
return Data_Show.show(Data_Show.showInt)(v.value0) + (" " + Data_Show.show(Data_Show.showInt)(v.value1));
});
var calcArea = function (input) {
var rY = Pos.create(0)(input.height - 1 | 0);
var rX = Pos.create(0)(input.width - 1 | 0);
return new Area(rX, rY);
};
var abs = function (x) {
return Data_Maybe.fromMaybe(-1 | 0)(Data_Int.fromNumber($$Math.abs(Data_Int.toNumber(x))));
};
var getMiddlePos = function (v) {
var y = Data_EuclideanRing.div(Data_EuclideanRing.euclideanRingInt)(abs(v.value1.value0 + v.value1.value1 | 0))(2);
var x = Data_EuclideanRing.div(Data_EuclideanRing.euclideanRingInt)(abs(v.value0.value0 + v.value0.value1 | 0))(2);
return new Pos(x, y);
};
var loop = function (input) {
return function (area) {
var yUp = function (y) {
return new Pos(y - 1 | 0, 0);
};
var yDown = function (y) {
return new Pos(y + 1 | 0, input.height - 1 | 0);
};
var xRight = function (x) {
return new Pos(x + 1 | 0, input.width - 1 | 0);
};
var xLeft = function (x) {
return new Pos(x - 1 | 0, 0);
};
var shrinkArea = function (v) {
return function (v1) {
if (v === "U") {
return new Area(new Pos(v1.value0, v1.value0), yUp(v1.value1));
};
if (v === "D") {
return new Area(new Pos(v1.value0, v1.value0), yDown(v1.value1));
};
if (v === "R") {
return new Area(xRight(v1.value0), new Pos(v1.value1, v1.value1));
};
if (v === "L") {
return new Area(xLeft(v1.value0), new Pos(v1.value1, v1.value1));
};
if (v === "UR") {
return new Area(xRight(v1.value0), yUp(v1.value1));
};
if (v === "DR") {
return new Area(xRight(v1.value0), yDown(v1.value1));
};
if (v === "DL") {
return new Area(xLeft(v1.value0), yDown(v1.value1));
};
if (v === "UL") {
return new Area(xLeft(v1.value0), yUp(v1.value1));
};
return new Area(new Pos(9999, 9999), new Pos(9999, 9999));
};
};
return function __do() {
var dir = Reader.readline();
var pos = new Pos(input.x, input.y);
var area$prime = shrinkArea(dir)(pos);
var v = getMiddlePos(area$prime);
var input$prime = {
x: v.value0,
y: v.value1,
height: input.height,
turns: input.turns,
width: input.width
};
Effect_Console.log(Data_Show.show(Data_Show.showInt)(v.value0) + (" " + Data_Show.show(Data_Show.showInt)(v.value1)))();
return loop(input$prime)(area$prime)();
};
};
};
var main = function __do() {
var input = Reader.parseInput();
return loop(input)(calcArea(input))();
};
exports["Pos"] = Pos;
exports["Area"] = Area;
exports["main"] = main;
exports["calcArea"] = calcArea;
exports["loop"] = loop;
exports["getMiddlePos"] = getMiddlePos;
exports["abs"] = abs;
exports["showPos"] = showPos;
})(PS);
PS["Main"].main();

View File

@@ -8,12 +8,13 @@ import Data.Maybe (fromMaybe)
import Effect (Effect) import Effect (Effect)
import Effect.Console (error, log) import Effect.Console (error, log)
import Math as M import Math as M
import Range (Range(..), Area(..), Pos(..), range, fromPos) import Range (Range(..), Area(..), Pos(..), range)
import Reader (GameInput, parseInput, readline) import Reader (GameInput, parseInput, readline)
main :: Effect Unit main :: Effect Unit
main = do main = do
input <- parseInput input <- parseInput
error $ show input
loop input (calcArea input) loop input (calcArea input)
calcArea :: GameInput -> Area calcArea :: GameInput -> Area
@@ -28,7 +29,9 @@ loop input area = do
let pos = Pos input.x input.y let pos = Pos input.x input.y
let area' = shrinkArea dir area (fromPos pos) let area' = shrinkArea dir area pos
error $ show area
error $ show area'
let (Pos x y) = getMiddlePos area' let (Pos x y) = getMiddlePos area'
let input' = input { x = x, y = y } let input' = input { x = x, y = y }
@@ -37,20 +40,20 @@ loop input area = do
loop input' area' loop input' area'
where where
shrinkArea :: String -> Area -> Range -> Area shrinkArea :: String -> Area -> Pos -> Area
shrinkArea "U" a (Range x y) = Area (Range x x) (yUp a y) shrinkArea "U" a (Pos x y) = Area (range x x) (yUp a y)
shrinkArea "D" a (Range x y) = Area (Range x x) (yDown a y) shrinkArea "D" a (Pos x y) = Area (range x x) (yDown a y)
shrinkArea "R" a (Range x y) = Area (xRight a x) (Range y y) shrinkArea "R" a (Pos x y) = Area (xRight a x) (range y y)
shrinkArea "L" a (Range x y) = Area (xLeft a x) (Range y y) shrinkArea "L" a (Pos x y) = Area (xLeft a x) (range y y)
shrinkArea "UR" a (Range x y) = Area (xRight a x) (yUp a y) shrinkArea "UR" a (Pos x y) = Area (xRight a x) (yUp a y)
shrinkArea "DR" a (Range x y) = Area (xRight a x) (yDown a y) shrinkArea "DR" a (Pos x y) = Area (xRight a x) (yDown a y)
shrinkArea "DL" a (Range x y) = Area (xLeft a x) (yDown a y) shrinkArea "DL" a (Pos x y) = Area (xLeft a x) (yDown a y)
shrinkArea "UL" a (Range x y) = Area (xLeft a x) (yUp a y) shrinkArea "UL" a (Pos x y) = Area (xLeft a x) (yUp a y)
shrinkArea _ _ _ = Area (Range 9999 9999) (Range 9999 9999) shrinkArea _ _ _ = Area (range 9999 9999) (range 9999 9999)
xRight (Area (Range x1 x2) _) x = Range (x+1) (input.width-1) xRight (Area (Range xL xR) _) x = range (x+1) xR
xLeft (Area (Range x1 x2) _) x = Range (x-1) 0 xLeft (Area (Range xL xR) _) x = range xL (x-1)
yUp (Area _ (Range y1 y2)) y = Range (y-1) 0 yUp (Area _ (Range yU yL)) y = range yU (y-1)
yDown (Area _ (Range y1 y2)) y = Range (y+1) (input.height-1) yDown (Area _ (Range yU yL)) y = range (y+1) yL
maxPos :: Pos -> Int maxPos :: Pos -> Int
maxPos (Pos x y) = max x y maxPos (Pos x y) = max x y
@@ -63,29 +66,6 @@ getMiddlePos (Area (Range x1 x2) (Range y1 y2)) = Pos x y
where where
x = abs (x1 + x2) / 2 x = abs (x1 + x2) / 2
y = abs (y1 + y2) / 2 y = abs (y1 + y2) / 2
-- minX = min \(Pos x y) -> x
-- minY = min \(Pos x y) -> y
-- maxX = max \(Pos x y) -> x
-- maxY = max \(Pos x y) -> y
abs :: Int -> Int abs :: Int -> Int
abs x = fromMaybe (-1) $ fromNumber $ M.abs $ toNumber x abs x = fromMaybe (-1) $ fromNumber $ M.abs $ toNumber x
-- nextMove :: Building -> Pos -> Array Pos -> Pos
-- nextMove building pos bombDirs =
-- fromMaybe (Pos 100 100) $ head $ sortBy comparePos validPos
-- where comparePos :: Pos -> Pos -> Ordering
-- comparePos p1 p2 = compare (posVal p2) (posVal p1)
-- validPos :: Array Pos
-- validPos = filter (posValid building) possiblePos
-- possiblePos = map (addPos pos) bombDirs
--
-- posValid :: Building -> Pos -> Boolean
-- posValid (Building w h) (Pos x y) = x <= w && y <= h
--
-- posVal :: Pos -> Int
-- posVal (Pos x y) = fromMaybe 0 $ fromNumber $ absNum x + absNum y
-- where absNum = abs <<< toNumber
--
-- addPos :: Pos -> Pos -> Pos
-- addPos (Pos x1 y1) (Pos x2 y2) = Pos (x1+x2) (y1+y2)

View File

@@ -3,7 +3,6 @@ module Range
, Pos(..) , Pos(..)
, Range(..) , Range(..)
, range , range
, fromPos
) where ) where
import Prelude import Prelude
@@ -15,8 +14,11 @@ data Area = Area Range Range
instance showPos :: Show Pos where instance showPos :: Show Pos where
show (Pos x y) = show x <> " " <> show y show (Pos x y) = show x <> " " <> show y
fromPos :: Pos -> Range instance showRange :: Show Range where
fromPos (Pos x y) = range x y show (Range x y) = show x <> "-" <> show y
instance showArea :: Show Area where
show (Area r1 r2) = show r1 <> " / " <> show r2
data Range = Range Int Int data Range = Range Int Int