-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6_part1.jakt
200 lines (167 loc) · 5.66 KB
/
day6_part1.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/// Expect:
/// - output: "3604\n"
import reader
import utility
function main() {
mut coords: [u32:[u32:[(usize, usize)]]] = [:]
mut visit: [(u32, u32, usize)] = []
mut x_bound = 0u32
mut y_bound = 0u32
for (id, coord_str) in utility::enumerate(reader::lines_string(day: 6).iterator()) {
let coord_parts = coord_str.split(',')
let x = coord_parts[0].to_uint()!
let y = coord_parts[1].substring(start: 1, length: coord_parts[1].length() - 1).to_uint()!
if not coords.contains(y) {
coords[y] = [:]
}
if not coords[y].contains(x) {
coords[y][x] = []
}
coords[y][x].push((id, 0uz))
visit.push((x, y, id))
if x > x_bound {
x_bound = x
}
if y > y_bound {
y_bound = y
}
}
mut expand_list: [(usize, u32, u32, usize)] = []
for (x, y, id) in visit.iterator() {
mut new_expand_list = expand(coords, id, x, y, x_bound, y_bound, step: 0uz)
for new_expand in new_expand_list.iterator() {
expand_list.push(new_expand)
}
}
mut i = 0uz
while i < expand_list.size() {
let (id, next_x, next_y, next_step) = expand_list[i]
i++
let new_expand_list = expand(coords, id, x: next_x, y: next_y, x_bound, y_bound, step: next_step)
for new_expand in new_expand_list.iterator() {
expand_list.push(new_expand)
}
}
count(coords, x_bound, y_bound)
}
function current_id_step(coord: [(usize, usize)], check_id: usize) -> usize {
mut smallest = 9999uz
for (id, step) in coord.iterator() {
if id == check_id and step < smallest {
smallest = step
}
}
return smallest
}
function expand(mut coords: [u32:[u32:[(usize, usize)]]], id: usize, x: u32, y:u32, x_bound: u32, y_bound: u32, step: usize) throws -> [(usize, u32, u32, usize)] {
if not coords.contains(y) {
coords[y] = [:]
}
if not coords[y].contains(x) {
coords[y][x] = []
}
if coords[y][x].size() > 0 {
utility::sort(values: coords[y][x], sorter: &function(anon item_a: &(usize, usize), anon item_b: &(usize, usize)) -> i8 {
if item_a.1 > item_b.1 {
return 1
} else if item_a.1 < item_b.1 {
return -1
} else {
return 0
}
});
}
mut should_expand = false
if coords[y][x].size() == 0 {
coords[y][x].push((id, step))
should_expand = true
} else if(coords[y][x][0].1 >= step and current_id_step(coord: coords[y][x], check_id: id) == 9999) {
coords[y][x].push((id, step))
should_expand = true
} else if step == 0 {
should_expand = true
}
if coords[y][x].size() > 1 {
utility::sort(values: coords[y][x], sorter: &function(anon item_a: &(usize, usize), anon item_b: &(usize, usize)) -> i8 {
if item_a.1 > item_b.1 {
return 1
} else if item_a.1 < item_b.1 {
return -1
} else {
return 0
}
});
}
mut expand_list: [(usize, u32, u32, usize)] = []
if should_expand {
let next_step = step + 1
if x > 0 {
expand_list.push((id, x-1, y, next_step))
}
if x < x_bound {
expand_list.push((id, x+1, y, next_step))
}
if y > 0 {
expand_list.push((id, x, y-1, next_step))
}
if y < y_bound {
expand_list.push((id, x, y+1, next_step))
}
}
return expand_list
}
function print_coords(coords: [u32:[u32:[(usize, usize)]]]) {
for y in 0..coords.size() {
if coords.contains(y as! u32) {
for x in 0..coords[y].size() {
if coords[y].contains(x as! u32) {
if coords[y][x].size() > 1 and coords[y][x][0].1 == coords[y][x][1].1 {
print("(.) ")
} else {
print("({}-{}) ", (coords[y][x][0].0 + 65) as! c_char, coords[y][x][0].1)
}
} else {
print("x ")
}
}
} else {
print("x")
}
print("\n")
}
}
function count(coords: [u32:[u32:[(usize, usize)]]], x_bound: u32, y_bound: u32) throws {
mut id_count: [usize: usize] = [:]
mut valid_id: [usize: bool] = [:]
mut largest_count = 0uz
mut largest_id = 0uz
for y in 0..coords.size() {
if coords.contains(y as! u32) {
for x in 0..coords[y].size() {
if coords[y].contains(x as! u32) {
if coords[y][x].size() == 1 or
(coords[y][x].size() > 1 and coords[y][x][0].1 < coords[y][x][1].1) {
if not id_count.contains(coords[y][x][0].0) {
id_count[coords[y][x][0].0] = 1
valid_id[coords[y][x][0].0] = true
} else {
id_count[coords[y][x][0].0]++
}
if y == 0 or y == y_bound as! usize {
valid_id[coords[y][x][0].0] = false
} else if x == 0 or x == x_bound as! usize {
valid_id[coords[y][x][0].0] = false
}
}
}
}
}
}
for id in id_count.keys().iterator() {
if id_count[id] > largest_count && valid_id[id] {
largest_count = id_count[id]
largest_id = id
}
}
println("{}", largest_count)
}