Skip to content

Commit 8165358

Browse files
author
Stefan Koch
committed
Add puzzle solution of day 05
1 parent 39e78a0 commit 8165358

File tree

3 files changed

+1072
-0
lines changed

3 files changed

+1072
-0
lines changed

05.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function calc(input) {
2+
var strings = input.split('\n');
3+
var nice1 = 0;
4+
var nice2 = 0;
5+
6+
strings.forEach(function(string) {
7+
if (string === "") return;
8+
9+
// Part 1
10+
var vowels = string.replace(/[^aeiou]/gi, "").length;
11+
vowels = (vowels < 3) ? false : true;
12+
13+
var duplicates = string.search(/([a-z])\1+/gi);
14+
duplicates = (duplicates < 0) ? false : true;
15+
16+
var banned = string.search(/ab|cd|pq|xy/gi);
17+
banned = (banned >= 0) ? false : true;
18+
19+
if (vowels && duplicates && banned) nice1++;
20+
21+
// Part 2
22+
var repeat = string.search(/([a-z][a-z])[a-z]*\1/gi);
23+
repeat = (repeat < 0) ? false : true;
24+
25+
var between = string.search(/([a-z])[a-z]{1}\1/gi);
26+
between = (between < 0) ? false : true;
27+
28+
if (repeat && between) nice2++;
29+
});
30+
31+
setResult(1, nice1);
32+
setResult(2, nice2);
33+
}

05.log

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--- Day 5: Doesn't He Have Intern-Elves For This? ---
2+
3+
Santa needs help figuring out which strings in his text file are naughty or nice.
4+
5+
A nice string is one with all of the following properties:
6+
7+
It contains at least three vowels (aeiou only), like aei, xazegov, or aeiouaeiouaeiou.
8+
It contains at least one letter that appears twice in a row, like xx, abcdde (dd), or aabbccdd (aa, bb, cc, or dd).
9+
It does not contain the strings ab, cd, pq, or xy, even if they are part of one of the other requirements.
10+
For example:
11+
12+
ugknbfddgicrmopn is nice because it has at least three vowels (u...i...o...), a double letter (...dd...), and none of the disallowed substrings.
13+
aaa is nice because it has at least three vowels and a double letter, even though the letters used by different rules overlap.
14+
jchzalrnumimnmhp is naughty because it has no double letter.
15+
haegwjzuvuyypxyu is naughty because it contains the string xy.
16+
dvszwmarrgswjxmb is naughty because it contains only one vowel.
17+
How many strings are nice?
18+
19+
Your puzzle answer was 255.
20+
21+
--- Part Two ---
22+
23+
Realizing the error of his ways, Santa has switched to a better model of determining whether a string is naughty or nice. None of the old rules apply, as they are all clearly ridiculous.
24+
25+
Now, a nice string is one with all of the following properties:
26+
27+
It contains a pair of any two letters that appears at least twice in the string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps).
28+
It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
29+
For example:
30+
31+
qjhvhtzxzqqjkmpb is nice because is has a pair that appears twice (qj) and a letter that repeats with exactly one letter between them (zxz).
32+
xxyxx is nice because it has a pair that appears twice and a letter that repeats with one between, even though the letters used by each rule overlap.
33+
uurcxstgmygtbstg is naughty because it has a pair (tg) but no repeat with a single letter between them.
34+
ieodomkazucvgmuy is naughty because it has a repeating letter with one between (odo), but no pair that appears twice.
35+
How many strings are nice under these new rules?
36+
37+
Your puzzle answer was 55.
38+
39+
Both parts of this puzzle are complete! They provide two gold stars: **

0 commit comments

Comments
 (0)