Skip to content

Instantly share code, notes, and snippets.

@Experiment5X
Created September 16, 2016 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Experiment5X/c96a2c89e19d734408b8245fb4317cd6 to your computer and use it in GitHub Desktop.
Save Experiment5X/c96a2c89e19d734408b8245fb4317cd6 to your computer and use it in GitHub Desktop.
Generate a list of possible methods of scoring in football for a given score.
//
// main.swift
// FootballScores
//
// Created by Adam Spindler on 9/15/16.
// Copyright © 2016 Expetelek. All rights reserved.
//
import Foundation
enum Score: CustomStringConvertible
{
case Safety
case FieldGoal
case Touchdown
case TouchdownWithExtraPoint
case TouchdownWithTwoPointConversion
static var AllValues: [Score] {
return [ .Safety, .FieldGoal, .Touchdown, .TouchdownWithExtraPoint, .TouchdownWithTwoPointConversion ]
}
var value: Int
{
switch self
{
case .Safety:
return 2
case .FieldGoal:
return 3
case .Touchdown:
return 6
case .TouchdownWithExtraPoint:
return 7
case .TouchdownWithTwoPointConversion:
return 8
}
}
var description: String {
switch self
{
case .Safety:
return "Safety"
case .FieldGoal:
return "Field Goal"
case .Touchdown:
return "Touchdown"
case .TouchdownWithExtraPoint:
return "Touchdown with Extra Point"
case .TouchdownWithTwoPointConversion:
return "Touchdown with 2 point conversion"
}
}
}
var found: [[Score]] = []
func generateAll(scoreToGet: Int, current: [Score])
{
// get the sum of the current combination
let sum = current.reduce(0) { $0 + $1.value }
// the base case
if sum > scoreToGet
{
return
}
else if sum == scoreToGet
{
// check to see if the combination was already found before storing it
let currentSorted = current.sort { $0.value < $1.value }
if !found.contains({ $0 == currentSorted })
{
found.append(current)
}
}
// if sum < scoreToGet try all the scores on it recursively
for score in Score.AllValues
{
let newCombo = current + [score]
generateAll(scoreToGet, current: newCombo)
}
}
generateAll(13, current: [])
for scoreSet in found
{
print(scoreSet)
}
/*
Output for 13:
[Safety, Safety, Safety, Safety, Safety, Field Goal]
[Safety, Safety, Safety, Touchdown with Extra Point]
[Safety, Safety, Field Goal, Field Goal, Field Goal]
[Safety, Safety, Field Goal, Touchdown]
[Safety, Field Goal, Touchdown with 2 point conversion]
[Field Goal, Field Goal, Touchdown with Extra Point]
[Touchdown, Touchdown with Extra Point]
Program ended with exit code: 0
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment