Last active
December 2, 2017 20:49
-
-
Save codyrobb/d31bb296f9bebf928890d0a88d723273 to your computer and use it in GitHub Desktop.
Day One
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - | |
// MARK: [Day 1] | |
func part_one(input: String) -> Int { | |
let sequence = input.characters.flatMap { Int(String($0)) } | |
var total = 0 | |
for (index, value) in sequence.enumerated() { | |
let next: Int | |
if index == sequence.count - 1 { | |
next = sequence[0] | |
} else { | |
next = sequence[index + 1] | |
} | |
if value == next { | |
total += value | |
} | |
} | |
return total | |
} | |
func part_two(input: String) -> Int { | |
let sequence = input.characters.flatMap { Int(String($0)) } | |
var total = 0 | |
for (index, value) in sequence.enumerated() { | |
let next: Int | |
let nextAdance = sequence.count / 2 | |
if index + nextAdance < sequence.count { | |
next = sequence[index + nextAdance] | |
} else { | |
let toEnd = sequence.count - index | |
let toIndex = nextAdance - toEnd | |
next = sequence[toIndex] | |
} | |
if value == next { | |
total += value | |
} | |
} | |
return total | |
} | |
// MARK: - | |
// MARK: [Day 1] Tests | |
func require(_ input: Int, toEqual output: Int) { | |
if input != output { | |
print("Error: input (\(input) does not match output (\(output))") | |
} | |
} | |
require(part_one(input: "1122"), toEqual: 3) | |
require(part_one(input: "1111"), toEqual: 4) | |
require(part_one(input: "1234"), toEqual: 0) | |
require(part_one(input: "91212129"), toEqual: 9) | |
require(part_two(input: "1212"), toEqual: 6) | |
require(part_two(input: "1221"), toEqual: 0) | |
require(part_two(input: "123425"), toEqual: 4) | |
require(part_two(input: "123123"), toEqual: 12) | |
require(part_two(input: "12131415"), toEqual: 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment