My omnium-gatherom of scripts and source code.
1use std::env; // to access arguments
2
3// code.golf functions begin here:
4
5//fn twelve_days() {}
6//fn bottles_of_beer() {}
7
8fn abundant() {
9 for i in 0..201 {
10 if (1..i).into_iter().filter(|x| i % x == 0).sum::<u64>() > i {
11 println!("{i}");
12 }
13 }
14}
15
16fn abundant_long() {
17 for i in 0..1001 {
18 if (1..i).into_iter().filter(|x| i % x == 0).sum::<u64>() > i {
19 println!("{i}");
20 }
21 }
22}
23
24//fn arabic_roman() {}
25//fn arrows() {}
26//fn ascii_table() {}
27//fn bf() {}
28//fn catalan_numbers() {}
29//fn catalan_const() {}
30//fn christmas_tree() {}
31
32fn collatz() {
33 fn calculator(n: u64, iteration: &mut u64) -> u64 {
34 *iteration += 1;
35 match n {
36 0 | 1 => 1,
37 n if n % 2 == 0 => calculator(n / 2, iteration),
38 _ => calculator(n * 3 + 1, iteration),
39 };
40 *iteration - 1
41 }
42
43 for i in 1..1001 {
44 let mut count: u64 = 0;
45 println!("{}", calculator(i, &mut count));
46 }
47}
48
49//fn css_colors() {}
50//fn cubes() {}
51//fn diamonds() {}
52
53fn divisors() {
54 for i in 1u64..101 {
55 (1..=i)
56 .into_iter()
57 .filter(|&x| i % x == 0)
58 .for_each(|m| print!("{m} "));
59 println!();
60 }
61}
62
63fn emirp() {
64 fn prime(n: u64) -> bool {
65 (1..n / 2)
66 .into_iter()
67 .filter(|x| n % x == 0)
68 .collect::<Vec<u64>>()
69 .len()
70 < 2
71 }
72 for i in 1..1001 {
73 let rv: u64 = i
74 .to_string()
75 .chars()
76 .rev()
77 .collect::<String>()
78 .parse()
79 .unwrap();
80
81 if rv != i && prime(i) && prime(rv) {
82 println!("{i}");
83 }
84 }
85}
86
87fn emirp_long() {
88 fn prime(n: u64) -> bool {
89 (1..n / 2)
90 .into_iter()
91 .filter(|&x| n % x == 0)
92 .collect::<Vec<u64>>()
93 .len()
94 < 2
95 }
96 for i in 1..10001 {
97 let rv: u64 = i
98 .to_string()
99 .chars()
100 .rev()
101 .collect::<String>()
102 .parse()
103 .unwrap();
104
105 if rv != i && prime(i) && prime(rv) {
106 println!("{i}");
107 }
108 }
109}
110
111//fn emojify() {}
112
113fn evil() {
114 for i in 1u8..51 {
115 if i.count_ones() % 2 == 0 {
116 println!("{i}")
117 }
118 }
119}
120
121fn evil_long() {
122 for i in 1u64..1001 {
123 if i.count_ones() % 2 == 0 {
124 println!("{i}")
125 }
126 }
127}
128
129fn fibonacci() {
130 fn fib(n: u32) -> u32 {
131 match n {
132 0 => 0,
133 1 => 1,
134 _ => fib(n - 1) + fib(n - 2),
135 }
136 }
137 for i in 0..31 {
138 println!("{}", fib(i));
139 }
140}
141
142fn fizzbuzz() {
143 for i in 1u16..101 {
144 let mut out = String::new();
145
146 if i % 3 == 0 {
147 out += "Fizz"
148 }
149 if i % 5 == 0 {
150 out += "Buzz"
151 }
152
153 if out.is_empty() {
154 println!("{i}")
155 } else {
156 println!("{out}")
157 }
158 }
159}
160
161fn foofizzbuzzbar() {
162 for i in 1u16..1001 {
163 let mut out = String::new();
164
165 if i % 2 == 0 {
166 out += "Foo"
167 }
168 if i % 3 == 0 {
169 out += "Fizz"
170 }
171 if i % 5 == 0 {
172 out += "Buzz"
173 }
174 if i % 7 == 0 {
175 out += "Bar"
176 }
177
178 if out.is_empty() {
179 println!("{i}")
180 } else {
181 println!("{out}")
182 }
183 }
184}
185
186//fn fractions() {}
187//fn g_o_l() {}
188
189fn happy() {
190 fn dc(n: u32, vec: Vec<u32>) -> Vec<u32> {
191 n.to_string()
192 .chars()
193 .map(|d| d.to_digit(10).unwrap().pow(2))
194 .fold(0, |acc, elem| acc * 10 + elem)
195 }
196 for i in 1u32..201 {
197 let mut ints = vec![i];
198 println!("{}", dc(i, &mut ints));
199 }
200}
201
202//fn happy_long() {}
203//fn hexdump() {}
204//fn intersection() {}
205//fn inventory() {}
206//fn isbn() {}
207//fn jacobi() {}
208//fn kolakoski() {}
209//fn kolakoski_seq() {}
210//fn leap() {}
211//fn levenshtein() {}
212//fn leyland() {}
213//fn looknsay() {}
214//fn lucky_numbers() {}
215//fn lucky_tickets() {}
216//fn maze() {}
217//fn morse_dec() {}
218//fn morse_enc() {}
219//fn music_chords() {}
220//fn niven() {}
221//fn niven_long() {}
222//fn num_spiral() {}
223//fn odious() {}
224//fn odious_long() {}
225//fn ordinal() {}
226//fn pangram() {}
227//fn pascal_triangle() {}
228//fn pernicious_numbers() {}
229//fn pernicious_long() {}
230//fn poker() {}
231//fn prime() {}
232//fn prime_long() {}
233//fn proximity_grid() {}
234//fn qr_decoder() {}
235//fn quine() {}
236//fn recaman() {}
237//fn repeating_decimals() {}
238//fn reverse_polish() {}
239//fn rpssp() {}
240//fn roman_arabic() {}
241//fn rule_110() {}
242//fn seven_segment() {}
243//fn sierpinski() {}
244//fn smith() {}
245//fn spelling() {}
246//fn star_wars() {}
247//fn sudoku() {}
248//fn sudoku_v2() {}
249//fn time_distance() {}
250//fn tongue() {}
251//fn us() {}
252//fn vampire() {}
253//fn van_eck() {}
254//fn zodiac() {}
255//fn gamma() {}
256//fn lambda() {}
257//fn pi() {}
258//fn tau() {}
259//fn phi() {}
260//fn root2() {}
261//fn euler() {}
262
263fn main() {
264 let args: Vec<String> = env::args().collect();
265 let hole_labels: Vec<(&str, fn())> = vec![
266 //("12days", twelve_days),
267 //("99bottles", bottles_of_beer),
268 ("abundant", abundant),
269 ("abundant-long", abundant_long),
270 //("arabic-to-roman", arabic_roman),
271 //("arrows", arrows),
272 //("ascii-table", ascii_table),
273 //("brainfuck", bf),
274 //("Catalan-numbers", catalan_numbers),
275 //("Catalan-constant", catalan_const),
276 //("christmas-tree", christmas_tree),
277 ("collatz", collatz),
278 //("css", css_colors),
279 //("cubes", cubes),
280 //("diamonds", diamonds),
281 ("divisors", divisors),
282 ("emirp", emirp),
283 ("emirp-long", emirp_long),
284 //("emojify", emojify),
285 ("evil", evil),
286 ("evil-long", evil_long),
287 ("Fibonacci", fibonacci),
288 ("FizzBuzz", fizzbuzz),
289 ("FooFizzBuzzBar", foofizzbuzzbar),
290 //("fractions", fractions),
291 //("game-of-life", g_o_l),
292 ("happy", happy),
293 //("happy-long", happy_long),
294 //("hexdump", hexdump),
295 //("intersection", intersection),
296 //("inventory-sequence", inventory),
297 //("ISBN", isbn),
298 //("Jacobi-symbol", jacobi),
299 //("Kolakoski-constant", kolakoski),
300 //("Kolakoski-sequence", kolakoski_seq),
301 //("leap", leap),
302 //("Levenshtein", levenshtein),
303 //("Leyland", leyland),
304 //("look-and-say", looknsay),
305 //("lucky-numbers", lucky_numbers),
306 //("lucky-tickets", lucky_tickets),
307 //("maze", maze),
308 //("morse-decoder", morse_dec),
309 //("morse-encoder", morse_enc),
310 //("musical-chords", music_chords),
311 //("Niven", niven),
312 //("Niven-long", niven_long),
313 //("number-spiral", num_spiral),
314 //("odious", odious),
315 //("odious-long", odious_long),
316 //("ordinal-numbers", ordinal),
317 //("pangram-grep", pangram),
318 //("Pascal-triangle", pascal_triangle),
319 //("pernicious-numbers", pernicious_numbers),
320 //("pernicious-long", pernicious_long),
321 //("poker", poker),
322 //("prime", prime),
323 //("prime-long", prime_long),
324 //("proximity-grid", proximity_grid),
325 //("QR-decoder", qr_decoder),
326 //("Quine", quine),
327 //("Recaman", recaman),
328 //("repeating-decimals", repeating_decimals),
329 //("reverse-polish", reverse_polish),
330 //("rock-papers-scissors-spock", rpssp),
331 //("roman-to-arabic", roman_arabic),
332 //("rule110", rule_110),
333 //("seven-segment", seven_segment),
334 //("Sierpinski-triangle", sierpinski),
335 //("Smith-numbers", smith),
336 //("spelling-numbers", spelling),
337 //("Star-Wars-crawl", star_wars),
338 //("sudoku", sudoku),
339 //("sudokuv2", sudoku_v2),
340 //("time-distance", time_distance),
341 //("tongue-twister", tongue),
342 //("US", us),
343 //("vampire-numbersr", vampire),
344 //("Van-Eck-sequence", van_eck),
345 //("zodiac-signs", zodiac),
346 //("Euler-Mascheroni", gamma),
347 //("Conway", lambda),
348 //("pi", pi),
349 //("tau", tau),
350 //("golden-ratio", phi),
351 //("Pythagoras", root2),
352 //("Euler", euler),
353 ];
354 if args.len() == 1 {
355 println!("Error: No hole specified.");
356 println!("The holes available are:\n");
357 for i in &hole_labels {
358 println!("\t{}", i.0);
359 }
360 panic!("To view a hole: golf {{HOLE}}");
361 }
362
363 if args.len() == 2 {
364 let index = hole_labels
365 .iter()
366 .position(|&r| {
367 return r.0
368 == args
369 .get(1)
370 .expect("This literally shouldn't be possible")
371 .as_str();
372 })
373 .expect("Hole not found");
374 hole_labels
375 .get(index)
376 .expect("Error indexing hole tuples array")
377 .1();
378 }
379 // "-h" | "--help" | "help" => {
380 // println!("");
381 // println!("The holes available are:\n");
382 // for i in &hole_labels {
383 // println!("\t{}", i.0)
384 // }
385}