Skip to content

Commit

Permalink
Day 11 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvidmar committed Dec 13, 2023
1 parent 020ec8c commit 3d295dc
Showing 1 changed file with 31 additions and 60 deletions.
91 changes: 31 additions & 60 deletions Day11/Day11/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,63 +39,9 @@ for r in (0..<points.count).reversed() {
}
}

// parse

//var map: [[Int]] = []
//var i = 0
//for line in lines {
// map.append(line.map( { $0 == "." ? 0 : 1 }))
//}

//printMap(map: map)
//print()

// expand rows

//for i in (0..<map.count).reversed() {
// if map[i].reduce(0, +) == 0 {
// let emptyLine = Array(repeating: 0, count:map[i].count)
//// print("Row: \(i)")
// map.insert(emptyLine, at: i)
// }
//}

//printMap(map: map)
//print()

// expand columns

//for colIndex in (0..<map[0].count).reversed() {
// var colSum = 0
// for rowIndex in (0..<map.count) {
// colSum += map[rowIndex][colIndex]
// }
// if (colSum == 0) {
//// print("Col: \(colIndex)")
// for rowIndex in (0..<map.count) {
// map[rowIndex].insert(0, at: colIndex)
// }
// }
//}

// number items

//var counter = 1
//for rowIndex in 0..<map.count {
// for colIndex in 0..<map[0].count {
// if (map[rowIndex][colIndex] != 0) {
// map[rowIndex][colIndex] = counter
// counter += 1
// }
// }
//}

//printMap(map: map)

// part 1

var result1 = 0

for (i, p) in points.enumerated() {
for (j, r) in points.enumerated() {
if i < j && p != r {
Expand All @@ -109,13 +55,38 @@ print("Part 1: \(result1)")

// part 2

var result2 = lines.count
//...
points.removeAll()
for (l, line) in lines.enumerated() {
for (c, char) in line.enumerated() {
if char == "#" { points.append((l, c)) }
}
}

print("Part 2: \(result2)")
// expand COLUMNS
for c in (0..<points.count).reversed() {
if points.allSatisfy({ $0.1 != c }) {
for (i, p) in points.enumerated() {
if p.1 > c { points[i].1 = p.1 + 999999 }
}
}
}

// expand ROWS
for r in (0..<points.count).reversed() {
if points.allSatisfy({ $0.0 != r } ) {
for (i, p) in points.enumerated() {
if p.0 > r { points[i].0 = p.0 + 999999 }
}
}
}

func printMap(map: [[Int]]) {
for row in map {
print(row)
var result2 = 0
for (i, p) in points.enumerated() {
for (j, r) in points.enumerated() {
if i < j && p != r {
result2 += abs(p.0 - r.0) + abs(p.1 - r.1)
}
}
}

print("Part 2: \(result2)")

0 comments on commit 3d295dc

Please sign in to comment.