Skip to content

Instantly share code, notes, and snippets.

@felix-larsen
Created December 24, 2020 07:50
Show Gist options
  • Save felix-larsen/226facf68b538ec05a8d731606bd27f3 to your computer and use it in GitHub Desktop.
Save felix-larsen/226facf68b538ec05a8d731606bd27f3 to your computer and use it in GitHub Desktop.
24th December solution - Advent of Code 2020 - swift
let occurences = coordinates.reduce(into: [:]) { counts, coordinate in counts[coordinate, default: 0] += 1 }
print(occurences.filter { $0.value % 2 == 1 }.count)
var grid = Dictionary(uniqueKeysWithValues:occurences.map { ($0.key,$0.value % 2 == 1) }.filter { $0.1 })
var newGrid = grid
for _ in 0..<100 {
grid = newGrid
for element in grid {
let coordinate = element.key
for neighbor in coordinate.neighbors {
if grid[neighbor] == nil {
grid[neighbor] = false
}
}
}
for element in grid {
let coordinate = element.key
var activeNeighbors = 0
for neighbor in coordinate.neighbors {
if grid[neighbor] == true {
activeNeighbors += 1
} else {
if newGrid[neighbor] == nil {
newGrid[neighbor] = false
}
}
}
if grid[coordinate] == true {
if activeNeighbors == 0 || activeNeighbors > 2 {
newGrid[coordinate] = false
} else {
newGrid[coordinate] = true
}
} else {
if activeNeighbors == 2 {
newGrid[coordinate] = true
} else {
newGrid[coordinate] = false
}
}
}
}
print(newGrid.filter { $0.value }.count)
import Foundation
let filename = "/Users/felix/xCodeProjects/AdventOfCode2020.playground/Resources/december24.txt"
let contents = try! String(contentsOfFile: filename)
let lines = contents.components(separatedBy: CharacterSet.newlines).filter { !$0.isEmpty}
struct Coordinate: Hashable {
let x: Int
let y: Int
init(_ x: Int, _ y: Int) {
self.x = x
self.y = y
}
var neighbors: [Coordinate] {
return [Coordinate(x - 2, y), Coordinate(x + 2, y), Coordinate(x + 1, y + 1), Coordinate(x - 1, y + 1), Coordinate(x + 1, y - 1), Coordinate(x - 1, y - 1)]
}
}
let diagonalDirections = [
"se": Coordinate(1,1),
"sw": Coordinate(-1,1),
"nw": Coordinate(-1,-1),
"ne": Coordinate(1,-1),
]
let lateralDirections = [
"e": Coordinate(2,0),
"w": Coordinate(-2,0)
]
let coordinates = lines.map { pLine -> Coordinate in
var relativeCoordinate = Coordinate(0,0)
var line = pLine
var substring = ""
while line.count > 0 || substring.count > 0 {
while line.count > 0 && substring.count < 2 {
substring.append(line.removeFirst())
}
var direction = diagonalDirections[substring]
if direction == nil {
direction = lateralDirections[String(substring.first!)]
substring.removeFirst()
} else {
substring.removeFirst()
substring.removeFirst()
}
relativeCoordinate = Coordinate(relativeCoordinate.x + direction!.x, relativeCoordinate.y + direction!.y)
}
return relativeCoordinate
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment