-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday07_palindromes.dart
153 lines (127 loc) · 4.09 KB
/
day07_palindromes.dart
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 'dart:io';
// ########
// SOLUTION
// ########
List<String> parseInput(String puzzleInput) {
return puzzleInput.trim().split("\n").map((item) => item.trim()).toList();
}
List<List<dynamic>> parseLine(String line) {
final supernetSequences = [];
final hypernetSequences = [];
var currentPosition = 0;
while (currentPosition < line.length) {
int startBracketPos = line.substring(currentPosition).indexOf("[");
int endBracketPos = line.substring(currentPosition).indexOf("]");
if (startBracketPos > 0 && startBracketPos < endBracketPos) {
final word =
line.substring(currentPosition, currentPosition + startBracketPos);
supernetSequences.add(word);
currentPosition += startBracketPos + 1;
} else if (endBracketPos > 0) {
final word =
line.substring(currentPosition, currentPosition + endBracketPos);
hypernetSequences.add(word);
currentPosition += endBracketPos + 1;
} else if (startBracketPos < 0 && endBracketPos < 0) {
final word = line.substring(currentPosition);
supernetSequences.add(word);
currentPosition = line.length;
}
}
return [supernetSequences, hypernetSequences];
}
bool containsFourLetterPalindrome(String word) {
for (int i = 0; i < word.length; i++) {
try {
var sequence = word.substring(i, i + 4);
if (sequence.substring(0, 1) != sequence.substring(3, 4)) continue;
if (sequence.substring(1, 2) != sequence.substring(2, 3)) continue;
if (sequence.substring(0, 1) == sequence.substring(1, 2)) continue;
return true;
} catch (RangeError) {
return false;
}
}
return false;
}
bool supportsTls(String line) {
var parsedSequences = parseLine(line);
final supernetSequences = parsedSequences[0]
.map((word) => containsFourLetterPalindrome(word))
.toList();
final hypernetSequences = parsedSequences[1]
.map((word) => containsFourLetterPalindrome(word))
.toList();
if (hypernetSequences.contains(true)) return false;
if (supernetSequences.contains(true)) return true;
return false;
}
int part1(puzzleInput) {
return parseInput(puzzleInput)
.map((line) => supportsTls(line))
.toList()
.where((value) => value)
.length;
}
List<String> findThreeLetterPalindrome(word) {
List<String> palindromes = [];
for (int i = 0; i < word.length; i++) {
try {
var sequence = word.substring(i, i + 3);
if (sequence.substring(0, 1) != sequence.substring(2, 3)) continue;
if (sequence.substring(0, 1) == sequence.substring(1, 2)) continue;
palindromes.add(sequence);
} catch (RangeError) {
continue;
}
}
return palindromes;
}
bool supportSsl(String line) {
var parsedSequences = parseLine(line);
final supernetPalindromes = parsedSequences[0]
.map((word) => findThreeLetterPalindrome(word))
.expand((element) => element)
.toList();
final hypernetHasReversePalindrome = parsedSequences[1].map((word) {
for (final palindrome in supernetPalindromes) {
final textToCheck = "${palindrome[1]}${palindrome[0]}${palindrome[1]}";
if (word.contains(textToCheck)) return true;
}
return false;
});
return hypernetHasReversePalindrome.contains(true);
}
int part2(puzzleInput) {
return parseInput(puzzleInput)
.map((line) => supportSsl(line))
.toList()
.where((value) => value)
.length;
}
// ###########
// RUN PROGRAM
// ###########
const TEST_INPUT1 = """abba[mnop]qrst
abcd[bddb]xyyx
aaaa[qwer]tyui
ioxxoj[asdfgh]zxcvbn\n""";
const TEST_INPUT2 = """aba[bab]xyz
xyx[xyx]xyx
aaa[kek]eke
zazbz[bzb]cdb\n""";
void main() {
String puzzleInput = File('data/day07_input.txt').readAsStringSync();
// part 1
assert(part1(TEST_INPUT1) == 2);
final stopwatchPart1 = Stopwatch()..start();
print("part 1: ${part1(puzzleInput)}");
stopwatchPart1.stop();
print("Elapsed time: ${stopwatchPart1.elapsed}");
// part 2
assert(part2(TEST_INPUT2) == 3);
final stopwatchPart2 = Stopwatch()..start();
print("part 2: ${part2(puzzleInput)}");
stopwatchPart2.stop();
print("Elapsed time: ${stopwatchPart2.elapsed}");
}