JSFiddle - React, Tailwind, and code Playground
HTML
<div id="part1">
</div>
<div id="part2">
</div>
JavaScript
/**
* Created by jbush_000 on 12/3/2015.
*/
var dirMap = {
"^": {
x: 0,
y: 1
},
">": {
x: 1,
y: 0
},
"v": {
x: 0,
y: -1
},
"<": {
x: -1,
y: 0
}
};
function PartOne() {
var santaVisits = [];
currentLocationSanta = {
x: 0,
y: 0,
visits: 1
};
santaVisits.push(currentLocationSanta);
getVals().split("").forEach(function (visit) {
var direction = dirMap[visit];
var newLocx = currentLocationSanta.x + direction.x;
var newLocy = currentLocationSanta.y + direction.y;
var visitedAlready = santaVisits.filter(function (a) {
return a.x == newLocx && a.y == newLocy
})[0];
if (visitedAlready) {
visitedAlready.visits++;
currentLocationSanta = visitedAlready;
} else {
santaVisits.push({
x: newLocx,
y: newLocy,
visits: 1
});
currentLocationSanta = {
x: newLocx,
y: newLocy,
visits: 1
}
}
});
document.getElementById('part1').innerHTML = "Part 1: Santa delivered at least one present to " + santaVisits.length + " homes";
}
function PartTwo() {
var santaVisits = [];
currentLocationSanta = {
x: 0,
y: 0,
visits: 1
};
currentLocationRobo = {
x: 0,
y: 0,
visits: 1
};
santaVisits.push(currentLocationSanta);
getVals().split("").forEach(function (visit, i) {
var direction = dirMap[visit];
if (i % 2 == 0) {
var newLocx = currentLocationSanta.x + direction.x;
var newLocy = currentLocationSanta.y + direction.y;
var visitedAlready = santaVisits.filter(function (a) {
return a.x == newLocx && a.y == newLocy
})[0];
if (visitedAlready) {
visitedAlready.visits++;
currentLocationSanta = visitedAlready;
} else {
santaVisits.push({
x: newLocx,
y: newLocy,
visits: 1
});
currentLocationSanta = {
x: newLocx,
y: newLocy,
...