-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay25.swift
112 lines (97 loc) · 2.33 KB
/
Day25.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import Foundation
import Tools
/// Hardcoded the input, couldn't be bothered to write a parser for such few instructions.
final class Day25Solver: DaySolver {
let dayNumber: Int = 25
struct Input {
let beginState: State
let steps: Int
let instructions: [State: [Instruction]]
}
enum Move {
case left
case right
}
struct Instruction {
let write: Int
let move: Move
let nextState: State
}
enum State {
case a
case b
case c
case d
case e
case f
}
func solvePart1(withInput input: Input) -> Int {
var state = input.beginState
var cursorPosition = 0
var values: [Int: Int] = [:]
for _ in 0 ..< input.steps {
let instruction: Instruction
if values[cursorPosition, default: 0] == 0 {
instruction = input.instructions[state]![0]
} else {
instruction = input.instructions[state]![1]
}
values[cursorPosition] = instruction.write
cursorPosition += instruction.move == .right ? 1 : -1
state = instruction.nextState
}
return values.values.filter { $0 == 1 }.count
}
func solvePart2(withInput input: Input) -> String {
"Merry Christmas 🎄"
}
func parseInput(rawString: String) -> Input {
// // example input
// return .init(
// beginState: .a,
// steps: 6,
// instructions: [
// .a: [
// .init(write: 1, move: .right, nextState: .b),
// .init(write: 0, move: .left, nextState: .b),
// ],
// .b: [
// .init(write: 1, move: .left, nextState: .a),
// .init(write: 1, move: .right, nextState: .a),
// ]
// ]
// )
//
// real input
return .init(
beginState: .a,
steps: 12861455,
instructions: [
.a: [
.init(write: 1, move: .right, nextState: .b),
.init(write: 0, move: .left, nextState: .b),
],
.b: [
.init(write: 1, move: .left, nextState: .c),
.init(write: 0, move: .right, nextState: .e),
],
.c: [
.init(write: 1, move: .right, nextState: .e),
.init(write: 0, move: .left, nextState: .d),
],
.d: [
.init(write: 1, move: .left, nextState: .a),
.init(write: 1, move: .left, nextState: .a),
],
.e: [
.init(write: 0, move: .right, nextState: .a),
.init(write: 0, move: .right, nextState: .f),
],
.f: [
.init(write: 1, move: .right, nextState: .e),
.init(write: 1, move: .right, nextState: .a),
],
]
)
}
}