Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Team google code jam solutions uploaded - Apprentice 2023a #423

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions solutions/esab-atad/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# ESAb ATAd

Last year, a research consortium had some trouble with a distributed database system that sometimes lost pieces of the data. You do not need to read or understand that problem in order to solve this one!

The consortium has decided that distributed systems are too complicated, so they are storing B bits of important information in a single array on one awesome machine. As an additional layer of security, they have made it difficult to obtain the information quickly; the user must query for a bit position between 1 and B, and then they receive that bit of the stored array as a response.

Unfortunately, this ultra-modern machine is subject to random quantum fluctuations! Specifically, after every 1st, 11th, 21st, 31st... etc. query is sent, but before the response is given, quantum fluctuation causes exactly one of the following four effects, with equal probability:

- 25% of the time, the array is complemented: every 0 becomes a 1, and vice versa.
- 25% of the time, the array is reversed: the first bit swaps with the last bit, the second bit swaps with the second-to-last bit, and so on.
- 25% of the time, both of the things above (complementation and reversal) happen to the array. (Notice that the order in which they happen does not matter.)
- 25% of the time, nothing happens to the array.

Moreover, there is no indication of what effect the quantum fluctuation has had each time. The consortium is now concerned, and it has hired you to get its precious data back, in whatever form it is in! Can you find the entire array, such that your answer is accurate as of the time that you give it? Answering does not count as a query, so if you answer after your 30th query, for example, the array will be the same as it was after your 21st through 30th queries.

More details: https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd27/0000000000209a9e
98 changes: 98 additions & 0 deletions solutions/esab-atad/go/esab-atad.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"bufio"
. "fmt" // Like C++ with the namespace, you use the functions without the 'fmt.' part
. "os"
)

var B, T int //where B is the size of the test, and T the number of Test Sets

var rd *bufio.Reader = bufio.NewReader(Stdin)
var wr *bufio.Writer = bufio.NewWriter(Stdout)

func main() {
Scanf("%d%d", &T, &B)
//Iterate trought all the cases
for i := 1; i <= T; i++ {
var DB = make([]int, B+1)
var L int = 1
var R int = B
for Q := 1; true; Q += 2 { //To keep an eye to the query number
if Q%10 == 1 && Q != 1 {
p := -1
asim := -1
for i := 1; i < L; i++ {
if DB[i] == DB[B+1-i] {
p = i
} else {
asim = i
}
}
if p == -1 {
status := query(1)
query(1)
if status != DB[1] {
for i := 1; i <= L; i++ {
DB[i] ^= 1
}
for i := R; i <= B; i++ {
DB[i] ^= 1
}
}
} else {
status := query(p)
if status != DB[p] {
for i := 1; i <= L; i++ {
DB[i] ^= 1
}
for i := R; i <= B; i++ {
DB[i] ^= 1
}
}
if asim == -1 {
query(p)
} else {
if query(asim) != DB[asim] {
for i, j := 1, len(DB)-1; i < j; i, j = i+1, j-1 {
DB[i], DB[j] = DB[j], DB[i]
}
}
}
}
Q += 2
}
DB[L] = query(L)
DB[R] = query(R)
L += 1
R -= 1
if L > R { //Means that we already have all the database
for i := 1; i <= B; i++ {
Printf("%d", DB[i])
}
Println()
var res string
Scanf("%s", &res)
if res == "N" { //Exit in case that our response is incorrect
Exit(0)
} else {
break
}

}
}
}
}

// To ask for a bit inside the database
func query(pos int) int {
Printf("%d\n", pos)
defer wr.Flush()
var bit int
Scanf("%d", &bit)
return bit
}

//Let me out of go jaj
//Note: Go is a good language, the thing was that
//I just used to much python.
112 changes: 112 additions & 0 deletions solutions/esab-atad/java/esab-atad.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import java.util.Scanner;

class Solution {

public static Scanner sc= new Scanner(System.in);
public static int count = 0;

public static void fillArray(int arr[]){
int size = arr.length;
for (int i = 0; i < size; i++){
arr[i] = 0;
}
}

public static void printArray(int arr[]){
int size = arr.length;
for (int i = 0; i < size; i++){
System.out.print(arr[i]);
}
System.out.print("\n");
}

public static void complement(int arr[]){
int size = arr.length;
for (int i = 0; i < size; i++){
arr[i] ^= 1;
}
}

public static void reverse(int arr[]){
int size = arr.length;
int tmp = 0;
for (int i = 0; i < Math.floor(size/2); i++){
tmp = arr[i];
arr[i] = arr[size-1-i];
arr[size-1-i] = tmp;
}
}

public static int query(int i){

if(i != -1){
System.out.print(i+1);
}
else{
System.out.print("1");
}
System.out.print("\n");
count++;
int input = sc.nextInt();
sc.nextLine();
return input;
}

public static void checkCorrect(int arr[]){
printArray(arr);
String input = sc.nextLine();

if (!input.equals("Y"))
System.exit(0);

}

public static void esab_atad(int B){
int result[] = new int[B];
int complement_i = -1;
int reverse_i = -1;
int complement_res = 0;
int reverse_res = 0;
count = 0;
fillArray(result);
for (int i = 0; i < Math.floor(B / 2); i++){
if(count != 0 && count%10 == 0){
complement_res = query(complement_i);
reverse_res = query(reverse_i);

if ( complement_i != -1 && (result[complement_i]^complement_res) == 1)
complement(result);

if ( reverse_i != -1 && (result[reverse_i]^reverse_res) == 1
)
reverse(result);
}

result[i] = query(i);

result[B-i-1] = query(B-i-1);

if( result[i] == result[B-i-1])
complement_i = i;
else
reverse_i = i;
}
checkCorrect(result);
}

public static void main(String[] args) {
int T;
int B;
String input;
String[] inputSplit;
input = sc.nextLine();
inputSplit = input.split(" ");

T = Integer.parseInt(inputSplit[0]);
B = Integer.parseInt(inputSplit[1]);

for (int i = 0; i < T; i++){
esab_atad(B);
}
}
}
145 changes: 145 additions & 0 deletions solutions/esab-atad/lisp/esab-atad.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
;; Function that gets a bit from the judge
(defun get-bit (idx)
(if (not idx)
(progn (format t "~d~%" 1) ;; In case idx is nil
(finish-output)
(parse-integer (read-line))
nil)
(progn (format t "~d~%" (1+ idx)) ;; idx is a valid number
(finish-output)
(parse-integer (read-line)))))

;; Function to query a pair of bits
(defun get-pair (left-idx)
(values (get-bit left-idx) (get-bit (- +number-length+ left-idx 1)))) ;; Return the left bit an its counterpart

; Function that returns true if left and right are symmetric (equal)
(defun symmetricp (left right)
(= left right))

;; Function that checks if the digit has changed after fluctuation
(defun digit-changed (old-value new-value)
(/= old-value new-value))

;; Function that returns the type of fluctuation when we have both pairs
(defun get-fluctuation-sym-asym (old-sym-value old-asym-value new-sym-value new-asym-value)
(cond
((and (/= old-sym-value new-sym-value)
(/= old-asym-value new-asym-value))
; Symmetrical AND asymmetrical changed
"C")
((/= old-sym-value new-sym-value) ; Only symmetrical changed
"CR")
((/= old-asym-value new-asym-value) ; Only asymmetrical changed
"R")
(t "N")) ; Neither changed
)

;; Function that returns the fluctuation type when we have only a symmetrical pair
(defun get-fluctuation-sym (old-sym-value new-sym-value)
(if (/= old-sym-value new-sym-value)
"C"
"N"))

;; Function that returns the fluctuation type when we have only an asymmetrical pair
(defun get-fluctuation-asym (old-asym-value new-asym-value)
(if (/= old-asym-value new-asym-value)
"C"
"N"))

;; Function that gets the fluctuation type, calls helper functions
(defun get-fluctuation-type (old-sym-value old-asym-value new-sym-value new-asym-value )
(cond ( (and old-sym-value old-asym-value) ; We have both pairs stored
(get-fluctuation-sym-asym old-sym-value old-asym-value new-sym-value new-asym-value)) ; Call helper function
; We only have one pair
( old-sym-value ; We only have sym
(get-fluctuation-sym old-sym-value new-sym-value) ; Call helper function
)
; We only have asym
(t (get-fluctuation-asym old-asym-value new-asym-value)) ; Call helper function
)
)

;; Function that changes 0s to 1s and viceversa .VERIFICAR CUANDO ELEMENTO DE ARR ES NIL
(defun complement-array (arr) ; bit index)
(loop for i from 0 to (- +number-length+ 1) do ; Loop through the array
(if (eql (aref arr i) 0) ; If the current bit is a 0
(setf (elt arr i) 1) ; Set the bit to a 1
(setf (elt arr i) 0) ; Set the bit to a 0
)))

;; Function that updates our stored array
(defun update-array (arr bit-index fluct-type)
(cond ( (equal fluct-type "C") ; If the fluctuation type is a C
(complement-array arr ) ; We complement the array
)
( (equal fluct-type "R") ; If the fluctuation type is an R
(setf arr (nreverse arr)) ; We reverse the array
)
( (equal fluct-type "CR") ; If the fluctuation type is a CR
(complement-array arr) ; We complement the array
(setf arr (nreverse arr)) ; We also reverse the array
)
(t nil) ; We leave our array the same
)
)

;; Helper function to convert an array to a list
(defun array-to-list (array)
(map 'list #'identity array))

;; Helper function that splits the input into ints
(defun split-space (s)
(let ( (space-idx (position #\Space s)))
;Return inputs as ints
(values (parse-integer (subseq s 0 space-idx)) (parse-integer (subseq s (1+ space-idx))) )))

;; Function that solves a test case
(defun solve-test-case (length)
;; Solve cases
(defconstant +number-length+ length) ; Define constant length of arrays
(defparameter arr (make-array +number-length+ :initial-element nil)) ; Create empty array of the length inputed
; Declare symmetrical and asymmetrical indexes, number of queries and current index
(let ((sym-index nil)
(asym-index nil)
(queries 0)
(bit-index 0))
(loop
(when (= bit-index (floor (/ +number-length+ 2))) (return)) ; Loop through the half of the array
(multiple-value-bind (left right) (get-pair bit-index) ; Declare left and right values
(setf queries (+ queries 2)) ; Add 2 to the queries
(if (or (not sym-index) (not asym-index)) ; If we dont have a sym or asym pair
(if (symmetricp left right) ; Check if its symmetrical
(if (not sym-index) (setf sym-index bit-index)) ; Set sym index, if we dont have one
(if (not asym-index) (setf asym-index bit-index)))) ; Set asym index, if we dont have one

(if (and (= (mod queries 10) 2) (/= bit-index 0)) ; Check if a fluctuation happened
(progn
(let ((new-sym-value (get-bit sym-index)) ; Get fluctuation and update accordingly
(new-asym-value (get-bit asym-index))
(old-sym-value (if (not sym-index) nil (elt arr sym-index)))
(old-asym-value (if (not asym-index) nil (elt arr asym-index))))
(update-array arr
bit-index
(get-fluctuation-type old-sym-value
old-asym-value
new-sym-value
new-asym-value)))
(setf queries (+ queries 2))) ; Add 2 to the queries
)
(setf (elt arr bit-index) left) ; Set new left value
(setf (elt arr (- +number-length+ bit-index 1)) right) ; Set new right value
(setf bit-index (1+ bit-index)) ; Add one to the current index
))
(format t "~{~d~}~%" (array-to-list arr)))) ; Print array

(defun main ()
(setf in (read-line)) ; Read input
(multiple-value-bind (test-cases length) (split-space in) ; Format input into test-cases and length
(loop for test from 0 to (- test-cases 1) do ; Loop test-cases times
(solve-test-case length)
(setf result (read-line))
(if (equal result "N") (return))) ; If we got a wrong answer, stop the program
))

(main)
Loading