Skip to content

Commit cc93cd5

Browse files
committed
Day 1 & 2
0 parents  commit cc93cd5

File tree

16 files changed

+1629
-0
lines changed

16 files changed

+1629
-0
lines changed

Diff for: .editorconfig

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true;
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true

Diff for: .eslintrc.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
"env": {
3+
"es6": true,
4+
"node": true,
5+
"mocha": true
6+
},
7+
"extends": "eslint:recommended",
8+
"parserOptions": {
9+
"sourceType": "module"
10+
},
11+
"rules": {
12+
"indent": ["error", 2, { "SwitchCase": 1 }],
13+
"quotes": [2, "single"],
14+
"semi": 2
15+
}
16+
};

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
3+
input.js

Diff for: .npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

Diff for: .travis.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- '8'
4+
script:
5+
- npm run lint
6+
- npm test

Diff for: .vscode/launch.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceFolder}\\index.js"
12+
}
13+
]
14+
}

Diff for: LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Mario Tacke
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Diff for: README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Advent of Code 2018
2+
3+
My repo for Advent of Code 2018 in nodejs/javascript. I'm using this as an excuse to work on my javascript, so this shouldn't be used as an example of the best way to do it.
4+
5+
Tests inspired by: https://github.com/mariotacke/advent-of-code-2018
6+
7+
Dev code starts as my own, but I will look at other solutions for inspiration of how to do it better after.
8+
9+
## Running Tests
10+
11+
Each day contains its own set of tests. To run them type `npm test`.
12+
13+
## Visit
14+
- http://adventofcode.com/2018

Diff for: day-01-chronal-calibration/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Day 1: Chronal Calibration
2+
3+
After feeling like you've been falling for a few minutes, you look at the device's tiny screen. "Error: Device must be calibrated before first use. Frequency drift detected. Cannot maintain destination lock." Below the message, the device shows a sequence of changes in frequency (your puzzle input). A value like `+6` means the current frequency increases by `6`; a value like `-3` means the current frequency decreases by `3`.
4+
5+
For example, if the device displays frequency changes of `+1, -2, +3, +1`, then starting from a frequency of zero, the following changes would occur:
6+
7+
- Current frequency ` 0`, change of `+1`; resulting frequency ` 1`.
8+
- Current frequency ` 1`, change of `-2`; resulting frequency `-1`.
9+
- Current frequency `-1`, change of `+3`; resulting frequency ` 2`.
10+
- Current frequency ` 2`, change of `+1`; resulting frequency ` 3`.
11+
12+
In this example, the resulting frequency is `3`.
13+
14+
Here are other example situations:
15+
16+
- `+1, +1, +1` results in ` 3`
17+
- `+1, +1, -2` results in ` 0`
18+
- `-1, -2, -3` results in `-6`
19+
20+
Starting with a frequency of zero, **what is the resulting frequency** after all of the changes in frequency have been applied?
21+
22+
## Part Two
23+
24+
You notice that the device repeats the same frequency change list over and over. To calibrate the device, you need to find the first frequency it reaches **twice**.
25+
26+
For example, using the same list of changes above, the device would loop as follows:
27+
28+
- Current frequency ` 0`, change of `+1`; resulting frequency ` 1`.
29+
- Current frequency ` 1`, change of `-2`; resulting frequency `-1`.
30+
- Current frequency `-1`, change of `+3`; resulting frequency ` 2`.
31+
- Current frequency ` 2`, change of `+1`; resulting frequency ` 3`.
32+
- (At this point, the device continues from the start of the list.)
33+
- Current frequency ` 3`, change of `+1`; resulting frequency ` 4`.
34+
- Current frequency ` 4`, change of `-2`; resulting frequency ` 2`, which has already been seen.
35+
36+
In this example, the first frequency reached twice is `2`. Note that your device might need to repeat its list of frequency changes many times before a duplicate frequency is found, and that duplicates might be found while in the middle of processing the list.
37+
38+
Here are other examples:
39+
40+
- `+1, -1` first reaches `0` twice.
41+
- `+3, +3, +4, -2, -4` first reaches `10` twice.
42+
- `-6, +3, +8, +5, -6` first reaches `5` twice.
43+
- `+7, +7, -2, -7, -4` first reaches `14` twice.
44+
45+
**What is the first frequency your device reaches twice?**
46+
47+
## References
48+
- https://adventofcode.com/2018/day/1

Diff for: day-01-chronal-calibration/chronal-calibration.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function calculateFrequency(input) {
2+
let result = 0;
3+
input.split(/\r?\n/).forEach(change => {
4+
result += parseInt(change);
5+
});
6+
return result;
7+
8+
//Better:
9+
// return input
10+
// .split('\n')
11+
// .map((x) => parseInt(x))
12+
// .reduce((a, b) => a + b, 0);
13+
}
14+
15+
function calculateFrequencyFoundTwice(input) {
16+
const frequencies = new Set([0]);
17+
let result = 0;
18+
let repetitionFound = false;
19+
20+
const changes = input
21+
.split('\n')
22+
.map((x) => parseInt(x));
23+
24+
while (!repetitionFound) {
25+
for (let i=0; i<changes.length; i++) {
26+
result += changes[i];
27+
if (frequencies.has(result)) {
28+
repetitionFound = true;
29+
break;
30+
}
31+
frequencies.add(result);
32+
}
33+
}
34+
return result;
35+
}
36+
37+
module.exports.calculateFrequency = calculateFrequency;
38+
module.exports.calculateFrequencyFoundTwice = calculateFrequencyFoundTwice;

0 commit comments

Comments
 (0)