Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CocoaheadsCodeGolf.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
269A58DE20B1822300872989 /* HeatWavesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269A58DD20B1822300872989 /* HeatWavesTests.swift */; };
269A58E020B1822300872989 /* HeatWaves.h in Headers */ = {isa = PBXBuildFile; fileRef = 269A58D220B1822300872989 /* HeatWaves.h */; settings = {ATTRIBUTES = (Public, ); }; };
269A58E820B1823C00872989 /* HeatWaves.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269A58E720B1823C00872989 /* HeatWaves.swift */; };
3448F28C20B80F38008CC209 /* HeatWaves-readable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3448F28B20B80F38008CC209 /* HeatWaves-readable.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -32,6 +33,7 @@
269A58DF20B1822300872989 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
269A58E720B1823C00872989 /* HeatWaves.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeatWaves.swift; sourceTree = "<group>"; };
269A58ED20B2D13D00872989 /* CharacterCount.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = CharacterCount.txt; sourceTree = "<group>"; };
3448F28B20B80F38008CC209 /* HeatWaves-readable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HeatWaves-readable.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -78,6 +80,7 @@
269A58E720B1823C00872989 /* HeatWaves.swift */,
269A58ED20B2D13D00872989 /* CharacterCount.txt */,
269A58D320B1822300872989 /* Info.plist */,
3448F28B20B80F38008CC209 /* HeatWaves-readable.swift */,
);
path = HeatWaves;
sourceTree = "<group>";
Expand Down Expand Up @@ -218,6 +221,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3448F28C20B80F38008CC209 /* HeatWaves-readable.swift in Sources */,
269A58E820B1823C00872989 /* HeatWaves.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
2 changes: 1 addition & 1 deletion HeatWaves/CharacterCount.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12 bytes.
80 bytes.
60 changes: 60 additions & 0 deletions HeatWaves/HeatWaves-readable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// HeatWaves-readable.swift
// HeatWaves
//
// Solution by Udo Borkowski (2018-05-25)
//
// FOR THE PUBLIC DOMAIN
//

// Background
//
// The Royal Netherlands Meteorological Institute defines a heat wave* as a
// series of at least 5 consecutive days of ≥25°C weather (“summery weather”),
// such that at least 3 of those days are ≥30°C (“tropical weather”).
//
// The tropical weather doesn't have to be measured consecutively: for example:
// 30, 25, 30, 26, 27, 28, 32, 30 is a 8-day long heat wave with 4 days of
// tropical weather.
//
// *(Well, by Dutch standards.)
//
// Challenge
//
// Given a non-empty list of positive integers representing Celsius temperature
// measurements from successive days, decide whether that list contains a heat
// wave (as per the above definition).
//
// The shortest answer in bytes wins.
//
// (This function contains the same code/algorithm as 'isHeatWaveIncludedIn'
// in 'HeatWaves.swift' but is intended for a reader who wants to understand
// the code better. So it includes comments, whitespaces, better names, ....)
public func isHeatWaveIncludedIn_readable(waves w: [Int]) -> Bool {

// This solution is similar to "solution-087-Reduce-Factors" but makes use of an
// extra nested closure. It looks like this is "too much" for the current
// Swift compiler (4.1) as it reports the error:
//
// Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions
//
// Too bad, as this is an 80 bytes solution !
//
// BTW: as suggested in the error message breaking up the expression solves the issue.
// E.g. if we extract the nested closure into an outer variable 'f':
//
// let f={$0%27==0&&$0>81 ?0:$0};return w.reduce(1,{f($1>24 ?$0*($1>29 ?3:2):$0>0 ?1:0)})<1
//
// the code compiles and the tests are green. However we are now at 88 bytes!
//

// Expect a compile error in the next line. See comment above...
return w.reduce(1,{($1>24 ?$0*($1>29 ?3:2):$0>0 ?1:0){$0%27==0&&$0>81 ?0:$0}})<1

// This solution is not yet fully documented. but you may have a look at "solution-087-Reduce-Factors"
// to get an idea how it works. The following lines align both solutions and show the differences:
//
// "solution-080-TooComplex" return w.reduce(1,{( $1>24 ?$0*($1>29 ?3:2):$0>0 ?1:0){ $0%27==0&&$0>81 ?0:$0}})<1
// "solution-087-Reduce-Factors" return w.reduce(1,{let n=$1>24 ?$0*($1>29 ?3:2):$0>0 ?1:0;return n %27==0&&n >81 ?0:n })<1

}
16 changes: 13 additions & 3 deletions HeatWaves/HeatWaves.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// HeatWaves.swift
// HeatWaves
//
// Created by Sven Titgemeyer on 20.05.18.
// Copyright © 2018 Cocoaheads Aachen. All rights reserved.
// Solution by Udo Borkowski (2018-05-25)
//
// FOR THE PUBLIC DOMAIN
//

// Background
Expand All @@ -25,6 +26,15 @@
// wave (as per the above definition).
//
// The shortest answer in bytes wins.
//
// (see "HeatWaves-readable.swift" for a readable/commented version of this code)
//
// NOTICE: The following code gives a compile error when running in Xcode 4.1 (Swift 4.1):
//
// "Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions"
//
// This is most likely caused by Swift's algorithm that resolves overloaded functions. In case this algorithm
// is improved in a later Swift version this code may compile and run....
public func isHeatWaveIncludedIn(waves w: [Int]) -> Bool {
return false
return w.reduce(1,{($1>24 ?$0*($1>29 ?3:2):$0>0 ?1:0){$0%27==0&&$0>81 ?0:$0}})<1
}