{"size": 1040, "ext": "js", "max_stars_repo_path": "2020/day12-1.js", "max_stars_repo_name": "FM-96/advent-of-code", "max_stars_repo_stars_event_min_datetime": "2020-12-02T11:17:50.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-02T11:17:50.000Z", "max_issues_repo_path": "2020/day12-1.js", "max_issues_repo_name": "FM-96/advent-of-code", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "2020/day12-1.js", "max_forks_repo_name": "FM-96/advent-of-code", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.2244897959, "max_line_length": 55, "alphanum_fraction": 0.5009615385}
const getInput = require('../getInput.js');

main();

async function main() {
	const input = await getInput(12);

	const instructions = input.split('\n');

	const pos = {
		x: 0,
		y: 0,
		d: 0, // direction: 0 = east, increases clockwise
	};

	for (const instruction of instructions) {
		let [, action, value] = /(.)(\d+)/.exec(instruction);
		value = Number(value);

		if (action === 'N') {
			pos.y += value;
		} else if (action === 'S') {
			pos.y -= value;
		} else if (action === 'E') {
			pos.x += value;
		} else if (action === 'W') {
			pos.x -= value;
		} else if (action === 'L') {
			pos.d = ((pos.d - (value / 90)) + 4) % 4;
		} else if (action === 'R') {
			pos.d = ((pos.d + (value / 90)) + 4) % 4;
		} else if (action === 'F') {
			if (pos.d === 0) {
				pos.x += value;
			} else if (pos.d === 1) {
				pos.y -= value;
			} else if (pos.d === 2) {
				pos.x -= value;
			} else if (pos.d === 3) {
				pos.y += value;
			}
		}
	}

	const distance = Math.abs(pos.x) + Math.abs(pos.y);

	console.log('Answer: ' + distance);
}
