Skip to content

Commit

Permalink
Completed Day 25 Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceMcRooster committed Dec 25, 2024
1 parent 30092e6 commit bb51be2
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Sources/AdventOfCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ let allChallenges: [any AdventDay] = [
Day21(),
Day22(),
Day23(),
Day24()
Day24(),
Day25()
]

@main
Expand Down
80 changes: 80 additions & 0 deletions Sources/Day25.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import Algorithms

struct Day25: AdventDay {
var data: String

struct Combination: Hashable {
let isLock: Bool

let col1: UInt8
let col2: UInt8
let col3: UInt8
let col4: UInt8
let col5: UInt8

init(fromData input: some StringProtocol) {
self.isLock = input.first! == "#"

var col1: UInt8?
var col2: UInt8?
var col3: UInt8?
var col4: UInt8?
var col5: UInt8?

for (lineNum, line) in input.split(separator: "\n").enumerated() {
assert(line.count == 5, "Got malformed line \(line)")

if self.isLock {
if col1 == nil, line[line.index(line.startIndex, offsetBy: 0)] == "." { col1 = UInt8(lineNum - 1) }
if col2 == nil, line[line.index(line.startIndex, offsetBy: 1)] == "." { col2 = UInt8(lineNum - 1) }
if col3 == nil, line[line.index(line.startIndex, offsetBy: 2)] == "." { col3 = UInt8(lineNum - 1) }
if col4 == nil, line[line.index(line.startIndex, offsetBy: 3)] == "." { col4 = UInt8(lineNum - 1) }
if col5 == nil, line[line.index(line.startIndex, offsetBy: 4)] == "." { col5 = UInt8(lineNum - 1) }
} else {
if col1 == nil, line[line.index(line.startIndex, offsetBy: 0)] == "#" { col1 = UInt8(6 - lineNum) }
if col2 == nil, line[line.index(line.startIndex, offsetBy: 1)] == "#" { col2 = UInt8(6 - lineNum) }
if col3 == nil, line[line.index(line.startIndex, offsetBy: 2)] == "#" { col3 = UInt8(6 - lineNum) }
if col4 == nil, line[line.index(line.startIndex, offsetBy: 3)] == "#" { col4 = UInt8(6 - lineNum) }
if col5 == nil, line[line.index(line.startIndex, offsetBy: 4)] == "#" { col5 = UInt8(6 - lineNum) }
}
}

self.col1 = col1!
self.col2 = col2!
self.col3 = col3!
self.col4 = col4!
self.col5 = col5!
}

func fits(_ other: Combination) -> Bool {
assert(self.isLock != other.isLock)

return self.col1 + other.col1 <= 5
&& self.col2 + other.col2 <= 5
&& self.col3 + other.col3 <= 5
&& self.col4 + other.col4 <= 5
&& self.col5 + other.col5 <= 5
}
}

func part1() -> Any {
let combinations = Set(data.split(separator: "\n\n").map(Combination.init))

let locks = combinations.filter(\.isLock)
let keys = combinations.subtracting(locks)

var count = 0

for key in keys {
for lock in locks {
if key.fits(lock) { count += 1 }
}
}

return count
}

func part2() -> Any {
return 0
}
}
57 changes: 57 additions & 0 deletions Tests/Day25.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Testing

@testable import AdventOfCode

struct Day25Tests {

let testData = """
#####
.####
.####
.####
.#.#.
.#...
.....
#####
##.##
.#.##
...##
...#.
...#.
.....
.....
#....
#....
#...#
#.#.#
#.###
#####
.....
.....
#.#..
###..
###.#
###.#
#####
.....
.....
.....
#....
#.#..
#.#.#
#####
"""

@Test func testPart1() async throws {
#expect(String(describing: Day25(data: testData).part1()) == "3")
}

@Test func testPart2() async throws {
#expect(true)
}
}

0 comments on commit bb51be2

Please sign in to comment.