diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-05 13:03:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-05 13:03:23 +0100 |
| commit | 6a17c4e3d88b12390a5f386aa5efb2619da88266 (patch) | |
| tree | a97e5c785bc5473a3c571e3e8c504865f5a00ca1 | |
| parent | 421e5759d57ff15670dbc4438a1b2ff4f9e6092d (diff) | |
| parent | b5381a91037887e8e0b9b01981de16bab8891d3b (diff) | |
| download | perlweeklychallenge-club-6a17c4e3d88b12390a5f386aa5efb2619da88266.tar.gz perlweeklychallenge-club-6a17c4e3d88b12390a5f386aa5efb2619da88266.tar.bz2 perlweeklychallenge-club-6a17c4e3d88b12390a5f386aa5efb2619da88266.zip | |
Merge pull request #10544 from zapwai/branch-for-281
Week 281
| -rw-r--r-- | challenge-281/zapwai/c/ch-1.c | 30 | ||||
| -rw-r--r-- | challenge-281/zapwai/c/ch-2.c | 96 | ||||
| -rw-r--r-- | challenge-281/zapwai/c/vec.h | 293 | ||||
| -rw-r--r-- | challenge-281/zapwai/javascript/ch-1.js | 26 | ||||
| -rw-r--r-- | challenge-281/zapwai/javascript/ch-2.js | 69 | ||||
| -rw-r--r-- | challenge-281/zapwai/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-281/zapwai/perl/ch-2.pl | 62 | ||||
| -rw-r--r-- | challenge-281/zapwai/python/ch-1.py | 23 | ||||
| -rw-r--r-- | challenge-281/zapwai/python/ch-2.py | 53 | ||||
| -rw-r--r-- | challenge-281/zapwai/rust/ch-1.rs | 27 | ||||
| -rw-r--r-- | challenge-281/zapwai/rust/ch-2.rs | 77 |
11 files changed, 782 insertions, 0 deletions
diff --git a/challenge-281/zapwai/c/ch-1.c b/challenge-281/zapwai/c/ch-1.c new file mode 100644 index 0000000000..b7b988f5d5 --- /dev/null +++ b/challenge-281/zapwai/c/ch-1.c @@ -0,0 +1,30 @@ +#include <stdio.h> + +void proc(char coord[]) { + printf("Input: %s\n", coord); + char l = coord[0]; + int n = coord[1] - '0'; + if (l == 'a' || l == 'c' || l == 'e' || l == 'g') { + if (n % 2 == 0) { + printf("Output: True\n"); + } else { + printf("Output: False\n"); + } + } else { + if (n % 2 == 1) { + printf("Output: True\n"); + } else { + printf("Output: False\n"); + } + } +} + +int main() { + char coord[] = "d3"; + proc(coord); + char coord2[] = "g5"; + proc(coord2); + char coord3[] = "e6"; + proc(coord3); +} + diff --git a/challenge-281/zapwai/c/ch-2.c b/challenge-281/zapwai/c/ch-2.c new file mode 100644 index 0000000000..41a8207944 --- /dev/null +++ b/challenge-281/zapwai/c/ch-2.c @@ -0,0 +1,96 @@ +#include <stdio.h> +#include "vec.h" + +vec* moves(char coord[]) { + char l = coord[0]; + int row = atoi(&coord[1]); + char *cols = "abcdefgh"; + char *k; + k = strchr(cols, l); + int col = 1 + (int)(k - cols); + vec *r = new_vec('i'); + vec *c = new_vec('i'); + for (int i = -2; i <= 2; i++) { + if (i == -2 || i == 2) { + for (int j = -1; j <= 1; j++) { + if (j == -1 || j == 1) { + int cval = col + i; + push(c, &cval); + int rval = row + j; + push(r, &rval); + } + } + } + } + for (int i = -1; i <= 1; i++) { + if (i == -1 || i == 1) { + for (int j = -2; j <= 2; j++) { + if (j == -2 || j == 2) { + int cval = col + i; + push(c, &cval); + int rval = row + j; + push(r, &rval); + } + } + } + } + vec *list = new_vec('s'); + for (int i = 0; i <= 7; i++) { + if (c->data[i] < 1 || c->data[i] > 8) + continue; + if (r->data[i] < 1 || r->data[i] > 8) + continue; + char move[3]; + move[0] = cols[c->data[i] - 1]; + move[1] = r->data[i] + '0'; + move[2] = '\0'; + push(list, move); + } + destroy_vec(c); + destroy_vec(r); + return list; +} + +void proc(char start[], char end[]) { + printf("Input: %s to %s\n", start, end); + vec* list[10]; + list[0] = moves(start); + int round = -1; + int found = 0; + while (found == 0) { + round++; + for (int i = 0; i < list[round]->size; i++) { + char* old_move = list[round]->datas[i]; + if (0 == strcmp(old_move, end)) + found = 1; + } + if (found == 1) { + break; + } else { + list[round+1] = new_vec('s'); + vec* L[list[round]->size]; + for (int i = 0; i < list[round]->size; i++) { + char* old_move = list[round]->datas[i]; + L[i] = moves(old_move); + for (int j = 0; j < L[i]->size; j++) { + char* ch = L[i]->datas[j]; + push(list[round+1], ch); + } + } + } + } + printf( "Output: %d\n", round+1); + for (int i = 0; i < round; i++) + destroy_vec(list[i]); +} + + +int main() { + char start[] = "g2"; + char end[] = "a8"; + proc(start, end); + + char start2[] = "g2"; + char end2[] = "h2"; + proc(start2, end2); +} diff --git a/challenge-281/zapwai/c/vec.h b/challenge-281/zapwai/c/vec.h new file mode 100644 index 0000000000..ead044082e --- /dev/null +++ b/challenge-281/zapwai/c/vec.h @@ -0,0 +1,293 @@ +#ifndef VEC_H +#define VEC_H +#include <stdlib.h> +#include <string.h> +#define VECMAXSTRLEN 50 +#define VECCAP 10 +typedef struct vector { + size_t cap; /* actual size allocated (reduces calls to realloc)*/ + size_t size; /* size of data */ + int* data; /* integer data */ + char* datac; /* char data */ + char** datas; /* string data */ + char type; /* i, c, or s */ +} vec; + +vec* new_vec(char t) { + struct vector* v = malloc(sizeof(vec)); + if ( t != 'c' && t != 'i' && t != 's') { + exit(1); + } + v->type = t; + v->size = 0; + v->cap = VECCAP; + v->data = malloc(v->cap * sizeof(int)); + v->datac = malloc(v->cap * sizeof(char)); + v->datas = malloc(v->cap * sizeof(char*)); + for (int i = 0; i < v->cap; i++) + v->datas[i] = malloc(VECMAXSTRLEN); + return v; +} + +void destroy_vec(struct vector* v) { + free (v->data); /* good habit */ + free (v->datac); + for (int i = 0; i < v->cap; i++) + free(v->datas[i]); + free (v->datas); + free(v); +} + +/* If a vector needs more memory, add VECCAP (e.g. 10) slots */ +void stretch_vec(struct vector* v) { + if (v->size == v->cap) { + v->cap += VECCAP; + switch(v->type) { + case 'i': + v->data = realloc(v->data, sizeof(int) * v->cap); + break; + case 'c': + v->datac = realloc(v->datac, sizeof(char) * v->cap); + break; + case 's': + v->datas = realloc(v->datas, sizeof(char*) * v->cap); + for (int i = v->size; i < v->cap; i++) + v->datas[i] = malloc(VECMAXSTRLEN); + break; + } + } +} + +void push(struct vector* v, void* value) { + v->size++; + stretch_vec(v); + switch(v->type) { + case 'i': + v->data[v->size - 1] = *(int *) value; + break; + case 'c': + v->datac[v->size - 1] = *(char *) value; + break; + case 's': + strcpy(v->datas[v->size - 1], (char *) value); + break; + } +} + + +void unshift(struct vector* v, void* value) { + v->size++; + stretch_vec(v); + switch(v->type) { + case 'i': + for (int i = v->size - 1; i > 0; i--) + v->data[i] = v->data[i - 1]; + v->data[0] = * (int*) value; + break; + case 'c': + for (int i = v->size - 1; i > 0; i--) + v->datac[i] = v->datac[i - 1]; + v->datac[0] = * (char*) value; + break; + case 's': + for (int i = v->size - 1; i > 0; i--) + strcpy(v->datas[i], v->datas[i - 1]); + strcpy(v->datas[0], (char*) value); + break; + } +} + +void* shift(struct vector* v) { + void *x = NULL; + if (v->size == 0) return NULL; + v->size--; + switch(v->type) { + case 'i': + x = malloc(sizeof(int)); + if (x == NULL) return NULL; + *(int*)x = v->data[0]; + for (int i = 0; i < v->size; i++) + v->data[i] = v->data[i + 1]; + v->data[v->size] = 0; + break; + case 'c': + x = malloc(sizeof(char)); + if (x == NULL) return NULL; + *(char *)x = v->datac[0]; + for (int i = 0; i < v->size; i++) + v->datac[i] = v->datac[i + 1]; + v->datac[v->size] = '\0'; + break; + case 's': + x = malloc(VECMAXSTRLEN); + if (x == NULL) return NULL; + strcpy(x, v->datas[0]); + for (int i = 0; i < v->size; i++) + strcpy(v->datas[i], v->datas[i + 1]); + strcpy(v->datas[v->size], ""); + break; + } + return x; +} + +void* pop(struct vector* v) { + void *x = NULL; + if (v->size == 0) return NULL; + v->size--; + switch(v->type) { + case 'i': + x = malloc(sizeof(int)); + *(int*)x = v->data[v->size]; + v->data[v->size] = 0; + break; + case 'c': + x = malloc(sizeof(char)); + *(char*)x = v->datac[v->size]; + v->datac[v->size] = '\0'; + break; + case 's': + x = malloc(VECMAXSTRLEN); + strcpy(x, v->datas[v->size]); + strcpy(v->datas[v->size], " "); + break; + } + return x; +} + +void push_list(struct vector* v, void* list, int listlen) { + for (int i = 0; i < listlen; i++) + switch(v->type) { + case 'i': + push(v, &((int*)list)[i]); + break; + case 'c': + push(v, &((char*)list)[i]); + break; + case 's': + push(v, ((char**)list)[i]); + break; + } +} + +void sort(struct vector* v) { + int cnt; + do { + cnt = 0; + for (int i = 0; i < v->size - 1; i++) { + switch(v->type) { + case 'i': + if (v->data[i] > v->data[i + 1]) { + cnt++; + int tmp = v->data[i]; + v->data[i] = v->data[i+1]; + v->data[i+1] = tmp; + } + break; + case 'c': + if (v->datac[i] > v->datac[i + 1]) { + cnt++; + char tmp = v->datac[i]; + v->datac[i] = v->datac[i+1]; + v->datac[i+1] = tmp; + } + break; + case 's': + if (strcmp(v->datas[i], v->datas[i + 1]) > 0) { + cnt++; + char* tmp = v->datas[i]; + v->datas[i] = v->datas[i+1]; + v->datas[i+1] = tmp; + } + break; + } + } + } while(cnt > 0); +} + +void reverse(struct vector* v) { + if (v->size == 1) return; + int k = v->size / 2; + for (int i = 0; i < k; i++) { + int idx = (v->size % 2 == 0) ? k+i : k+1+i; + int idy = k - i - 1; + switch(v->type) { + case 'i': + { + int x = v->data[idy]; + v->data[idy] = v->data[idx]; + v->data[idx] = x; + } + break; + case 'c': + { + char x = v->datac[k-1-i]; + v->datac[idy] = v->datac[idx]; + v->datac[idx] = x; + } + break; + case 's': + { + char* x = v->datas[k-1-i]; + v->datas[idy] = v->datas[idx]; + v->datas[idx] = x; + } + break; + } + } +} + +void display(struct vector* v) { + printf("["); + if (v->size > 0){ + switch(v->type) { + case 'i': + for (int i = 0; i < v->size - 1; i++) printf("%d, ", v->data[i]); + printf("%d", v->data[v->size-1]); + break; + case 'c': + for (int i = 0; i < v->size - 1; i++) printf("%c, ", v->datac[i]); + printf("%c", v->datac[v->size-1]); + break; + case 's': + for (int i = 0; i < v->size - 1; i++) printf("%s, ", v->datas[i]); + printf("%s", v->datas[v->size-1]); + break; + } + } + printf("]"); +} + +void* get(struct vector* v, int k) { + void* x = NULL; + switch(v->type) { + case 'i': + x = malloc(sizeof(int)); + *(int*) x = v->data[k]; + break; + case 'c': + x = malloc(sizeof(char)); + *(char*) x = v->datac[k]; + break; + case 's': + x = malloc(VECMAXSTRLEN); + strcpy(x, v->datas[k]); + break; + } + return x; +} + +void set(struct vector* v, int k, void* value) { + switch(v->type) { + case 'i': + v->data[k] = *(int*) value; + break; + case 'c': + v->datac[k] = *(char*) value; + break; + case 's': + strcpy(v->datas[k], (char*) value); + break; + } + +} +#endif diff --git a/challenge-281/zapwai/javascript/ch-1.js b/challenge-281/zapwai/javascript/ch-1.js new file mode 100644 index 0000000000..c11aa66659 --- /dev/null +++ b/challenge-281/zapwai/javascript/ch-1.js @@ -0,0 +1,26 @@ +let coord = "d3"; +proc(coord); +coord = "g5"; +proc(coord); +coord = "e6"; +proc(coord); + +function proc(coord) { + console.log( "Input:", coord); + a = coord.split(""); + l = a[0] + n = a[1] + if (l == "a" || l == "c" || l == "e" || l == "g") { + if (n % 2 == 0) { + console.log( "Output: True"); + } else { + console.log( "Output: False"); + } + } else { + if (n % 2 == 1) { + console.log( "Output: True" ); + } else { + console.log( "Output: False" ); + } + } +} diff --git a/challenge-281/zapwai/javascript/ch-2.js b/challenge-281/zapwai/javascript/ch-2.js new file mode 100644 index 0000000000..840d2ce0b1 --- /dev/null +++ b/challenge-281/zapwai/javascript/ch-2.js @@ -0,0 +1,69 @@ +let start = "g2"; +let end = "a8"; +proc(start, end); + +start = "g2"; +end = "h2"; +proc(start, end); + +function proc(start, end) { + console.log("Input:", start,"to", end); + let list = [moves(start)]; + let round = -1; + let found = 0; + while (found == 0) { + round++; + for (let old_move of list[round]) { + if (old_move == end) { + found = 1; + } + } + + if (found == 1) { + break; + } else { + let L = []; + for (let old_move of list[round]) { + for (let move of moves(old_move)) { + L.push(move); + } + } + list.push(L); + } + } + round++; + console.log("Output:", round); +} + +function moves(coord) { + let l = coord.charAt(0); + let row = parseInt(coord.charAt(1), 10); + let cols = "abcdefgh"; + let col = 1 + cols.indexOf(l); + let r = []; + let c = []; + for (let i of [-2, 2]) { + for (let j of [-1, 1]) { + c.push(col + i); + r.push(row + j); + } + } + for (let i of [-1, 1]) { + for (let j of [-2, 2]) { + c.push(col + i); + r.push(row + j); + } + } + let list = []; + for (let i = 0; i <= 7; i++) { + if (c[i] < 1 || c[i] > 8) { + continue; + } + if (r[i] < 1 || r[i] > 8) { + continue; + } + let move = cols.charAt(c[i]-1)+r[i]; + list.push(move); + } + return list; +} diff --git a/challenge-281/zapwai/perl/ch-1.pl b/challenge-281/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..a0d6af0688 --- /dev/null +++ b/challenge-281/zapwai/perl/ch-1.pl @@ -0,0 +1,26 @@ +use v5.38; +my $coord = "d3"; +proc($coord); +$coord = "g5"; +proc($coord); +$coord = "e6"; +proc($coord); + +sub proc($coord) { + say "Input: $coord"; + my ($l, $n) = split "", $coord; + $l = lc $l; + if ($l eq "a" or $l eq "c" or $l eq "e" or $l eq "g") { + if ($n % 2 == 0) { + say "Output: True"; + } else { + say "Output: False"; + } + } else { + if ($n % 2 == 1) { + say "Output: True"; + } else { + say "Output: False"; + } + } +} diff --git a/challenge-281/zapwai/perl/ch-2.pl b/challenge-281/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..8c96832519 --- /dev/null +++ b/challenge-281/zapwai/perl/ch-2.pl @@ -0,0 +1,62 @@ +use v5.38; +my $start = "g2"; +my $end = "a8"; +proc($start, $end); + +$start = "g2"; +$end = "h2"; +proc($start, $end); + +sub proc($start, $end) { + say "Input: $start to $end"; + my @list = [moves($start)]; + my $round = -1; + my $found = 0; + while ($found == 0) { + $round++; + for my $old_move (@{$list[$round]}) { + if ($old_move eq $end) { + $found = 1; + } + } + if ($found == 1) { + last; + } else { + my @L; + for my $old_move (@{$list[$round]}) { + push @L, moves($old_move); + } + push @list, \@L; + } + } + $round++; + say "Output: $round"; +} + +sub moves($coord) { + my ($l, $row) = split "", $coord; + my $cols = "abcdefgh"; + my $col = 1 + index $cols, $l; + my @r; + my @c; + for my $i (-2, 2) { + for my $j (-1, 1) { + push @c, $col + $i; + push @r, $row + $j; + } + } + for my $i (-1, 1) { + for my $j (-2, 2) { + push @c, $col + $i; + push @r, $row + $j; + } + } + my @list; + for my $i (0 .. 7) { + next if ($c[$i] < 1 or $c[$i] > 8); + next if ($r[$i] < 1 or $r[$i] > 8); + my $move = substr($cols, $c[$i] - 1, 1).$r[$i]; + push @list, $move; + } + return @list; +} diff --git a/challenge-281/zapwai/python/ch-1.py b/challenge-281/zapwai/python/ch-1.py new file mode 100644 index 0000000000..7cac22ae23 --- /dev/null +++ b/challenge-281/zapwai/python/ch-1.py @@ -0,0 +1,23 @@ +def proc(coord): + print("Input:", coord) + a = list(coord) + l = a[0] + n = int(a[1]) + if l == "a" or l == "c" or l == "e" or l == "g": + if n % 2 == 0: + print("Output: True") + else: + print("Output: False") + else: + if n % 2 == 1: + print("Output: True") + else: + print("Output: False") + +coord = "d3" +proc(coord) +coord = "g5" +proc(coord) +coord = "e6" +proc(coord) + diff --git a/challenge-281/zapwai/python/ch-2.py b/challenge-281/zapwai/python/ch-2.py new file mode 100644 index 0000000000..ffc81971d4 --- /dev/null +++ b/challenge-281/zapwai/python/ch-2.py @@ -0,0 +1,53 @@ +def proc(start, end): + print("Input:", start, "to", end) + mylist = [moves(start)] + roundy = -1 + found = 0 + while found == 0: + roundy += 1 + for old_move in mylist[roundy]: + if old_move == end: + found = 1 + if found == 1: + break + else: + L = [] + for old_move in mylist[roundy]: + for new_move in moves(old_move): + L.append(new_move) + mylist.append(L) + roundy += 1 + print("Output:", roundy) + +def moves(coord): + l = coord[0] + row = int(coord[1]) + cols = "abcdefgh" + col = 1 + cols.index(l) + r = [] + c = [] + for i in [-2, 2]: + for j in [-1, 1]: + c.append(col + i) + r.append(row + j) + for i in [-1, 1]: + for j in [-2, 2]: + c.append(col + i) + r.append(row + j) + mylist = [] + for i in range(7): + if c[i] < 1 or c[i] > 8: + continue + if r[i] < 1 or r[i] > 8: + continue + move = str(cols[c[i] - 1]) + str(r[i]) + mylist.append(move) + return mylist + +start = "g2" +end = "a8" +proc(start, end) + +start = "g2" +end = "h2" +proc(start, end) diff --git a/challenge-281/zapwai/rust/ch-1.rs b/challenge-281/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..c850a01184 --- /dev/null +++ b/challenge-281/zapwai/rust/ch-1.rs @@ -0,0 +1,27 @@ +fn main() { + let coord = "d3"; + proc(coord); + let coord2 = "g5"; + proc(coord2); + let coord3 = "e6"; + proc(coord3); +} + +fn proc(coord : &str) { + println!("Input: {coord}"); + let l = coord.chars().nth(0).unwrap(); + let n = coord.chars().nth(1).unwrap() as i32; + if l == 'a' || l == 'c' || l == 'e' || l == 'g' { + if n % 2 == 0 { + println!("Output: True"); + } else { + println!("Output: False"); + } + } else { + if n % 2 == 1 { + println!("Output: True"); + } else { + println!("Output: False"); + } + } +} diff --git a/challenge-281/zapwai/rust/ch-2.rs b/challenge-281/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..9983e1c6ae --- /dev/null +++ b/challenge-281/zapwai/rust/ch-2.rs @@ -0,0 +1,77 @@ +fn main() { + let start = "g2"; + let end = "a8"; + proc(start, end); + + let start2 = "g2"; + let end2 = "h2"; + proc(start2, end2); +} + +fn proc(start :&str, end :&str) { + println!("Input: {start} to {end}"); + let mut list :Vec<Vec<String>> = vec![moves(&start.to_string())]; + let mut round = 0; + let mut found = 0; + while found == 0 { + round += 1; + for old_move in &list[round - 1] { + if old_move == end { + found = 1; + } + } + if found == 1 { + break; + } else { + let mut move_list :Vec<String> = Vec::new(); + for old_move in &list[round - 1] { + for new_move in moves(old_move) { + move_list.push(new_move); + } + } + let mut uniq_mv_list :Vec<String> = Vec::new(); + for elem in move_list { + let mut elem_flag = 0; + for q in &uniq_mv_list { + if *q == elem { + elem_flag = 1; + } + } + if elem_flag == 0 { + uniq_mv_list.push(elem); + } + } + list.push(uniq_mv_list); + } + } + println!("Output: {round}"); +} + +fn moves(coord :&String) -> Vec<String> { + let l = coord.chars().nth(0).unwrap(); + let row:i32 = coord.chars().nth(1).unwrap().to_digit(10).unwrap() as i32; + let cols = "abcdefgh"; + let col :i32 = 1 + cols.chars().position(|c| c == l).unwrap() as i32; + let mut r :Vec<i32> = Vec::new(); + let mut c :Vec<i32> = Vec::new(); + for i in [-2, 2] { + for j in [-1, 1] { + c.push(col + i); + r.push(row + j); + } + } + for i in [-1, 1] { + for j in [-2, 2] { + c.push(col + i); + r.push(row + j); + } + } + let mut list :Vec<String> = Vec::new(); + for i in 0 .. 8 { + if c[i] < 1 || c[i] > 8 { continue; } + if r[i] < 1 || r[i] > 8 { continue; } + let newmove = format!("{}{}", cols.chars().nth((c[i] as usize)-1).unwrap(), r[i]); + list.push(newmove); + } + return list; +} |
