From 1e98cedbafdfefb1a9e94c31f0d0bb3cb1f3866e Mon Sep 17 00:00:00 2001 From: elioat Date: Wed, 19 Jun 2024 11:29:22 -0400 Subject: * --- swift/life/.gitignore | 8 +++++++ swift/life/Package.swift | 14 ++++++++++++ swift/life/Sources/main.swift | 53 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 swift/life/.gitignore create mode 100644 swift/life/Package.swift create mode 100644 swift/life/Sources/main.swift diff --git a/swift/life/.gitignore b/swift/life/.gitignore new file mode 100644 index 0000000..0023a53 --- /dev/null +++ b/swift/life/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/swift/life/Package.swift b/swift/life/Package.swift new file mode 100644 index 0000000..e1f6d6e --- /dev/null +++ b/swift/life/Package.swift @@ -0,0 +1,14 @@ +// swift-tools-version: 5.10 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "life", + targets: [ + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. + .executableTarget( + name: "life"), + ] +) diff --git a/swift/life/Sources/main.swift b/swift/life/Sources/main.swift new file mode 100644 index 0000000..4b3e7a6 --- /dev/null +++ b/swift/life/Sources/main.swift @@ -0,0 +1,53 @@ +import Foundation + +let rows = 10 +let cols = 10 +var board = Array(repeating: Array(repeating: 0, count: cols), count: rows) + +func printBoard(_ board: [[Int]]) { + board.forEach { row in + row.forEach { cell in + print(cell == 1 ? "@" : ".", terminator: "") + } + print() + } + print("\n") +} + +func countLiveNeighbors(_ board: [[Int]], x: Int, y: Int) -> Int { + let directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] + return directions.reduce(0) { count, dir in + let newX = x + dir.0 + let newY = y + dir.1 + if newX >= 0 && newX < rows && newY >= 0 && newY < cols { + return count + board[newX][newY] + } + return count + } +} + +func nextGeneration(_ currentBoard: [[Int]]) -> [[Int]] { + return currentBoard.enumerated().map { (x, row) in + return row.enumerated().map { (y, cell) in + let liveNeighbors = countLiveNeighbors(currentBoard, x: x, y: y) + if cell == 1 && (liveNeighbors < 2 || liveNeighbors > 3) { + return 0 + } else if cell == 0 && liveNeighbors == 3 { + return 1 + } + return cell + } + } +} + +board[1][2] = 1 +board[2][3] = 1 +board[3][1] = 1 +board[3][2] = 1 +board[3][3] = 1 + +for _ in 0..<10 { + printBoard(board) + board = nextGeneration(board) + sleep(1) // FIXME: figure out how to sleep for periods shorter than a full second +} -- cgit 1.4.1-2-gfad0