Add 2018 solutions
This commit is contained in:
parent
6f9b51146c
commit
43a2ee8414
8 changed files with 2027 additions and 0 deletions
305
2018/AoC.xcodeproj/project.pbxproj
Normal file
305
2018/AoC.xcodeproj/project.pbxproj
Normal file
|
@ -0,0 +1,305 @@
|
|||
// !$*UTF8*$!
|
||||
{
|
||||
archiveVersion = 1;
|
||||
classes = {
|
||||
};
|
||||
objectVersion = 50;
|
||||
objects = {
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
26089D5821DBDFA200D6750F /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26089D5721DBDFA200D6750F /* main.swift */; };
|
||||
26B7AD1C21E000F70025E32E /* algorithms.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26B7AD1B21E000F70025E32E /* algorithms.swift */; };
|
||||
26B7AD1E21E004EE0025E32E /* day3input.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26B7AD1D21E004EE0025E32E /* day3input.swift */; };
|
||||
26B7AD2021E0063F0025E32E /* scanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26B7AD1F21E0063F0025E32E /* scanner.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXCopyFilesBuildPhase section */
|
||||
26089D5221DBDFA200D6750F /* CopyFiles */ = {
|
||||
isa = PBXCopyFilesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
dstPath = /usr/share/man/man1/;
|
||||
dstSubfolderSpec = 0;
|
||||
files = (
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 1;
|
||||
};
|
||||
/* End PBXCopyFilesBuildPhase section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
26089D5421DBDFA200D6750F /* AoC */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = AoC; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
26089D5721DBDFA200D6750F /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
|
||||
26B7AD1B21E000F70025E32E /* algorithms.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = algorithms.swift; sourceTree = "<group>"; };
|
||||
26B7AD1D21E004EE0025E32E /* day3input.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day3input.swift; sourceTree = "<group>"; };
|
||||
26B7AD1F21E0063F0025E32E /* scanner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = scanner.swift; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
26089D5121DBDFA200D6750F /* Frameworks */ = {
|
||||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXFrameworksBuildPhase section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
26089D4B21DBDFA200D6750F = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
26089D5621DBDFA200D6750F /* AoC */,
|
||||
26089D5521DBDFA200D6750F /* Products */,
|
||||
);
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
26089D5521DBDFA200D6750F /* Products */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
26089D5421DBDFA200D6750F /* AoC */,
|
||||
);
|
||||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
26089D5621DBDFA200D6750F /* AoC */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
26089D5721DBDFA200D6750F /* main.swift */,
|
||||
26B7AD1B21E000F70025E32E /* algorithms.swift */,
|
||||
26B7AD1D21E004EE0025E32E /* day3input.swift */,
|
||||
26B7AD1F21E0063F0025E32E /* scanner.swift */,
|
||||
);
|
||||
path = AoC;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
/* End PBXGroup section */
|
||||
|
||||
/* Begin PBXNativeTarget section */
|
||||
26089D5321DBDFA200D6750F /* AoC */ = {
|
||||
isa = PBXNativeTarget;
|
||||
buildConfigurationList = 26089D5B21DBDFA200D6750F /* Build configuration list for PBXNativeTarget "AoC" */;
|
||||
buildPhases = (
|
||||
26089D5021DBDFA200D6750F /* Sources */,
|
||||
26089D5121DBDFA200D6750F /* Frameworks */,
|
||||
26089D5221DBDFA200D6750F /* CopyFiles */,
|
||||
);
|
||||
buildRules = (
|
||||
);
|
||||
dependencies = (
|
||||
);
|
||||
name = AoC;
|
||||
productName = AoC;
|
||||
productReference = 26089D5421DBDFA200D6750F /* AoC */;
|
||||
productType = "com.apple.product-type.tool";
|
||||
};
|
||||
/* End PBXNativeTarget section */
|
||||
|
||||
/* Begin PBXProject section */
|
||||
26089D4C21DBDFA200D6750F /* Project object */ = {
|
||||
isa = PBXProject;
|
||||
attributes = {
|
||||
LastSwiftUpdateCheck = 1010;
|
||||
LastUpgradeCheck = 1300;
|
||||
ORGANIZATIONNAME = "Sven Weidauer";
|
||||
TargetAttributes = {
|
||||
26089D5321DBDFA200D6750F = {
|
||||
CreatedOnToolsVersion = 10.1;
|
||||
LastSwiftMigration = 1300;
|
||||
};
|
||||
};
|
||||
};
|
||||
buildConfigurationList = 26089D4F21DBDFA200D6750F /* Build configuration list for PBXProject "AoC" */;
|
||||
compatibilityVersion = "Xcode 9.3";
|
||||
developmentRegion = en;
|
||||
hasScannedForEncodings = 0;
|
||||
knownRegions = (
|
||||
en,
|
||||
);
|
||||
mainGroup = 26089D4B21DBDFA200D6750F;
|
||||
productRefGroup = 26089D5521DBDFA200D6750F /* Products */;
|
||||
projectDirPath = "";
|
||||
projectRoot = "";
|
||||
targets = (
|
||||
26089D5321DBDFA200D6750F /* AoC */,
|
||||
);
|
||||
};
|
||||
/* End PBXProject section */
|
||||
|
||||
/* Begin PBXSourcesBuildPhase section */
|
||||
26089D5021DBDFA200D6750F /* Sources */ = {
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
26089D5821DBDFA200D6750F /* main.swift in Sources */,
|
||||
26B7AD1C21E000F70025E32E /* algorithms.swift in Sources */,
|
||||
26B7AD2021E0063F0025E32E /* scanner.swift in Sources */,
|
||||
26B7AD1E21E004EE0025E32E /* day3input.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXSourcesBuildPhase section */
|
||||
|
||||
/* Begin XCBuildConfiguration section */
|
||||
26089D5921DBDFA200D6750F /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_ENABLE_OBJC_WEAK = YES;
|
||||
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_COMMA = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INFINITE_RECURSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
|
||||
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
|
||||
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
|
||||
CLANG_WARN_STRICT_PROTOTYPES = YES;
|
||||
CLANG_WARN_SUSPICIOUS_MOVE = YES;
|
||||
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
CODE_SIGN_IDENTITY = "Mac Developer";
|
||||
COPY_PHASE_STRIP = NO;
|
||||
DEBUG_INFORMATION_FORMAT = dwarf;
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
ENABLE_TESTABILITY = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu11;
|
||||
GCC_DYNAMIC_NO_PIC = NO;
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
GCC_OPTIMIZATION_LEVEL = 0;
|
||||
GCC_PREPROCESSOR_DEFINITIONS = (
|
||||
"DEBUG=1",
|
||||
"$(inherited)",
|
||||
);
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
MACOSX_DEPLOYMENT_TARGET = 10.14;
|
||||
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
|
||||
MTL_FAST_MATH = YES;
|
||||
ONLY_ACTIVE_ARCH = YES;
|
||||
SDKROOT = macosx;
|
||||
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
26089D5A21DBDFA200D6750F /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_ENABLE_OBJC_WEAK = YES;
|
||||
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_COMMA = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INFINITE_RECURSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
|
||||
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
|
||||
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
|
||||
CLANG_WARN_STRICT_PROTOTYPES = YES;
|
||||
CLANG_WARN_SUSPICIOUS_MOVE = YES;
|
||||
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
CODE_SIGN_IDENTITY = "Mac Developer";
|
||||
COPY_PHASE_STRIP = NO;
|
||||
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
|
||||
ENABLE_NS_ASSERTIONS = NO;
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu11;
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
MACOSX_DEPLOYMENT_TARGET = 10.14;
|
||||
MTL_ENABLE_DEBUG_INFO = NO;
|
||||
MTL_FAST_MATH = YES;
|
||||
SDKROOT = macosx;
|
||||
SWIFT_COMPILATION_MODE = wholemodule;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-O";
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
26089D5C21DBDFA200D6750F /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
CODE_SIGN_IDENTITY = "-";
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
DEVELOPMENT_TEAM = 722B335UM5;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_VERSION = 5.0;
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
26089D5D21DBDFA200D6750F /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
CODE_SIGN_IDENTITY = "-";
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
DEVELOPMENT_TEAM = 722B335UM5;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SWIFT_VERSION = 5.0;
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
/* End XCBuildConfiguration section */
|
||||
|
||||
/* Begin XCConfigurationList section */
|
||||
26089D4F21DBDFA200D6750F /* Build configuration list for PBXProject "AoC" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
26089D5921DBDFA200D6750F /* Debug */,
|
||||
26089D5A21DBDFA200D6750F /* Release */,
|
||||
);
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
26089D5B21DBDFA200D6750F /* Build configuration list for PBXNativeTarget "AoC" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
26089D5C21DBDFA200D6750F /* Debug */,
|
||||
26089D5D21DBDFA200D6750F /* Release */,
|
||||
);
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
/* End XCConfigurationList section */
|
||||
};
|
||||
rootObject = 26089D4C21DBDFA200D6750F /* Project object */;
|
||||
}
|
7
2018/AoC.xcodeproj/project.xcworkspace/contents.xcworkspacedata
generated
Normal file
7
2018/AoC.xcodeproj/project.xcworkspace/contents.xcworkspacedata
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Workspace
|
||||
version = "1.0">
|
||||
<FileRef
|
||||
location = "self:AoC.xcodeproj">
|
||||
</FileRef>
|
||||
</Workspace>
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>IDEDidComputeMac32BitWarning</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
87
2018/AoC.xcodeproj/xcshareddata/xcschemes/AoC.xcscheme
Normal file
87
2018/AoC.xcodeproj/xcshareddata/xcschemes/AoC.xcscheme
Normal file
|
@ -0,0 +1,87 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "1300"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
buildImplicitDependencies = "YES">
|
||||
<BuildActionEntries>
|
||||
<BuildActionEntry
|
||||
buildForTesting = "YES"
|
||||
buildForRunning = "YES"
|
||||
buildForProfiling = "YES"
|
||||
buildForArchiving = "YES"
|
||||
buildForAnalyzing = "YES">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "26089D5321DBDFA200D6750F"
|
||||
BuildableName = "AoC"
|
||||
BlueprintName = "AoC"
|
||||
ReferencedContainer = "container:AoC.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
</BuildActionEntries>
|
||||
</BuildAction>
|
||||
<TestAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES">
|
||||
<MacroExpansion>
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "26089D5321DBDFA200D6750F"
|
||||
BuildableName = "AoC"
|
||||
BlueprintName = "AoC"
|
||||
ReferencedContainer = "container:AoC.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
<Testables>
|
||||
</Testables>
|
||||
</TestAction>
|
||||
<LaunchAction
|
||||
buildConfiguration = "Debug"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
launchStyle = "0"
|
||||
useCustomWorkingDirectory = "NO"
|
||||
ignoresPersistentStateOnLaunch = "NO"
|
||||
debugDocumentVersioning = "YES"
|
||||
debugServiceExtension = "internal"
|
||||
allowLocationSimulation = "YES">
|
||||
<BuildableProductRunnable
|
||||
runnableDebuggingMode = "0">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "26089D5321DBDFA200D6750F"
|
||||
BuildableName = "AoC"
|
||||
BlueprintName = "AoC"
|
||||
ReferencedContainer = "container:AoC.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildableProductRunnable>
|
||||
</LaunchAction>
|
||||
<ProfileAction
|
||||
buildConfiguration = "Release"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES"
|
||||
savedToolIdentifier = ""
|
||||
useCustomWorkingDirectory = "NO"
|
||||
debugDocumentVersioning = "YES">
|
||||
<BuildableProductRunnable
|
||||
runnableDebuggingMode = "0">
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "26089D5321DBDFA200D6750F"
|
||||
BuildableName = "AoC"
|
||||
BlueprintName = "AoC"
|
||||
ReferencedContainer = "container:AoC.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildableProductRunnable>
|
||||
</ProfileAction>
|
||||
<AnalyzeAction
|
||||
buildConfiguration = "Debug">
|
||||
</AnalyzeAction>
|
||||
<ArchiveAction
|
||||
buildConfiguration = "Release"
|
||||
revealArchiveInOrganizer = "YES">
|
||||
</ArchiveAction>
|
||||
</Scheme>
|
84
2018/AoC/algorithms.swift
Normal file
84
2018/AoC/algorithms.swift
Normal file
|
@ -0,0 +1,84 @@
|
|||
import Foundation
|
||||
func readInput() -> AnySequence<String> {
|
||||
return AnySequence {
|
||||
AnyIterator {
|
||||
readLine()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Sequence {
|
||||
func fold(initial: Element, operation: @escaping (Element, Element) -> Element) -> AnySequence<Element> {
|
||||
return AnySequence { () -> AnyIterator<Element> in
|
||||
var iterator: Iterator? = nil
|
||||
var runningSum = initial
|
||||
|
||||
return AnyIterator {
|
||||
if iterator == nil {
|
||||
iterator = self.makeIterator()
|
||||
return runningSum
|
||||
}
|
||||
|
||||
guard let next = iterator?.next() else {
|
||||
return nil
|
||||
}
|
||||
|
||||
runningSum = operation(runningSum, next)
|
||||
return runningSum
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func repeated() -> AnySequence<Element> {
|
||||
|
||||
return AnySequence(sequence(state: nil as Iterator?) { iterator in
|
||||
if let next = iterator?.next() {
|
||||
return next
|
||||
}
|
||||
|
||||
iterator = self.makeIterator()
|
||||
return iterator?.next()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
extension Sequence where Element: Hashable {
|
||||
func firstDuplicate() -> Element? {
|
||||
var items: Set<Element> = []
|
||||
return first { !items.insert($0).inserted }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension Sequence where Element: Hashable {
|
||||
func frequencies() -> [Element: Int] {
|
||||
return Dictionary(self.map { ($0, 1) }, uniquingKeysWith: { first, second in
|
||||
first + second
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
extension Sequence {
|
||||
func allPairs() -> AnySequence<(Element, Element)> {
|
||||
return AnySequence { () -> AnyIterator<(Element, Element)> in
|
||||
var pairIterator: Array<(Element, Element)>.Iterator? = nil
|
||||
var outerIterator = self.makeIterator()
|
||||
var drop = 1
|
||||
|
||||
return AnyIterator { () -> (Element, Element)? in
|
||||
if let result = pairIterator?.next() {
|
||||
return result
|
||||
}
|
||||
|
||||
guard let value = outerIterator.next() else {
|
||||
return nil
|
||||
}
|
||||
|
||||
pairIterator = self.dropFirst(drop).lazy.map { (value, $0) }.makeIterator()
|
||||
drop += 1
|
||||
|
||||
return pairIterator?.next()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1411
2018/AoC/day3input.swift
Normal file
1411
2018/AoC/day3input.swift
Normal file
File diff suppressed because it is too large
Load diff
111
2018/AoC/main.swift
Normal file
111
2018/AoC/main.swift
Normal file
|
@ -0,0 +1,111 @@
|
|||
import Foundation
|
||||
|
||||
|
||||
|
||||
struct Claim {
|
||||
let id: Int
|
||||
let left: Int
|
||||
let top: Int
|
||||
let width: Int
|
||||
let height: Int
|
||||
|
||||
var right: Int { return left + width }
|
||||
var bottom: Int { return top + height }
|
||||
}
|
||||
|
||||
func overlap(_ first: Claim, _ second: Claim) -> (Int, Int, Int, Int)? {
|
||||
let overlapRight: Int = min(first.right, second.right)
|
||||
let overlapLeft: Int = max(first.left, second.left)
|
||||
let widthOverlap = overlapRight - overlapLeft
|
||||
let overlapBottom: Int = min(first.bottom, second.bottom)
|
||||
let overlapTop: Int = max(first.top, second.top)
|
||||
let heightOverlap = overlapBottom - overlapTop
|
||||
guard widthOverlap > 0 && heightOverlap > 0 else { return nil }
|
||||
return (overlapLeft, overlapTop, widthOverlap, heightOverlap)
|
||||
}
|
||||
|
||||
extension Scanner {
|
||||
|
||||
func scan() -> Claim? {
|
||||
guard
|
||||
scan(string: "#"),
|
||||
let id: Int = scan(),
|
||||
scan(string: "@"),
|
||||
let left: Int = scan(),
|
||||
scan(string: ","),
|
||||
let top: Int = scan(),
|
||||
scan(string: ":"),
|
||||
let width: Int = scan(),
|
||||
scan(string: "x"),
|
||||
let height: Int = scan()
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return Claim(id: id, left: left, top: top, width: width, height: height)
|
||||
}
|
||||
|
||||
func readClaims() -> [Claim] {
|
||||
var result: [Claim] = []
|
||||
while !isAtEnd, let claim: Claim = scan() {
|
||||
result.append(claim)
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
let claims = Scanner(string:day3Input)
|
||||
.readClaims()
|
||||
.allPairs()
|
||||
.compactMap(overlap)
|
||||
//.reduce(0) { $0 + $1.0 * $1.1 }
|
||||
|
||||
print(claims)
|
||||
|
||||
//func removeDifferingCharacter(_ a: String, _ b: String) -> String? {
|
||||
// var differingIndex: Int? = nil
|
||||
// for (index, (first, second)) in zip(a,b).enumerated() {
|
||||
// if first == second {
|
||||
// continue
|
||||
// }
|
||||
//
|
||||
// if differingIndex != nil {
|
||||
// return nil
|
||||
// }
|
||||
//
|
||||
// differingIndex = index
|
||||
// }
|
||||
//
|
||||
// guard let offset = differingIndex, let index = a.index(a.startIndex, offsetBy: offset, limitedBy: a.endIndex) else { return nil }
|
||||
//
|
||||
// return String(a[a.startIndex..<index]) + a[a.index(after: index)..<a.endIndex]
|
||||
//}
|
||||
//
|
||||
//print(Array(readInput())
|
||||
// .allPairs()
|
||||
// .compactMap(removeDifferingCharacter))
|
||||
|
||||
//let frequencies = readInput()
|
||||
// .map { $0.frequencies() }
|
||||
// .reduce(into: (0, 0)) { result, frequencies in
|
||||
// print(frequencies);
|
||||
// if frequencies.contains(where: { _, count in count == 2 }) {
|
||||
// print("double")
|
||||
// result.0 += 1
|
||||
// }
|
||||
//
|
||||
// if frequencies.contains(where: { _, count in count == 3 }) {
|
||||
// print("triple")
|
||||
// result.1 += 1
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
//let firstDuplicate = readInput()
|
||||
// .compactMap(Int.init)
|
||||
// .repeated()
|
||||
// .fold(initial: 0, operation: +)
|
||||
// .firstDuplicate()
|
||||
//
|
||||
//if let x = firstDuplicate { print(x) }
|
||||
|
14
2018/AoC/scanner.swift
Normal file
14
2018/AoC/scanner.swift
Normal file
|
@ -0,0 +1,14 @@
|
|||
import Foundation
|
||||
|
||||
extension Scanner {
|
||||
func scan(string: String) -> Bool {
|
||||
return scanString(string, into: nil)
|
||||
}
|
||||
|
||||
func scan() -> Int? {
|
||||
var result: Int = 0
|
||||
guard scanInt(&result) else { return nil }
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Reference in a new issue