Skip to content

Commit 37455a2

Browse files
author
Justin van Grootveld
committed
Add boilerplate and day01
1 parent d4a3206 commit 37455a2

File tree

11 files changed

+2183
-2
lines changed

11 files changed

+2183
-2
lines changed

2021/create-day.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#! /bin/sh
2+
3+
DAY="$*"
4+
5+
mkdir "${DAY}"
6+
touch "${DAY}/input.txt"
7+
touch "${DAY}/input_example.txt"
8+
echo "package ${DAY}
9+
10+
import (
11+
\"fmt\"
12+
)
13+
14+
func Part01(input []int) int {
15+
fmt.Println(\"HO HO HO!\")
16+
17+
return 0
18+
}
19+
20+
func Part02(input []int) int {
21+
fmt.Println(\"HO HO HO!\")
22+
23+
return 0
24+
}
25+
" > "${DAY}/part1.go"
26+
27+
28+
echo "package ${DAY}
29+
30+
import (
31+
\"fmt\"
32+
\"testing\"
33+
\"github.com/jvgrootveld/advent-of-code/avent2021/shared\"
34+
)
35+
36+
func TestPart01(t *testing.T) {
37+
content := shared.ReadFileAsInts(\"input_example.txt\")
38+
39+
result := Part01(content)
40+
fmt.Println(\"Result: \", result) // ???
41+
}
42+
43+
func TestPart02(t *testing.T) {
44+
content := shared.ReadFileAsInts(\"input_example.txt\")
45+
46+
result := Part02(content)
47+
fmt.Println(\"Result: \", result) // ???
48+
}
49+
" > "${DAY}/part1_test.go"

2021/day01/day01.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package day01
2+
3+
import (
4+
"github.com/jvgrootveld/advent-of-code/avent2021/shared"
5+
)
6+
7+
// Part01
8+
// How many measurements are larger than the previous measurement?
9+
func Part01(input []int) int {
10+
areLarger := 0
11+
12+
for i := 1; i < len(input); i++ {
13+
if input[i] > input[i-1] {
14+
areLarger++
15+
}
16+
}
17+
18+
return areLarger
19+
}
20+
21+
// Part02
22+
// Consider sums of a three-measurement sliding window. How many sums are larger than the previous sum?
23+
func Part02(input []int) int {
24+
var grouped []int
25+
26+
for i := 0; i < len(input) - 2; i++ {
27+
summed := shared.SumInts(input[i : i+3])
28+
grouped = append(grouped, summed)
29+
}
30+
31+
return Part01(grouped)
32+
}

2021/day01/day01_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package day01
2+
3+
import (
4+
"fmt"
5+
"github.com/jvgrootveld/advent-of-code/avent2021/shared"
6+
"testing"
7+
)
8+
9+
func TestPart01(t *testing.T) {
10+
content := shared.ReadFileAsInts("input.txt")
11+
12+
result := Part01(content)
13+
fmt.Println("Result: ", result) // 1390
14+
}
15+
16+
func TestPart02(t *testing.T) {
17+
content := shared.ReadFileAsInts("input.txt")
18+
19+
result := Part02(content)
20+
fmt.Println("Result: ", result) // 1457
21+
}

0 commit comments

Comments
 (0)