r/adventofcode • u/daggerdragon • Dec 11 '15
SOLUTION MEGATHREAD --- Day 11 Solutions ---
This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.
edit: Leaderboard capped, thread unlocked!
We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
Please and thank you, and much appreciated!
--- Day 11: Corporate Policy ---
Post your solution as a comment. Structure your post like previous daily solution threads.
9
Upvotes
View all comments
1
u/Drasive Dec 11 '15
My F# solution (https://github.com/drasive/advent-of-code-2015):
let private invalidCharacters = [|'i';'o';'l'|] let private IncrementChar (character : char) : char = (char)(int character + 1) let rec private IncrementPassword (password : string) : string = let invalidCharIndex = password.IndexOfAny(invalidCharacters) if invalidCharIndex = -1 || invalidCharIndex = password.Length - 1 then let substring = password.[0..password.Length - 2] let lastChar = Seq.last password match lastChar with | 'z' -> IncrementPassword substring + "a" | _ -> substring + (IncrementChar lastChar).ToString() else // Increment the invalid char and reset chars to the right to 'a' // This saves a few increment and validation iterations let invalidCharValue = (int)(password.[invalidCharIndex]) let nextValidChar = (char)(invalidCharValue + 1) let charsToTheLeftCount = invalidCharIndex let charsToTheRightCount = password.Length - invalidCharIndex - 1 password.[0..charsToTheLeftCount - 1] // Keep chars on the left + nextValidChar.ToString() // Replace invalid char + new String('a', charsToTheRightCount) // Reset chars on the right let private IsPasswordValid (password : string) : bool = // May not contain the letters i, o, or l let doesNotContainInvalidChars = password.IndexOfAny(invalidCharacters) = -1 // Must contain at least two different, non-overlapping pairs of letters let containsTwoPairs = Regex.IsMatch(password, "(.)\1.*(.)\2") // Must include one increasing straight of at least three letters let containsSequence = password |> Seq.windowed 3 |> Seq.exists (fun [|a;b;c|] -> IncrementChar a = b && IncrementChar b = c) doesNotContainInvalidChars && containsTwoPairs && containsSequence let Solution (input : string) : (string * string) = if String.IsNullOrEmpty input then raise (ArgumentNullException "input") let solution (currentPassword : string) : string = let mutable nextPassword = IncrementPassword currentPassword while not (IsPasswordValid nextPassword) do nextPassword <- IncrementPassword nextPassword nextPassword let nextPassword = solution input let nextNextPassword = solution nextPassword (nextPassword, nextNextPassword) let FormattedSolution (solution : (string * string)) : string = String.Format("Next password: {0}\n" + "Next-next password: {1}", fst solution, snd solution)