Google Code Jam 2022

Siarhei Siamashka siarhei.siamashka at gmail.com
Tue Apr 5 01:32:16 UTC 2022


A plot twist. I actually used Golang for submitting all my 
solutions in the qualification round:
https://codingcompetitions.withgoogle.com/codejam/submissions/0000000000876ff1/000000000048a518

The reason is that right now I'm switching to Golang at work. And 
Code Jam tasks are a good practice to become more comfortable 
with new programming languages, considering that the 
qualification round had no strict time limit. It's also a good 
chance to compare Golang vs. Dlang for this kind of activity.

Below are a few solutions for the second task of the 
qualification round "3D Printing": 
https://codingcompetitions.withgoogle.com/codejam/round/0000000000876ff1/0000000000a4672b

Golang:
```Go
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, 
a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, 
a...) }

// From 
https://stackoverflow.com/questions/27516387/what-is-the-correct-way-to-find-the-min-between-two-integers-in-go
func MinOf(vars ...int) int {
	min := vars[0]
	for _, i := range vars {
		if min > i {
			min = i
		}
	}
	return min
}

// From 
https://stackoverflow.com/questions/37532255/one-liner-to-transform-int-into-string
func SplitToString(a []int, sep string) string {
	if len(a) == 0 {
		return ""
	}
	b := make([]string, len(a))
	for i, v := range a {
		b[i] = strconv.Itoa(v)
	}
	return strings.Join(b, sep)
}

func main() {
	defer writer.Flush()
	var t int
	scanf("%d\n", &t)
	for casenum := 1; casenum <= t; casenum++ {
		var p1 = make([]int, 4)
		var p2 = make([]int, 4)
		var p3 = make([]int, 4)
		var ans = make([]int, 4)
		scanf("%d %d %d %d\n", &p1[0], &p1[1], &p1[2], &p1[3])
		scanf("%d %d %d %d\n", &p2[0], &p2[1], &p2[2], &p2[3])
		scanf("%d %d %d %d\n", &p3[0], &p3[1], &p3[2], &p3[3])
		todo := 1000000
		for i := 0; i < 4; i++ {
			ans[i] = MinOf(p1[i], p2[i], p3[i], todo)
			todo -= ans[i]
		}
		if todo > 0 {
			printf("Case #%d: IMPOSSIBLE\n", casenum)
		} else {
			printf("Case #%d: %s\n", casenum, SplitToString(ans, " "))
		}
	}
}
```

Ruby or Crystal:
```Ruby
1.upto(gets.to_s.to_i) do |casenum|
   todo = 1000000
   a = 3.times.map { gets.to_s.split.map {|x| x.to_i } 
}.to_a.transpose.map do |x|
     todo -= (result = [todo, x.min].min)
     result
   end
   printf("Case #%d: %s\n", casenum, todo > 0 ? "IMPOSSIBLE" : 
a.join(" "))
end
```

Dlang:
```D
import std.algorithm, std.stdio, std.string, std.conv, std.range;

void main()
{
   foreach (casenum ; 1 .. readln.strip.to!int + 1) {
     auto todo = 1000000;
     auto a = zip(readln.splitter.map!(to!int),
                  readln.splitter.map!(to!int),
                  
readln.splitter.map!(to!int)).map!"a[].min".array;
     foreach (ref x ; a)
       todo -= (x = min(todo, x));

     if (todo > 0)
       writefln("Case #%d: IMPOSSIBLE", casenum);
     else
       writefln("Case #%d: %(%s %)", casenum, a);
   }
}
```

Now which of these solutions is better? Getting a solution ready 
as fast as possible is usually very important in programming 
competitions, so expressive programming languages and smaller 
source code size is a clear advantage. That's why normally my 
primary choice would be Dlang (because Crystal is not supported 
by Code Jam, Codeforces and Codechef platforms at the moment).

But Golang is a simple language with no advanced features and 
syntax sugar, so it's easier for beginners to read and understand 
the code. From the practical point of view this is also useful 
and may be sometimes preferable in real projects. Python is 
easier than Ruby and is more popular. Golang is easier than Dlang 
and is more popular too.

I'm going to switch back to Dlang in the upcoming Round 1.


More information about the Digitalmars-d-learn mailing list