-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAoC2017_Day_01_1.js
More file actions
31 lines (23 loc) · 1.14 KB
/
AoC2017_Day_01_1.js
File metadata and controls
31 lines (23 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Advent Of Code Puzzle #1 - (name of puzzle here)
// Programming in JavaScript via command line node
// Repo located at https://github.com/gerty/advent-of-code-2017
// Comments and suggestions welcome
// 12/1/17 - Happy Holidays to one and all! I'm in a good mood and ready to start!
console.log('Hello to the world...');
var fs = require("fs");
var dailyinput = ''; // init daily input string
var data = fs.readFileSync('input_day_01.txt'); // CHANGE FILENAME DAILY!!
//var data = fs.readFileSync('test_input_01.txt'); // CHANGE FILENAME DAILY!!
dailyinput = data.toString();
var len = 0;
var answer = 0;
len = dailyinput.length; // How long is the input? A useful thing to know.
console.log('Today\'s input has ' + len + ' characters.');
for (var i = 0; i<len; i++) { // traverse the string
// if (dailyinput[i] == dailyinput[ (i+1) % len]) { // Part 1
if (dailyinput[i] == dailyinput[(i+(len/2)) % len]) { // Part 2
answer = answer + parseInt(dailyinput[i]); // need to add the char as an int
console.log("Adding " + dailyinput[i]); // just reporting to the console
}
}
console.log('Answer to Day 1 Part 1 = ' + answer); // not 1174, 1177