seperate haskell und purescript directories

This commit is contained in:
weiss
2020-04-19 05:51:24 +02:00
parent 7dfe85a5fd
commit 49481147ff
67 changed files with 5 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
module Lib where
import Prelude
import Data.Int (fromNumber, pow, toNumber)
import Data.Maybe (fromMaybe)
import Math as M
import Range (Area(..), Pos(..), Range(..))
maxPos :: Pos -> Int
maxPos (Pos x y) = max x y
minPos :: Pos -> Int
minPos (Pos x y) = min x y
getMiddlePos :: Area -> Pos
getMiddlePos (Area (Range x1 x2) (Range y1 y2)) = Pos x y
where
x = abs (x1 + x2) / 2
y = abs (y1 + y2) / 2
abs :: Int -> Int
abs x = fromMaybe 0 $ fromNumber $ M.abs $ toNumber x
sqrt :: Int -> Int
sqrt x = fromMaybe 0 $ fromNumber $ M.sqrt $ toNumber x
dist :: Pos -> Pos -> Int
dist (Pos x1 y1) (Pos x2 y2) = sqrt $ a2 + b2
where
a2 = abs (x2 - x1) `pow` 2
b2 = abs (y2 - y1) `pow` 2
toPos :: forall e. { x :: Int, y :: Int | e } -> Pos
toPos p = Pos p.x p.y

View File

@@ -0,0 +1,47 @@
module Main where
import Prelude
import Data.Array (any, drop, head, length, sortBy)
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Console (log)
import Lib (dist, toPos)
import Range (Pos(..))
import Reader (Player, Human, parseInput)
main :: Effect Unit
main = do
loop Nothing
loop :: Maybe Pos -> Effect Unit
loop target = do
input <- parseInput
loop' input.player input.humans target
loop' :: Player -> Array Human -> Maybe Pos -> Effect Unit
loop' player humans target = do
let nearestHuman = sortBy (\h1 h2 -> compare (dist (toPos h1) playerPos) (dist (toPos h2) playerPos)) humans
let sndHuman = if length nearestHuman > 1 then drop 1 nearestHuman else nearestHuman
let target' = case target of
Just t -> if targetAlive then [t] else map toPos sndHuman
Nothing -> if length nearestHuman == 2 then map toPos sndHuman else map toPos nearestHuman
let pos = case head target' of
Just p -> p
Nothing -> Pos 6000 6000
log $ show pos
loop $ Just pos
where
playerPos :: Pos
playerPos = toPos player
targetAlive :: Boolean
targetAlive = case target of
Just (Pos tx ty) -> any (\h -> h.x == tx && h.y == ty) humans
Nothing -> false

View File

@@ -0,0 +1,26 @@
module Range
( Area(..)
, Pos(..)
, Range(..)
, range
) where
import Prelude
-- data Building = Building Int Int
data Pos = Pos Int Int
data Area = Area Range Range
instance showPos :: Show Pos where
show (Pos x y) = show x <> " " <> show y
instance showRange :: Show Range where
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
range :: Int -> Int -> Range
range x y = Range (min x y) (max x y)

View File

@@ -0,0 +1,47 @@
"use strict";
exports.readline = readline
exports.parseInput = function() {
var inputs = readline().split(' ');
var x = parseInt(inputs[0]);
var y = parseInt(inputs[1]);
var humanCount = parseInt(readline());
var humans = []
for (let i = 0; i < humanCount; i++) {
var inputs = readline().split(' ');
var humanId = parseInt(inputs[0]);
var humanX = parseInt(inputs[1]);
var humanY = parseInt(inputs[2]);
humans.push({
id: humanId,
x: humanX,
y: humanY,
})
}
var zombieCount = parseInt(readline());
var zombies = []
for (let i = 0; i < zombieCount; i++) {
var inputs = readline().split(' ');
var zombieId = parseInt(inputs[0]);
var zombieX = parseInt(inputs[1]);
var zombieY = parseInt(inputs[2]);
var zombieXNext = parseInt(inputs[3]);
var zombieYNext = parseInt(inputs[4]);
zombies.push({
id: zombieId,
x: zombieX,
y: zombieY,
nextX: zombieXNext,
nextY: zombieYNext,
})
}
return {
player: { x, y },
humanCount,
zombieCount,
humans,
zombies,
}
};

View File

@@ -0,0 +1,34 @@
module Reader where
import Effect (Effect)
type Player =
{ x :: Int
, y :: Int
}
type Human =
{ id :: Int
, x :: Int
, y :: Int
}
type Zombie =
{ id :: Int
, x :: Int
, y :: Int
, nextX :: Int
, nextY :: Int
}
type GameInput =
{ player :: Player
, humanCount :: Int
, zombieCount :: Int
, humans :: Array Human
, zombies :: Array Zombie
}
foreign import parseInput :: Effect GameInput
foreign import readline :: Effect String