Skip to main content
improved description
Source Link

C++ (g++), 60 54 4747 40 bytes

1-indexed. f(2)==29.

int f(int n){return(n/8|n/4|n)%2+(n^n/8)%2+29;%2-(n==2)+30;}

Solution from k-map, which simplified to A=w+x+z and B=zw'+z'w. A was negated and became a subtraction.

int f(int n){          //N is a 4-digit binary number wxyz.
return (n/8|nn^n/4|n8) %2  //Add 1 if (w OR x ORXOR z)
     +- (n^n/8n==2) %2      //AddSubtract 1 if !(w XOROR x OR z), which simplifies to (w'x'z'),
     + 29;             //Addwhich 29is only satisfied when N is 0000 or 0010.
                   //Since n is never 0, thethis lengthcondition ofis only satisfied when n==2.
     + 30;         //Add 30, the shortestbase month length.
}

At first I tried adding 1 or 2 instead of 1 or 1, but my solution was 87 bytes (if I remember correctly). I tried that with 0-indexing and 1-indexing, but they ended up being the exact same length.

Edit47 bytes: Thanks to Herman L. for the improvement from int f(int n){return(n/8|n/4|n)%2+(n^n/8)%2+29;}

54 bytes: int f(int n){return((n>>3|n>>2|n)&1)+((n^n>>3)&1)+29;}

60 bytes: int f(int n){return(n>>3&1|n>>2&1|n&1)+((n&1)^(n>>3&1))+29;}

Edit: Thanks to Herman L. for the improvement to 54 bytes, I'm glad someone found my approach interesting even though it's clearly not the shortest!

Edit 2: Thank you to ceilingcat for the suggestionimprovement to use (n/8|n/4|n)%2+(n^n/8)%2 instead of ((n>>3|n>>2|n)&1)+((n^n>>3)&1)47 bytes. This uses division instead of bit shifting, which saves one byte in each of three places. It also replaces &1 with %2 in two places. Both of these functionally return the last byte of the integer, but the modulo operator has precedence over addition. This means that two pairs of parentheses can be removed, saving four bytes. In total, by mixing arithmetic and bitwise operators, ceilingcat managed to improve the answer by seven bytes!

Edit 3: Another improvement by ceilingcat brings it down six bytes by doing bitwise comparison with 13, -!(n&13). Since this is only true in one case, simplified to an equality check.

C++ (g++), 60 54 47 bytes

1-indexed. f(2)==29.

int f(int n){return(n/8|n/4|n)%2+(n^n/8)%2+29;}

Solution from k-map, which simplified to A=w+x+z and B=zw'+z'w.

int f(int n){          //N is a 4-digit binary number wxyz.
return (n/8|n/4|n) %2  //Add 1 if (w OR x OR z)
     + (n^n/8) %2      //Add 1 if (w XOR z)
     + 29;             //Add 29, the length of the shortest month.
}

At first I tried adding 1 or 2 instead of 1 or 1, but my solution was 87 bytes (if I remember correctly). I tried that with 0-indexing and 1-indexing, but they ended up being the exact same length.

Edit: Thanks to Herman L. for the improvement from f(int n){return(n>>3&1|n>>2&1|n&1)+((n&1)^(n>>3&1))+29;}, I'm glad someone found my approach interesting even though it's clearly not the shortest!

Edit 2: Thank you to ceilingcat for the suggestion to use (n/8|n/4|n)%2+(n^n/8)%2 instead of ((n>>3|n>>2|n)&1)+((n^n>>3)&1). This uses division instead of bit shifting, which saves one byte in each of three places. It also replaces &1 with %2 in two places. Both of these functionally return the last byte of the integer, but the modulo operator has precedence over addition. This means that two pairs of parentheses can be removed, saving four bytes. In total, by mixing arithmetic and bitwise operators, ceilingcat managed to improve the answer by seven bytes!

C++ (g++), 60 54 47 40 bytes

1-indexed. f(2)==29.

int f(int n){return(n^n/8)%2-(n==2)+30;}

Solution from k-map, which simplified to A=w+x+z and B=zw'+z'w. A was negated and became a subtraction.

int f(int n){      //N is a 4-digit binary number wxyz.
return (n^n/8) %2  //Add 1 if (w XOR z)
     - (n==2)      //Subtract 1 if !(w OR x OR z), which simplifies to (w'x'z'),
                   //which is only satisfied when N is 0000 or 0010.
                   //Since n is never 0, this condition is only satisfied when n==2.
     + 30;         //Add 30, the base month length.
}

At first I tried adding 1 or 2 instead of 1 or 1, but my solution was 87 bytes (if I remember correctly). I tried that with 0-indexing and 1-indexing, but they ended up being the exact same length.

47 bytes: int f(int n){return(n/8|n/4|n)%2+(n^n/8)%2+29;}

54 bytes: int f(int n){return((n>>3|n>>2|n)&1)+((n^n>>3)&1)+29;}

60 bytes: int f(int n){return(n>>3&1|n>>2&1|n&1)+((n&1)^(n>>3&1))+29;}

Edit: Thanks to Herman L. for the improvement to 54 bytes, I'm glad someone found my approach interesting even though it's clearly not the shortest!

Edit 2: Thank you to ceilingcat for the improvement to 47 bytes. This uses division instead of bit shifting, which saves one byte in each of three places. It also replaces &1 with %2 in two places. Both of these functionally return the last byte of the integer, but the modulo operator has precedence over addition. This means that two pairs of parentheses can be removed, saving four bytes. In total, by mixing arithmetic and bitwise operators, ceilingcat managed to improve the answer by seven bytes!

Edit 3: Another improvement by ceilingcat brings it down six bytes by doing bitwise comparison with 13, -!(n&13). Since this is only true in one case, simplified to an equality check.

added 565 characters in body
Source Link

C++ (g++), 60 5454 47 bytes

1-indexed. f(2)==29.

int f(int n){return((n>>3|n>>2|n)&1n/8|n/4|n)+(%2+(n^n>>3)&1n^n/8)+29;%2+29;}

Solution from k-map, which simplified to A=w+x+z and B=zw'+z'w.

int f(int n){                 //N is a 4-digit binary number wxyz.
return((n>>3 | n>>2 | (n/8|n/4|n) &%2 1) //Add 1 if (w OR x OR z)
     +((n ^ n>>3) & 1(n^n/8)  %2      //Add 1 if (w XOR z)
     +29;}      + 29;             //Add 29, the length of the shortest month.
}

At first I tried adding 1 or 2 instead of 1 or 1, but my solution was 87 bytes (if I remember correctly). I tried that with 0-indexing and 1-indexing, but they ended up being the exact same length.

Edit: Thanks to Herman L. for the improvement from f(int n){return(n>>3&1|n>>2&1|n&1)+((n&1)^(n>>3&1))+29;}, I'm glad someone found my approach interesting even though it's clearly not the shortest!

Edit 2: Thank you to ceilingcat for the suggestion to use (n/8|n/4|n)%2+(n^n/8)%2 instead of ((n>>3|n>>2|n)&1)+((n^n>>3)&1). This uses division instead of bit shifting, which saves one byte in each of three places. It also replaces &1 with %2 in two places. Both of these functionally return the last byte of the integer, but the modulo operator has precedence over addition. This means that two pairs of parentheses can be removed, saving four bytes. In total, by mixing arithmetic and bitwise operators, ceilingcat managed to improve the answer by seven bytes!

C++ (g++), 60 54 bytes

1-indexed. f(2)==29.

int f(int n){return((n>>3|n>>2|n)&1)+((n^n>>3)&1)+29;}

Solution from k-map, which simplified to A=w+x+z and B=zw'+z'w.

int f(int n){                 //N is a 4-digit binary number wxyz.
return((n>>3 | n>>2 | n) & 1) //Add 1 if (w OR x OR z)
     +((n ^ n>>3) & 1)        //Add 1 if (w XOR z)
     +29;}                    //Add 29, the length of the shortest month.

At first I tried adding 1 or 2 instead of 1 or 1, but my solution was 87 bytes (if I remember correctly). I tried that with 0-indexing and 1-indexing, but they ended up being the exact same length.

Thanks to Herman L. for the improvement from f(int n){return(n>>3&1|n>>2&1|n&1)+((n&1)^(n>>3&1))+29;}, I'm glad someone found my approach interesting even though it's clearly not the shortest!

C++ (g++), 60 54 47 bytes

1-indexed. f(2)==29.

int f(int n){return(n/8|n/4|n)%2+(n^n/8)%2+29;}

Solution from k-map, which simplified to A=w+x+z and B=zw'+z'w.

int f(int n){          //N is a 4-digit binary number wxyz.
return (n/8|n/4|n) %2  //Add 1 if (w OR x OR z)
     + (n^n/8) %2      //Add 1 if (w XOR z)
     + 29;             //Add 29, the length of the shortest month.
}

At first I tried adding 1 or 2 instead of 1 or 1, but my solution was 87 bytes (if I remember correctly). I tried that with 0-indexing and 1-indexing, but they ended up being the exact same length.

Edit: Thanks to Herman L. for the improvement from f(int n){return(n>>3&1|n>>2&1|n&1)+((n&1)^(n>>3&1))+29;}, I'm glad someone found my approach interesting even though it's clearly not the shortest!

Edit 2: Thank you to ceilingcat for the suggestion to use (n/8|n/4|n)%2+(n^n/8)%2 instead of ((n>>3|n>>2|n)&1)+((n^n>>3)&1). This uses division instead of bit shifting, which saves one byte in each of three places. It also replaces &1 with %2 in two places. Both of these functionally return the last byte of the integer, but the modulo operator has precedence over addition. This means that two pairs of parentheses can be removed, saving four bytes. In total, by mixing arithmetic and bitwise operators, ceilingcat managed to improve the answer by seven bytes!

Improved answer
Source Link

C++ (g++), 6060 54 bytes

1-indexed. f(2)==29.

int f(int n){return(n>>3&1|n>>2&1|n&1(n>>3|n>>2|n)&1)+((n&1)^(n>>3&1n^n>>3)&1)+29;}

Solution from k-map, which simplified to A=w+x+z and B=zw'+z'w.

int f(int n){                 //N is a 4-digit binary number wxyz.
return(n>>3&1(n>>3 | n>>2&1n>>2 | n&1n) & 1) //Add 1 if (w OR x OR z)
     +((n&1)n ^ (n>>3&1n>>3) & 1)        //Add 1 if (w XOR z)
     +29;}                    //Add 29, the length of the shortest month.

At first I tried adding 1 or 2 instead of 1 or 1, but my solution was 87 bytes (if I remember correctly). I tried that with 0-indexing and 1-indexing, but they ended up being the exact same length.

Thanks to Herman L. for the improvement from f(int n){return(n>>3&1|n>>2&1|n&1)+((n&1)^(n>>3&1))+29;}, I'm glad someone found my approach interesting even though it's clearly not the shortest!

C++ (g++), 60 bytes

1-indexed. f(2)==29.

int f(int n){return(n>>3&1|n>>2&1|n&1)+((n&1)^(n>>3&1))+29;}

Solution from k-map, which simplified to A=w+x+z and B=zw'+z'w.

int f(int n){                 //N is a 4-digit binary number wxyz.
return(n>>3&1 | n>>2&1 | n&1) //Add 1 if (w OR x OR z)
     +((n&1) ^ (n>>3&1))      //Add 1 if (w XOR z)
     +29;}                    //Add 29, the length of the shortest month.

At first I tried adding 1 or 2 instead of 1 or 1, but my solution was 87 bytes (if I remember correctly). I tried that with 0-indexing and 1-indexing, but they ended up being the exact same length.

C++ (g++), 60 54 bytes

1-indexed. f(2)==29.

int f(int n){return((n>>3|n>>2|n)&1)+((n^n>>3)&1)+29;}

Solution from k-map, which simplified to A=w+x+z and B=zw'+z'w.

int f(int n){                 //N is a 4-digit binary number wxyz.
return((n>>3 | n>>2 | n) & 1) //Add 1 if (w OR x OR z)
     +((n ^ n>>3) & 1)        //Add 1 if (w XOR z)
     +29;}                    //Add 29, the length of the shortest month.

At first I tried adding 1 or 2 instead of 1 or 1, but my solution was 87 bytes (if I remember correctly). I tried that with 0-indexing and 1-indexing, but they ended up being the exact same length.

Thanks to Herman L. for the improvement from f(int n){return(n>>3&1|n>>2&1|n&1)+((n&1)^(n>>3&1))+29;}, I'm glad someone found my approach interesting even though it's clearly not the shortest!

added 16 characters in body
Source Link
Loading
Source Link
Loading