Skip to content

Instantly share code, notes, and snippets.

@Mellen
Last active January 12, 2022 17:15
Show Gist options
  • Save Mellen/4b924340cdf34a1d1113d27bda4727a2 to your computer and use it in GitHub Desktop.
Save Mellen/4b924340cdf34a1d1113d27bda4727a2 to your computer and use it in GitHub Desktop.
(function(inp)
{
const EMPTY = '.';
const EAST = '>';
const SOUTH = 'v';
function Space(occupier)
{
this.nextEast = null;
this.nextSouth = null;
this.occupier = occupier;
this.moveable = false;
}
Space.prototype.addSouthSpace = function(space)
{
if(this.nextSouth == null)
{
this.nextSouth = space;
}
else
{
this.nextSouth.addSouthSpace(space);
}
}
Space.prototype.isEmpty = function()
{
return this.occupier == EMPTY;
}
Space.prototype.setMoveable = function(direction)
{
this.moveable = false;
if(this.occupier == direction)
{
if(direction == EAST)
{
this.moveable = this.nextEast.isEmpty();
}
else if(direction == SOUTH)
{
this.moveable = this.nextSouth.isEmpty();
}
}
return this.moveable;
}
Space.prototype.move = function(direction)
{
if(this.occupier == direction && this.moveable)
{
if(this.occupier == EAST)
{
this.nextEast.occupier = EAST;
this.occupier = EMPTY;
}
else if(this.occupier = SOUTH)
{
this.nextSouth.occupier = SOUTH;
this.occupier = EMPTY;
}
}
}
function draw(firstSpace)
{
let finalString = '';
let leftEdge = firstSpace;
let done = false;
while(!done)
{
finalString += leftEdge.occupier;
let nextSpace = leftEdge.nextEast;
while(nextSpace != leftEdge)
{
finalString += nextSpace.occupier;
nextSpace = nextSpace.nextEast;
}
if(nextSpace == leftEdge)
{
finalString += '\n';
leftEdge = leftEdge.nextSouth;
done = leftEdge == firstSpace;
}
}
return finalString;
}
function setMoveable(firstSpace, direction)
{
let done = false;
let leftEdge = firstSpace;
let movementOccurred = false;
while(!done)
{
movementOccurred = leftEdge.setMoveable(direction) || movementOccurred;
let nextSpace = leftEdge.nextEast;
while(nextSpace != leftEdge)
{
movementOccurred = nextSpace.setMoveable(direction) || movementOccurred;
nextSpace = nextSpace.nextEast;
}
if(nextSpace == leftEdge)
{
leftEdge = leftEdge.nextSouth;
done = leftEdge == firstSpace;
}
}
return movementOccurred;
}
function move(firstSpace, direction)
{
let done = false;
let leftEdge = firstSpace;
while(!done)
{
leftEdge.move(direction);
let nextSpace = leftEdge.nextEast;
while(nextSpace != leftEdge)
{
nextSpace.move(direction);
nextSpace = nextSpace.nextEast;
}
if(nextSpace == leftEdge)
{
leftEdge = leftEdge.nextSouth;
done = leftEdge == firstSpace;
}
}
}
let lines = inp.split('\n');
let height = lines.length;
let width = lines[0].length;
let topSpaces = [];
let firstSpace = null;
let lastTop = null;
let lastLeft = null;
for(let li = 0; li < lines.length; li++)
{
let line = lines[li];
let leftSpaces = [];
for(let ci = 0; ci < line.length; ci++)
{
let c = line[ci];
let sp = new Space(c);
if(ci == 0 && li == 0)
{
firstSpace = sp;
}
if(leftSpaces.length > 0)
{
leftSpaces[leftSpaces.length-1].nextEast = sp;
}
leftSpaces.push(sp);
if(li == 0)
{
topSpaces.push(sp);
}
else
{
topSpaces[ci].addSouthSpace(sp);
}
}
leftSpaces[leftSpaces.length-1].nextEast = leftSpaces[0];
}
for(let sp of topSpaces)
{
sp.addSouthSpace(sp);
}
let movementOccured = setMoveable(firstSpace, EAST);
move(firstSpace, EAST);
movementOccured = setMoveable(firstSpace, SOUTH) || movementOccured;
move(firstSpace, SOUTH);
let moves = 1;
while(movementOccured)
{
movementOccured = setMoveable(firstSpace, EAST);
move(firstSpace, EAST);
movementOccured = setMoveable(firstSpace, SOUTH) || movementOccured;
move(firstSpace, SOUTH);
moves++;
}
return moves;
})(document.querySelector('pre').innerText.trim());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment