-1

I'm solving a puzzle (Advent of code 2022 day 8) by allocating an object for each digit in the input file. Using the sample input (25 digits) it works fine but doesn't scale to the 99^2 digits in the full input file. Xcode stops responding after allocating the objects.

What is the limitation that I need to be aware of?

import Foundation

let sampleInput = """
30373
25512
65332
33549
35390
"""

let fileURL = Bundle.main.url(forResource: "input", withExtension: "txt")
let content = try String(contentsOf: fileURL!, encoding: String.Encoding.utf8)

//Using a class because classes are passed by reference, 
//so I can create a transposed view the grid containing the same trees
class Tree {
    var height: Int
    var visibleFromLeft: Bool = false
    var visibleFromRight: Bool = false
    var visibleFromTop: Bool = false
    var visibleFromBottom: Bool = false
    var visible: Bool {
        get {
            return visibleFromLeft || visibleFromRight || visibleFromTop || visibleFromBottom
        }
    }
    init(height: Int) {
        self.height = height
    }
}

let gridOfRows = content.split(separator: "\n").map({rowStr in
    var row: [Tree] = []
    for c in rowStr {
        row.append(Tree(height: Int(String(c))!))
    }
    return row
})
William
  • 191
  • 8
  • You shouldn't use playgrounds for resource-intensive work, since playgrounds are not optimised. Use a real project instead. – Dávid Pásztor Dec 20 '22 at 14:09
  • @DávidPásztor I only needed it to run once though :/ – William Dec 20 '22 at 14:19
  • If only creating a new project was not so tedious! – cora Dec 20 '22 at 14:23
  • Use SPM, then creating a command line project is just as easy as creating a new playground. Also, if you have a single "playground" project that you keep around, you don't have to create a new one every time you want to test something quickly, you can just modify the existing project. – Dávid Pásztor Dec 20 '22 at 14:24
  • @cora If creating a new project seems tedious, might be doing it wrong; actually I find the process a lot sprightlier than using a playground – matt Dec 20 '22 at 15:38
  • @matt I was being sarcastic – cora Dec 20 '22 at 15:41
  • @cora Really not supposed to do that on Stack Overflow. – matt Dec 20 '22 at 15:51

1 Answers1

1

Playground can't really handle as much as a normal project can. Other people have had issues with this as well. Here's a Stack Overflow post explaining the general idea.

Oddly enough, someone had the same issue with the Advent of Code 2021, and the same issues arose for them.

Ultimately, it seems that there is a much different expectation in Playground vs an actual project and XCode doesn't expect the same level of computing.

Ben A.
  • 874
  • 7
  • 23