Skip to content

Instantly share code, notes, and snippets.

@felix-larsen
Created December 11, 2020 21:23
Show Gist options
  • Save felix-larsen/13c38d5ad2099d4706be3652da891a3d to your computer and use it in GitHub Desktop.
Save felix-larsen/13c38d5ad2099d4706be3652da891a3d to your computer and use it in GitHub Desktop.
11th December solution - Advent of Code - swift
var iterations = 0
var newGrid = grid
repeat {
grid = newGrid
newGrid = [String]()
for (rowIndex, row) in grid.enumerated() {
var newRow = ""
for (columnIndex, element) in row.enumerated() {
if element == "L" {
let count = countAdjacentChars(of: "#", in: grid, at: (columnIndex, rowIndex), allowedMismatches: 8)
if count == 0 {
newRow += "#"
} else {
newRow += "L"
}
} else if element == "#" {
let count = countAdjacentChars(of: "#", in: grid, at: (columnIndex, rowIndex), allowedMismatches: 4)
if count >= 4 {
newRow += "L"
} else {
newRow += "#"
}
} else if element == "." {
newRow += "."
}
}
newGrid.append(newRow)
}
iterations += 1
print(iterations)
} while grid != newGrid
print(newGrid.reduce(0, { (result, row) -> Int in
result + row.filter { $0 == "#" }.count
}))
func countAdjacentChars(of char: Character, in localGrid:[String], at position :(Int, Int), allowedMismatches: Int) -> Int {
let directions = [(1,1), (1,0), (0,1), (-1,1), (-1,0), (-1,-1), (0,-1), (1,-1)]
var count = 0
var mismatches = 0
for direction in directions {
let x = position.0 + direction.0
let y = position.1 + direction.1
if y >= 0 && y < localGrid.count && x >= 0 && x < localGrid[0].count {
let row = Array(localGrid[y])
let element = row[x ..< x + 1].first
if element == char {
count += 1
} else {
mismatches += 1
if (mismatches > allowedMismatches) {
return count
}
}
}
}
return count
}
var iterations = 0
var newGrid = grid
repeat {
grid = newGrid
newGrid = [String]()
for (rowIndex, row) in grid.enumerated() {
var newRow = ""
for (columnIndex, element) in row.enumerated() {
if element == "L" {
let count = countAdjacentChars2(of: "#", in: grid, at: (columnIndex, rowIndex))
if count == 0 {
newRow += "#"
} else {
newRow += "L"
}
} else if element == "#" {
let count = countAdjacentChars2(of: "#", in: grid, at: (columnIndex, rowIndex))
if count >= 5 {
newRow += "L"
} else {
newRow += "#"
}
} else if element == "." {
newRow += "."
}
}
newGrid.append(newRow)
}
iterations += 1
print(iterations)
} while grid != newGrid
print(newGrid.reduce(0, { (result, row) -> Int in
result + row.filter { $0 == "#" }.count
}))
func countAdjacentChars2(of char: Character, in localGrid:[String], at position :(Int, Int)) -> Int {
let directions = [(1,1), (1,0), (0,1), (-1,1), (-1,0), (-1,-1), (0,-1), (1,-1)]
var count = 0
for direction in directions {
var foundSeat = false
var factor = 1
while !foundSeat {
let x = position.0 + direction.0 * factor
let y = position.1 + direction.1 * factor
if y >= 0 && y < localGrid.count && x >= 0 && x < localGrid[0].count {
let row = Array(localGrid[y])
let element = row[x ..< x + 1].first
if element == char {
count += 1
}
if element != "." {
foundSeat = true
}
factor += 1
} else {
foundSeat = true
}
}
}
return count
}
let filename = "/Users/felix/xCodeProjects/AdventOfCode2020.playground/Resources/december11.txt"
let contents = try! String(contentsOfFile: filename)
var grid = contents.components(separatedBy: CharacterSet.newlines).filter { !$0.isEmpty }
// fill seats
grid = grid.map { $0.replacingOccurrences(of: "L", with: "#")}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment