117 lines
4.1 KiB
Swift
117 lines
4.1 KiB
Swift
|
let input = """
|
||
|
UULLULLUULLLURDLDUURRDRRLDURDULLRURDUDULLLUULURURLRDRRRRULDRUULLLLUUDURDULDRRDRUDLRRLDLUDLDDRURURUURRRDDDLLRUDURDULUULLRRULLRULDUDRDRLDLURURUDDUDLURUDUDURLURURRURLUDDRURRDLUURLLRURRDUDLULULUDULDLLRRRDLRDLDUDRDDDRRUURRRRRUURRDRRDLURDRRURDLLUULULLRURDLDDDRRLLRRUURULURUUDDLRRUDDRURUUDLRLRDLRURRRDULLDLRUDDUULRDULURUURDULUDLLRRLDDLRDLRUDRLDDRLRRRDURDULLRRRDRRLUURURDRRDRRLDLUDURURLDUURDRUDRDDRLDRRLDLURURULLUURUDUUDLRLL
|
||
|
LLLULLULDDULRLLURLLLRUUDDLRUULRLULLDLLRRDRLRLRLLDRUUURULDRDDLUDLLDUDULLLRLULLLRULDRDRUDLLRLRLLUDULRRRLDRUULDDULLDULULLUDUDLDRDURDLDLLDUDRRRDLUURRUURULLURLDURLRRLLDDUUULDRLUUDUDLURLULUDURRDRLLDDDDDRRULLRLDULULDDRUURRDLUDDDUDURDDRDRULULLLLUURDURUUUULUDLRURRULRDDRURURLLRLUUDUUURDLLDDLUDRLLLUDLLLLULRLURDRRRDUUDLLDLDDDURRDDRURUURDDRURRLDDDURDLLUURUUULRLUURRUDRLLDLURDUDRLULDLRLULULUDDLRDUDRUDLUULUULDURDRRRRLRULLUDRDDRDLDUDRDRRLDLLLLUDDLRULDLLDDUULDDRRULRRUURUDRDURLLLDDUUDRUUDLULLDR
|
||
|
UDUUULLDDDDLUDLDULRLRDLULLDDRULDURRLURRUDLRRUDURRDUDRRRUULRLLRLUDLDRRDUURDDRDRDUUUDUDLDLLRRLUURLUUUDDDUURLULURRLURRRDRDURURUDRLRUURUDRUDDDRDRDLDRDURDLDRRDUUDLLURLDDURRRLULDRDRLLRLLLRURLDURDRLDRUURRLDLDRLDDDRLDLRLDURURLLLLDDRDUDLRULULLRDDLLUDRDRRLUUULDRLDURURDUDURLLDRRDUULDUUDLLDDRUUULRRULDDUDRDRLRULUUDUURULLDLLURLRRLDDDLLDRRDDRLDDLURRUDURULUDLLLDUDDLDLDLRUDUDRDUDDLDDLDULURDDUDRRUUURLDUURULLRLULUURLLLLDUUDURUUDUULULDRULRLRDULDLLURDLRUUUDDURLLLLDUDRLUUDUDRRURURRDRDDRULDLRLURDLLRRDRUUUURLDRURDUUDLDURUDDLRDDDDURRLRLUDRRDDURDDRLDDLLRR
|
||
|
ULDRUDURUDULLUDUDURLDLLRRULRRULRUDLULLLDRULLDURUULDDURDUUDLRDRUDUDDLDRDLUULRRDLRUULULUUUDUUDDRDRLLULLRRDLRRLUDRLULLUUUUURRDURLLRURRULLLRLURRULRDUURRLDDRRDRLULDDRRDRLULLRDLRRURUDURULRLUDRUDLUDDDUDUDDUDLLRDLLDRURULUDRLRRULRDDDDDRLDLRRLUUDLUURRDURRDLDLDUDRLULLULRLDRDUDLRULLULLRLDDRURLLLRLDDDLLLRURDDDLLUDLDLRLUULLLRULDRRDUDLRRDDULRLLDUURLLLLLDRULDRLLLUURDURRULURLDDLRRUDULUURRLULRDRDDLULULRRURLDLRRRUDURURDURDULURULLRLDD
|
||
|
DURLRRRDRULDLULUDULUURURRLULUDLURURDDURULLRRUUDLRURLDLRUDULDLLRRULLLLRRLRUULDLDLLRDUDLLRLULRLLUUULULRDLDLRRURLUDDRRLUUDDRRUDDRRURLRRULLDDULLLURRULUDLRRRURRULRLLLRULLRRURDRLURULLDULRLLLULLRLRLLLDRRRRDDDDDDULUUDUDULRURDRUDRLUULURDURLURRDRRRRDRRLLLLUDLRRDURURLLULUDDLRLRLRRUURLLURLDUULLRRDURRULRULURLLLRLUURRULLLURDDDRURDUDDULLRULUUUDDRURUUDUURURRDRURDUDRLLRRULURUDLDURLDLRRRRLLUURRLULDDDUUUURUULDLDRLDUDULDRRULDRDULURRUURDU
|
||
|
|
||
|
"""
|
||
|
let inputTest = """
|
||
|
ULL
|
||
|
RRDDD
|
||
|
LURDL
|
||
|
UUUUD
|
||
|
|
||
|
"""
|
||
|
|
||
|
protocol KeyPad {
|
||
|
associatedtype Digit
|
||
|
|
||
|
var current: Digit { get }
|
||
|
mutating func up()
|
||
|
mutating func down()
|
||
|
mutating func left()
|
||
|
mutating func right()
|
||
|
}
|
||
|
|
||
|
func solve<T: KeyPad>(pad: T) -> [T.Digit] {
|
||
|
var pad = pad
|
||
|
var result: [T.Digit] = []
|
||
|
for char in input {
|
||
|
switch char {
|
||
|
case "U": pad.up()
|
||
|
case "D": pad.down()
|
||
|
case "L": pad.left()
|
||
|
case "R": pad.right()
|
||
|
case "\n": result.append(pad.current)
|
||
|
default: fatalError("Invalid input")
|
||
|
}
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
struct Part1Pad: KeyPad {
|
||
|
var current: Int = 5
|
||
|
|
||
|
mutating func up() {
|
||
|
if current - 3 >= 1 {
|
||
|
current -= 3
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mutating func down() {
|
||
|
if current + 3 <= 9 {
|
||
|
current += 3
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mutating func left() {
|
||
|
if current != 1 && current != 4 && current != 7 {
|
||
|
current -= 1
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mutating func right() {
|
||
|
if current != 3 && current != 6 && current != 9 {
|
||
|
current += 1
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
print("Part 1", solve(pad: Part1Pad()))
|
||
|
|
||
|
struct Part2Pad: KeyPad {
|
||
|
let grid: [[Character]] = [
|
||
|
Array("##1##"),
|
||
|
Array("#234#"),
|
||
|
Array("56789"),
|
||
|
Array("#ABC#"),
|
||
|
Array("##D##"),
|
||
|
]
|
||
|
|
||
|
var x = 0
|
||
|
var y = 2
|
||
|
|
||
|
var current: Character {
|
||
|
grid[y][x]
|
||
|
}
|
||
|
|
||
|
|
||
|
mutating func up() {
|
||
|
if y > 0 && grid[y - 1][x] != "#" {
|
||
|
y -= 1
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mutating func down() {
|
||
|
if y < 4 && grid[y + 1][x] != "#" {
|
||
|
y += 1
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mutating func left() {
|
||
|
if x > 0 && grid[y][x - 1] != "#" {
|
||
|
x -= 1
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mutating func right() {
|
||
|
if x < 4 && grid[y][x + 1] != "#" {
|
||
|
x += 1
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
print("Part 2:", solve(pad: Part2Pad()))
|
||
|
|