Skip to content

Commit

Permalink
day 4, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
tschulte committed Dec 10, 2018
1 parent 6ec8d20 commit 2164ad6
Show file tree
Hide file tree
Showing 6 changed files with 1,308 additions and 0 deletions.
48 changes: 48 additions & 0 deletions day04/javascript/tschulte/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
This solution is written in JavaScript.

https://adventofcode.com/2018/day/4

== First Star

The idea is to first sort all lines alphabetically, wich will work, because the date format uses the year-month-day format.
Since the lines with the start and end of sleep do not state the guard, the lines can not be parsed in isolation this time.
Instead, it is necessary to use reduce on the lines array.

We need a parseLine method, that is passed the result of the previous parseLine call and the new line.
We than only need to parse the minute part of the date and if this is the start of a new guard duty, or if it is the beginning of sleep or the end.

[source, js]
....
include::part1.js[tags=parseLine]
....

[source, js]
....
include::part1.js[tags=splitLine]
....

[source, js]
....
include::part1.js[tags=guardBeginsShift]
....

With that data, we can find the guard, that is most asleep

[source, js]
....
include::part1.js[tags=guardMostAsleep]
....

And get the overlapping sleep times of that guard

[source, js]
....
include::part1.js[tags=overlappingSleepTimes]
....

And put it all together

[source, js]
....
include::solution.js[tags=part1]
....

0 comments on commit 2164ad6

Please sign in to comment.