Hive

From Esolang
Jump to navigation Jump to search


Hive is a 2D Programming Language created by User:Bsoelch, based on the idea of Hive intelligence.

An single ant (instruction pointer) can only solve the most basic tasks, but if through the interactions of multiple ants much more complicated tasks can be completed.

Memory

The only way to store information in a Hive program is within the ants themselves. Each ant can remember a single (signed) 32-bit integer, to store more complicated information multiple ants have to work together.


The instructions [ ] and ( ) can be used to construct queues and stacks. Both constructions assume that all arriving ants have distances that are multiples of 2

  • Queue ([])
    D
    ^
A> [] >C
    ^
    B

Enqueue: Ants moving east from A will get trapped between the brackets.

Dequeue: When an ant moving north from B hits the bracket the first ant in the queue will leave the brackets in direction C, the ant coming from B will continue moving towards D. If no ants are in the queue, the incoming ant will be waiting in the bracket until an ant is added to the queue.

  • Stack (>(])
    C
    ^
A> >(]
    ^
    B

Push: Ants moving east from A will get trapped between > and ]

Pop: When an ant moving north from B hits the bracket the first ant in the queue will leave the brackets in direction C, the ant coming from B will be killed in the process. If no ants are on the stack, the incoming ant will be waiting in the bracket until an ant is added to the stack.

Instructions

Control flow

Op Description
# creates an ant (facing east) at the start of the program
@ kills ant
<^>v rotate ant to face the same direction as the arrow
/\ mirrors
  • { and }

Ants hitting the outer side of the bracket will be split in a pair of ants, one moving north the other one moving south, ants hitting the inside of the bracket will reverse direction, ant moving in north-south direction are not affected by the bracket.

Diagram of possible paths (arrows indicate moving direction of ants before/after hitting {

          ^  
>{   =>   {      {<  =>   {>
          v
          ^      v
 {   =>   {      {   =>    {
 ^                         v
  • [ and ]:

If no ant is waiting in the current cell, [ and ] act as one-way mirrors for ants moving in east-west direction >[ => [>[< => [>. If another ant is already waiting at the cell this ant will be released, and ants moving in east-east direction will continue moving in their previous direction. Independent of the previous state of the cell ants moving in north-south direction will wait on the cell until the next ant arrives.

  • ( and ):

(Description only for (, ) is the same but with east and west swapped)

Ants moving east, will release any ants waiting in the cell, and then wait for the next ant to arrive. If there are ants waiting in the cell, ants moving west will be redirected to move east, if no ants are waiting they will continue to move west. Ants moving in north-south direction will push out any waiting ant in their own moving direction and then be killed.

Memory modification

Op Description
& set the memory of the ant to zero
0-9 multiply the memory by 10, then add the given digit (subtract if the ants memory is negative)
~ multiply the ants memory by minus one

Binary operators

compute the result of an arithmetic operation using two ants, if only one ant is present wait for a second ant after the computation both ants continue on the previous path with updated values, in the following table A is the value of the first ant and B the value of the second ant.

Op Description First Ant Second Ant
+ addition A+B B
- subtraction A-B B
* multiplication A*B B
% integer division with remainder A/B A%B
= check for equality 1 if A==B 0 otherwise B

Filters

When reaching a filter ants wait for a second ant to arrive, then a condition is checked and if the condition is not satisfied the second ant will be killed. After checking the condition all remaining ants continue on their previous paths.

Op Condition
w just wait, no kill condition
l the memory value of the second ant has to be less than the memory of the first ant
e the memory value of the second ant has to be equal to the memory of the first ant
g the memory value of the second ant has to be greater than the memory of the first ant

IO

Op Description
, reads a single byte from standard input
. write the memory value of the current ant modulo 256 as byte to standard output
? reads a (decimal) number from standard input: skips bytes until a +- or a digit (0-9) is read, then reads bytes until the first non-digit character. The character after the last digit will not be consumed and can be read by a subsequent , operation
! writes the memory value of the current ant in decimal notation to standard output

Examples

  • Print all Fibonacci Numbers less than 1000:
 #1 v <
#1> +!{
 /l w<0001#<
  ^ < \&32.@
 \   /
  • Hello World:
#72 .@#101.@#108.@
#108.@#111.@#32 .@
#87 .@#111.@#114.@
#108.@#100.@#33 .@
  • Prime checker:
              #2v2\
 > v v0#<      /+&/
 2/%@         /{
#^ > =@      /{\   \
#?{#1-\@  /1 *   @
   /  *\  # 3^  /
  \{/  e\ &   #0v\
   \/ @\ >{/  %@ &
/ 2#<     \{  \ =/
             #>1-\
            /    *\
              &
           \{ } /@         @
            \     e @ #2\@!= \
        \                    /
         \        / /   >  e@
                   >{#0v \
     \        >        =&/@
                    #>1-  */
                     \&/
                                
\                   >     /

Reads a number from standard input, prints 1 if the number is prime and 0 otherwise. The empty line is important for timing.


External resources