Skip to content

Instantly share code, notes, and snippets.

@Gryzor
Created December 18, 2015 01:24
Show Gist options
  • Save Gryzor/d141825a7b6548b3bf14 to your computer and use it in GitHub Desktop.
Save Gryzor/d141825a7b6548b3bf14 to your computer and use it in GitHub Desktop.
import Swift
/*
Now, a nice string is one with all of the following properties:
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).
It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
*/
var d5input: [String] = ["qjhvvvzxzqqjkmpb", "sknufchjdvccccta", "ugqblsonqaxycvkg", "yevmbiyrqdqrmlbw", "bvpvwrhoyneorcmm", "gbyjqzcsheaxnyib", "knhsmdjssycvuoqf","nizjxiwdakpfttyh", "nwrkbhorhfqqoliz", "ynsqwvwuwzqpzzwp", "yitscrgexjfclwwh", "dhajwxqdbtrfltzz", "bmrfylxhthiaozpv", "frvatcvgknjhcndw", "xlvtdmpvkpcnmhya", "pxpemuzuqzjlmtoc", "dijdacfteteypkoq", "knrcdkrvywagglnf", "viuajtspnvnptia", "xvlqzukmwbcjgwho"]
var pairs = [String:Int]()
func day5_2(input: [String]) {
var good = 0
for str in input {
if (checkString(str)) {
print("good one: \(str)")
good++
}
}
print("\n Found \(good) good string(s)")
}
func checkString(s: String) -> Bool {
var isCheckOneOK: Bool = false
var isCheckTwoOK: Bool = false
var isAtEnd: Bool = false
pairs.removeAll()
for var pos = 0 ; pos < s.characters.count - 1 ; pos++ {
let char = String(s[s.startIndex.advancedBy(pos)])
var pair = ""
pair += char
pair += String(s[s.startIndex.advancedBy(pos + 1)])
// Check 1 (Non Overlapping Pairs)
if (pairs.keys.contains(pair)) {
if let pairStartingPos = pairs[pair] {
if pairStartingPos == pos - 1 {
// overlaps, bail out
print("Skipping String \(s) because pair \(pair) is duplicated and overlaps at position \(pairStartingPos)")
return false
}
else {
isCheckOneOK = true
}
}
} else {
pairs[pair] = pos
}
// Check 2 At least one letter repeats with something in between
if (pos+2) > s.characters.count - 1 {
isAtEnd = true
}
if isAtEnd {
continue
}
let charPlusTwo: String? = String(s[s.startIndex.advancedBy(pos + 2)])
if (charPlusTwo != nil) {
if char == charPlusTwo {
isCheckTwoOK = true
}
}
}
return isCheckOneOK && isCheckTwoOK
}
day5_2(d5input)
@Gryzor
Copy link
Author

Gryzor commented Dec 18, 2015

It basically obtains the two characters (32:35)

Check 1:

Checks if we have this pair (38) and obtains the “position” where the pair starts in the string (39). If the pair was not found, store it (50)
Check for overlapping (40) and bail out if needed (43)
If it doesn’t overlap we’re good to continue searching so far(46)

Check 2: (duplicated letter with 1 in between)
First make sure you don’t go beyond the char, if pos+2 is beyond the string, we’re at the end and the current character is irrelevant for this check. (54), so we continue (59)
Otherwise, get the +2 character (61) and see if it’s the same (64)

Loop that shit until you’re done (or exited in line 43) and return the results.

My output was (I commented line 42 so less logging):

good one: dijdacfteteypkoq

 Found 1 good string(s)

@Gryzor
Copy link
Author

Gryzor commented Dec 18, 2015

By "manually" looking at the input I used, it seems right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment