-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.nim
153 lines (121 loc) · 3.79 KB
/
day19.nim
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import sets
import strscans
import strutils
import sugar
import tables
type
RuleType {.pure.} = enum
Value
RuleList
RuleOption
Rule = int
RuleContent = object
ruleType: RuleType
value: char
ruleList: seq[Rule]
var ruleDirectory = newTable[Rule, RuleContent]()
var auxiliaryRuleId = -1
proc ruleType(rule: Rule): RuleType =
ruleDirectory[rule].ruleType
proc value(rule: Rule): char =
ruleDirectory[rule].value
proc ruleList(rule: Rule): seq[Rule] =
ruleDirectory[rule].ruleList
# Rule checking procedures
proc checkRule(rule: Rule, message: string, index: int): HashSet[int]
proc checkRuleValue(rule: Rule, message: string, index: int): HashSet[int] =
if message[index] == rule.value:
toHashSet([index + 1])
else:
initHashSet[int]()
proc checkRuleList(rules: Rule, message: string, index: int): HashSet[int] =
var indexSet = toHashSet([index])
for rule in rules.ruleList:
var nextIndexSet = initHashSet[int]()
for idx in indexSet:
nextIndexSet.incl(checkRule(rule, message, idx))
indexSet = nextIndexSet
indexSet
proc checkRuleOption(rules: Rule, message: string, index: int): HashSet[int] =
var indexSet = initHashSet[int]()
for rule in rules.ruleList:
indexSet.incl(checkRule(rule, message, index))
indexSet
proc checkRule(rule: Rule, message: string, index: int): HashSet[int] =
if index >= message.len:
return initHashSet[int]()
var handler: (Rule, string, int) -> HashSet[int]
case rule.ruleType:
of Value:
handler = checkRuleValue
of RuleList:
handler = checkRuleList
of RuleOption:
handler = checkRuleOption
handler(rule, message, index)
proc verifyMessage(message: string): bool =
for parsedCharacters in checkRule(0, message, 0):
if parsedCharacters == message.len:
return true
return false
proc parseRule(ruleString: string) =
# Initial parsing
var ruleId: Rule
var ruleContentString: string
discard scanf(ruleString, "$i: $+$.", ruleId, ruleContentString)
# Parse rule conditions
if '"' in ruleContentString:
# Value
ruleDirectory[ruleId] = RuleContent(ruleType: Value,
value: ruleContentString[1], ruleList: @[])
elif '|' in ruleContentString:
# Rule options
ruleDirectory[ruleId] = RuleContent(ruleType: RuleOption, value: '\0',
ruleList: @[])
for optionString in ruleContentString.split('|'):
ruleDirectory[auxiliaryRuleId] = RuleContent(ruleType: RuleList,
value: '\0', ruleList: @[])
for optionStringElement in optionString.split(' '):
if not optionStringElement.isEmptyOrWhitespace:
ruleDirectory[auxiliaryRuleId].ruleList.add(parseInt(optionStringElement))
ruleDirectory[ruleId].ruleList.add(auxiliaryRuleId)
dec auxiliaryRuleId
else:
# Rule list
ruleDirectory[ruleId] = RuleContent(ruleType: RuleList,
value: ruleContentString[1], ruleList: @[])
for optionStringElement in ruleContentString.split(' '):
ruleDirectory[ruleId].ruleList.add(parseInt(optionStringElement))
# Parse input
let inputFile = "resources/day19_input.txt"
var parsingRules = true
var correctMessages = 0
for line in lines inputFile:
if parsingRules:
if not line.isEmptyOrWhitespace:
line.parseRule
else:
parsingRules = false
else:
if line.verifyMessage:
inc correctMessages
# Print results
echo "--- Part 1 Report ---"
echo "Correct messages = " & $correctMessages
## Part 2
# Parse new rules
"8: 42 | 42 8".parseRule
"11: 42 31 | 42 11 31".parseRule
parsingRules = true
correctMessages = 0
for line in lines inputFile:
if parsingRules:
if not line.isEmptyOrWhitespace:
continue
else:
parsingRules = false
else:
if line.verifyMessage:
inc correctMessages
echo "--- Part 2 Report ---"
echo "Correct messages = " & $correctMessages