-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aaa1e52
commit 5907cd8
Showing
5 changed files
with
854 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ public struct AdventOfCode2023: Year { | |
Day5(), | ||
Day6(), | ||
Day7(), | ||
Day8(), | ||
// {DAYS} | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import Algorithms | ||
import Foundation | ||
import Utilities | ||
|
||
public struct Day8: Day { | ||
public func part1Solution(for input: Input) -> Int { | ||
let parts = input.words(by: "\n\n") | ||
|
||
let instructions = parts[0].characters | ||
|
||
let map = parts[1] | ||
.lines | ||
.map { line in | ||
line.words(by: CharacterSet.alphanumerics.inverted) | ||
.map(\.raw) | ||
} | ||
.keyed { $0[0] } | ||
.mapValues { (left: $0[1], right: $0[2]) } | ||
|
||
var index = 0 | ||
var count = 0 | ||
var location = "AAA" | ||
let end = "ZZZ" | ||
|
||
while location != end { | ||
let instruction = instructions[index] | ||
if instruction == "L" { | ||
location = map[location]!.left | ||
} else if instruction == "R" { | ||
location = map[location]!.right | ||
} else { | ||
fatalError("impossible") | ||
} | ||
|
||
index = (index + 1) % instructions.count | ||
count += 1 | ||
} | ||
|
||
return count | ||
} | ||
|
||
public func part2Solution(for input: Input) -> Int { | ||
let parts = input.words(by: "\n\n") | ||
|
||
let instructions = parts[0].characters | ||
|
||
let map = parts[1] | ||
.lines | ||
.map { line in | ||
line.words(by: CharacterSet.alphanumerics.inverted) | ||
.map(\.raw) | ||
} | ||
.keyed { $0[0] } | ||
.mapValues { (left: $0[1], right: $0[2]) } | ||
|
||
return map.keys | ||
.filter { $0.hasSuffix("A") } | ||
.map { start in | ||
var index = 0 | ||
var count = 0 | ||
var location = start | ||
|
||
while !location.hasSuffix("Z") { | ||
let instruction = instructions[index] | ||
if instruction == "L" { | ||
location = map[location]!.left | ||
} else if instruction == "R" { | ||
location = map[location]!.right | ||
} else { | ||
fatalError("impossible") | ||
} | ||
|
||
index = (index + 1) % instructions.count | ||
count += 1 | ||
} | ||
|
||
return count | ||
} | ||
.reduce(1) { $0.lcm(with: $1) } | ||
} | ||
} |
Oops, something went wrong.