Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
79
Task/Langtons-ant/CoffeeScript/langtons-ant-1.coffee
Normal file
79
Task/Langtons-ant/CoffeeScript/langtons-ant-1.coffee
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
class Ant
|
||||
constructor: (@world) ->
|
||||
@location = [0, 0]
|
||||
@direction = 'E'
|
||||
|
||||
move: =>
|
||||
[x, y] = @location
|
||||
if @world.is_set x, y
|
||||
@world.unset x, y
|
||||
@direction = Directions.left @direction
|
||||
else
|
||||
@world.set x, y
|
||||
@direction = Directions.right @direction
|
||||
@location = Directions.forward(x, y, @direction)
|
||||
|
||||
# Model a theoretically infinite 2D world with a hash, allowing squares
|
||||
# to be black or white (independent of any ants.)
|
||||
class BlackWhiteWorld
|
||||
constructor: ->
|
||||
@bits = {}
|
||||
|
||||
set: (x, y) ->
|
||||
@bits["#{x},#{y}"] = true
|
||||
|
||||
unset: (x, y) ->
|
||||
delete @bits["#{x},#{y}"]
|
||||
|
||||
is_set: (x, y) ->
|
||||
@bits["#{x},#{y}"]
|
||||
|
||||
draw: ->
|
||||
# Most of this code just involves finding the extent of the world.
|
||||
# Always include the origin, even if it's not set.
|
||||
@min_x = @max_x = @min_y = @max_y = 0
|
||||
for key of @bits
|
||||
[xx, yy] = (coord for coord in key.split ',')
|
||||
x = parseInt xx
|
||||
y = parseInt yy
|
||||
@min_x = x if x < @min_x
|
||||
@max_x = x if x > @max_x
|
||||
@min_y = y if y < @min_y
|
||||
@max_y = y if y > @max_y
|
||||
console.log "top left: #{@min_x}, #{@max_y}, bottom right: #{@max_x}, #{@min_y}"
|
||||
for y in [@max_y..@min_y] by -1
|
||||
s = ''
|
||||
for x in [@min_x..@max_x]
|
||||
if @bits["#{x},#{y}"]
|
||||
s += '#'
|
||||
else
|
||||
s += '_'
|
||||
console.log s
|
||||
|
||||
# Simple code for directions, independent of ants.
|
||||
Directions =
|
||||
left: (dir) ->
|
||||
return 'W' if dir == 'N'
|
||||
return 'S' if dir == 'W'
|
||||
return 'E' if dir == 'S'
|
||||
'N'
|
||||
|
||||
right: (dir) ->
|
||||
return 'E' if dir == 'N'
|
||||
return 'S' if dir == 'E'
|
||||
return 'W' if dir == 'S'
|
||||
'N'
|
||||
|
||||
forward: (x, y, dir) ->
|
||||
return [x, y+1] if dir == 'N'
|
||||
return [x, y-1] if dir == 'S'
|
||||
return [x+1, y] if dir == 'E'
|
||||
return [x-1, y] if dir == 'W'
|
||||
|
||||
|
||||
world = new BlackWhiteWorld()
|
||||
ant = new Ant(world)
|
||||
for i in [1..11500]
|
||||
ant.move()
|
||||
console.log "Ant is at #{ant.location}, direction #{ant.direction}"
|
||||
world.draw()
|
||||
80
Task/Langtons-ant/CoffeeScript/langtons-ant-2.coffee
Normal file
80
Task/Langtons-ant/CoffeeScript/langtons-ant-2.coffee
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
> coffee langstons_ant.coffee
|
||||
Ant is at -24,46, direction W
|
||||
top left: -25, 47, bottom right: 22, -29
|
||||
_##__##_________________________________________
|
||||
##_#####________________________________________
|
||||
#____##_#_______________________________________
|
||||
____#_#_##______________________________________
|
||||
_####_###_#_____________________________________
|
||||
_#####_#__##____________________________________
|
||||
__#___##_##_#___________________________________
|
||||
___###___#__##__________________________________
|
||||
____#___##_##_#_________________________________
|
||||
_____###___#__##________________________________
|
||||
______#___##_##_#_______________________________
|
||||
_______###___#__##______________________________
|
||||
________#___##_##_#_____________________________
|
||||
_________###___#__##____________________________
|
||||
__________#___##_##_#___________________________
|
||||
___________###___#__##__________________________
|
||||
____________#___##_##_#_________________________
|
||||
_____________###___#__##________________________
|
||||
______________#___##_##_#_______________________
|
||||
_______________###___#__##______________________
|
||||
________________#___##_##_#_____________________
|
||||
_________________###___#__##____________________
|
||||
__________________#___##_##_#___________________
|
||||
___________________###___#__##__________________
|
||||
____________________#___##_##_#_________________
|
||||
_____________________###___#__##________________
|
||||
______________________#___##_##_#_______________
|
||||
_______________________###___#__##______________
|
||||
________________________#___##_##_#__##_________
|
||||
_________________________###___#__##__##________
|
||||
__________________________#___##_##__##___#_____
|
||||
____________________####___###___#___#__###_____
|
||||
___________________#____#___#___##_####___#_____
|
||||
__________________###____#___#_#______#_##_#____
|
||||
__________________###____#_##_____#_##__#_##____
|
||||
___________________#____#___##_#_#_____##_______
|
||||
___________________#_#______#_#####__#___#______
|
||||
__________________#___#####__________##_######__
|
||||
__________________###__##__#_##_#_#_#___##_#_##_
|
||||
________________##__#_#######_#___#__###____##_#
|
||||
_______________#__#__######_##___#__#_##___#___#
|
||||
______________#____#_#_##_#__######_#######___#_
|
||||
______________#_####_##_#_####____##__##_#_##_#_
|
||||
_______________#____####___#__#_######_##____###
|
||||
__________________#___#_##_#_###_#__##__##___###
|
||||
_____________________#######____#__##_##_#_____#
|
||||
_____________####__##_##__####_##_##_##__#_____#
|
||||
____________#____#_#___###_##_###____#_####____#
|
||||
___________###_______###_#_#_#####____#_#______#
|
||||
___________#_#___###_####_##_#___##_###_##_____#
|
||||
_________________##_##__####____####_#_#_#_____#
|
||||
____________#____#__##___###__###_____###______#
|
||||
____________##___##_###_####__#______###___##__#
|
||||
____________##_#_####_____#___#__#_##_###_##___#
|
||||
___________####_##___##_####__#_#__#__#__###___#
|
||||
___________#_##_###__#_#_##_#_#_____#_#_____#_#_
|
||||
_______________#_#__#____##_##__#_#__###_##_____
|
||||
_______________##_#____#__#####_#____#____#__#_#
|
||||
______________#_##_#__#____##_##_#__###______###
|
||||
____________#_#___#__#__#__#__###___##__##____#_
|
||||
___________###_#_#####_######_###_#######_#_##__
|
||||
___________#_#_#____#####___##__#####_#####_____
|
||||
_____________#__##___#______#__#_##__###_###____
|
||||
__________####___#####_#########___#_#__________
|
||||
_____##____#__#_____###_#_#___#_###__###________
|
||||
____#__#__####_##___###_##___###_##_____##______
|
||||
___###____#_##_#_#####___#____#__#__##_###______
|
||||
___#_#####_#_#___##__##_____#____#___#__#_______
|
||||
_______######_####__##_#___#__##__#_#_##________
|
||||
_____##______#_###_##__####___#___###___________
|
||||
______#__#_#####__#___#_##___#__#__#____________
|
||||
______##_###_#######_____#_____#_##_____________
|
||||
_____#_#__##_##______#___##____#________________
|
||||
____#__#_####________###__##__#_________________
|
||||
____#_##_###____________##__##__________________
|
||||
_____##_________________________________________
|
||||
______##________________________________________
|
||||
Loading…
Add table
Add a link
Reference in a new issue