-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5_part2.jakt
99 lines (80 loc) · 2.13 KB
/
day5_part2.jakt
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
/// Expect:
/// - output: "6188\n"
import reader
function previous_char(chars: &[u8], current: usize) -> usize {
if current > 0 {
let previous = current - 1
for i in previous..0 {
if chars[i] != 0 {
return i
}
}
}
return 0
}
function next_char(chars: &[u8], current: usize) -> usize {
let next = current + 1
if next < chars.size() {
for i in next..chars.size() {
if chars[i] != 0 {
return i
}
}
}
return 0
}
function count_chars(chars: &[u8]) -> i32 {
mut count = 0i32
for char_ in chars.iterator() {
if char_ != 0 {
count++
}
}
return count
}
function remove_char(chars: [u8], char_: u8) throws -> [u8] {
mut chars_removed = chars
for i in 0..chars.size() {
if chars[i] == char_ or chars[i] == char_ + 32 {
chars_removed[i] = 0
}
}
return chars_removed
}
function main() {
mut chars_base = reader::chars(day: 5)
mut start = 0uz
mut ended = false
mut lowest_count = 100000i32
for z in 65..91 {
mut chars = remove_char(chars: chars_base[..].to_array(), char_: z as! u8)
loop {
mut i = 0uz
while i < chars.size() {
let char_ = chars[i]
let next = next_char(&chars, current: i)
if next != 0 {
if (char_ > chars[next] and char_ - chars[next] == 32) or
(chars[next] > char_ and chars[next] - char_ == 32) {
chars[i] = 0
chars[next] = 0
i = previous_char(&chars, current: i)
} else {
i = next
}
} else {
ended = true
break
}
}
if ended {
break
}
}
let count = count_chars(&chars)
if count < lowest_count {
lowest_count = count
}
}
println("{}", lowest_count)
}