Skip to content

Commit f591f1e

Browse files
committed
Add Golang for 2021 🎅🚀
1 parent c4e9fb0 commit f591f1e

File tree

10 files changed

+219
-4
lines changed

10 files changed

+219
-4
lines changed

2020/Day_01/benchmark.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"PHP": "66.4229ms"
2+
"GO": "10ms",
3+
"PHP": "58.8491ms"
34
}

2020/Day_01/code.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"advent-of-code/helper"
5+
"fmt"
6+
"path"
7+
"runtime"
8+
"strconv"
9+
"time"
10+
)
11+
12+
func main() {
13+
_, f, _, _ := runtime.Caller(0)
14+
cwd := path.Join(path.Dir(f))
15+
16+
input := helper.ReadInput(cwd, helper.NewLine)
17+
18+
// Execute
19+
start := time.Now()
20+
result01 := part01(input)
21+
result02 := part02(input)
22+
executionTime := helper.ExecutionTime(time.Since(start))
23+
24+
fmt.Printf("Solution Part 1: %v\n", result01)
25+
fmt.Printf("Solution Part 2: %v\n", result02)
26+
fmt.Printf("Execution time: %s\n", executionTime)
27+
28+
helper.SaveBenchmarkTime(executionTime, cwd)
29+
30+
// Testing
31+
helper.TestResults(
32+
[]helper.TestingValue{
33+
helper.TestingValue{Result: result01, Expect: 935419},
34+
helper.TestingValue{Result: result02, Expect: 49880012},
35+
},
36+
)
37+
}
38+
39+
// Task code
40+
func part01(input []string) int {
41+
for _, firstValue := range input {
42+
for _, secondValue := range input {
43+
fv, _ := strconv.Atoi(firstValue)
44+
sv, _ := strconv.Atoi(secondValue)
45+
46+
sum := fv + sv
47+
48+
if 2020 == sum {
49+
return fv * sv
50+
}
51+
}
52+
}
53+
54+
return 0
55+
}
56+
57+
func part02(input []string) int {
58+
for _, firstValue := range input {
59+
for _, secondValue := range input {
60+
for _, thirdValue := range input {
61+
fv, _ := strconv.Atoi(firstValue)
62+
sv, _ := strconv.Atoi(secondValue)
63+
tv, _ := strconv.Atoi(thirdValue)
64+
65+
sum := fv + sv + tv
66+
67+
if 2020 == sum {
68+
return fv * sv * tv
69+
}
70+
}
71+
}
72+
}
73+
74+
return 0
75+
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Advent of Code
22

33
[2015](/2015/README.md) | [2016](/2016/README.md) | [2017](/2017/README.md) |
4-
[2018](/2018/README.md) | [2019](/2020/README.md) | [2020](/2020/README.md)
4+
[2018](/2018/README.md) | [2019](/2020/README.md) | [2020](/2020/README.md) | [2021](/2021/README.md)
55

66
This repository contains my solutions for [Advent of Code](https://adventofcode.com/).
77

@@ -36,5 +36,6 @@ You only need to add the binary for executing your script into `binary-ext.map.p
3636
Already added languages:
3737
- PHP
3838
- Javascript/Node (in `template/.ignore/`)
39+
- Golang (in `template/.ignore/`)
3940

4041
To enable ignored templates for `bin/task`, you need to moved them manually to `template/`.

binary-ext.map.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33
return [
44
'php' => ['.php'],
55
'node' => ['.js', '.mjs'],
6+
'go' => [
7+
'extensions' => ['.go'],
8+
'options' => ['run',]
9+
],
610
];

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module advent-of-code
2+
3+
go 1.17
4+
5+
require (
6+
advent-of-code/helper v0.0.0
7+
)
8+
9+
replace (
10+
advent-of-code/helper => ./lib/go/helper
11+
)

lib/go/helper/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module advent-of-code/helper
2+
3+
go 1.17

lib/go/helper/helper.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package helper
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"strings"
8+
"time"
9+
)
10+
11+
const (
12+
NewLine = "\n"
13+
)
14+
15+
func ReadInput(currentDir string, delimiter string) []string {
16+
buffer, err := ioutil.ReadFile(fmt.Sprintf("%s/input.txt", currentDir))
17+
if err != nil {
18+
panic(err)
19+
}
20+
21+
return strings.Split(string(buffer), delimiter)
22+
}
23+
24+
func ExecutionTime(duration time.Duration) string {
25+
return fmt.Sprintf("%dms", duration.Milliseconds())
26+
}
27+
28+
func SaveBenchmarkTime(execTime string, currentDir string) {
29+
fileName := fmt.Sprintf("%s/benchmark.json", currentDir)
30+
result := make(map[string]interface{})
31+
32+
data, _ := ioutil.ReadFile(fileName)
33+
34+
json.Unmarshal(data, &result)
35+
36+
result["GO"] = execTime
37+
38+
bufferJson, err := json.Marshal(result)
39+
if err != nil {
40+
panic(err)
41+
}
42+
43+
ioutil.WriteFile(fileName, bufferJson, 0644)
44+
}
45+
46+
type TestingValue struct {
47+
Result interface{}
48+
Expect interface{}
49+
}
50+
51+
func (v *TestingValue) isEqual() bool {
52+
return v.Result == v.Expect
53+
}
54+
55+
func TestResults(values []TestingValue) {
56+
if len(values) == 0 {
57+
fmt.Println("Skipped tests!")
58+
}
59+
60+
for i, tv := range values {
61+
pos := i + 1
62+
63+
if tv.isEqual() {
64+
fmt.Printf("\033[32mPart %d: passed\033[37m\n", pos)
65+
continue
66+
}
67+
68+
fmt.Printf(
69+
"\033[31mPart %d: failed with Expected: '%s' but get '%s'. \033[37m\n",
70+
pos,
71+
tv.Expect,
72+
tv.Result,
73+
)
74+
}
75+
}

lib/js/utils.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const readInput = (fileUrl, delimiter = '\n') => {
1717

1818
const testResults = (expects, results) => {
1919
if (expects.length === 0) {
20-
console.log('Skipped test!');
20+
console.log('Skipped tests!');
2121

2222
return;
2323
}

lib/php/utils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function readInput(string $currentDir, string $delimiter = PHP_EOL): array
132132
function testResults(array $expects, array $results): void
133133
{
134134
if (empty($expects)) {
135-
writeln('Skipped test!');
135+
writeln('Skipped tests!');
136136

137137
return;
138138
}

template/.ignore/template.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"advent-of-code/helper"
5+
"fmt"
6+
"path"
7+
"runtime"
8+
"time"
9+
)
10+
11+
func main() {
12+
_, f, _, _ := runtime.Caller(0)
13+
cwd := path.Join(path.Dir(f))
14+
15+
input := helper.ReadInput(cwd, helper.NewLine)
16+
17+
// Execute
18+
start := time.Now()
19+
result01 := part01(input)
20+
result02 := part02(input)
21+
executionTime := helper.ExecutionTime(time.Since(start))
22+
23+
fmt.Printf("Solution Part 1: %v\n", result01)
24+
fmt.Printf("Solution Part 2: %v\n", result02)
25+
fmt.Printf("Execution time: %s\n", executionTime)
26+
27+
helper.SaveBenchmarkTime(executionTime, cwd)
28+
29+
// Testing
30+
//helper.TestResults(
31+
// []helper.TestingValue{
32+
// helper.TestingValue{Result: result01, Expect: nil},
33+
// helper.TestingValue{Result: result02, Expect: nil},
34+
// },
35+
//)
36+
}
37+
38+
// Task code
39+
func part01(input []string) {
40+
41+
}
42+
43+
func part02(input []string) {
44+
45+
}

0 commit comments

Comments
 (0)