Compare commits

...

4 Commits

Author SHA1 Message Date
f8d8916b60 feat: add main code 2025-11-24 21:24:48 +01:00
5545200823 test: add unit tests 2025-11-24 21:24:43 +01:00
0f33caf778 feat: add input 2025-11-24 21:24:22 +01:00
63f9aa5ad1 go:init 2021/day01 package 2025-11-24 21:24:18 +01:00
4 changed files with 2075 additions and 0 deletions

3
2021/day01/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module 2021/day01
go 1.25.4

2000
2021/day01/input.txt Normal file

File diff suppressed because it is too large Load Diff

51
2021/day01/main.go Normal file
View File

@@ -0,0 +1,51 @@
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
)
func parseInput(file string) []int {
content, err := os.ReadFile(file)
if err != nil {
log.Fatalf("Failed to read input file: %v", err)
}
var data []int
for line := range strings.SplitSeq(string(content), "\n") {
num, err := strconv.Atoi(line)
if err != nil {
log.Fatalf("Failed to convert string to int: %v", err)
}
data = append(data, num)
}
return data
}
func PartOne(data []int) int {
count := 0
for i := 1; i < len(data); i++ {
if data[i] > data[i-1] {
count++
}
}
return count
}
func PartTwo(data []int) int {
count := 0
for i := 3; i < len(data); i++ {
if data[i] > data[i-3] {
count++
}
}
return count
}
func main() {
data := parseInput("input.txt")
fmt.Println("Part 1:", PartOne(data))
fmt.Println("Part 2:", PartTwo(data))
}

21
2021/day01/main_test.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import "testing"
var testInput = []int{199, 200, 208, 210, 200, 207, 240, 269, 260, 263}
func TestPartOne(t *testing.T) {
expected := 7
got := PartOne(testInput)
if got != expected {
t.Errorf("PartOne() = %d, want %d", got, expected)
}
}
func TestPartTwo(t *testing.T) {
expected := 5
got := PartTwo(testInput)
if got != expected {
t.Errorf("PartTwo() = %d, want %d", got, expected)
}
}