Compare commits
4 Commits
3602fc356b
...
f8d8916b60
| Author | SHA1 | Date | |
|---|---|---|---|
| f8d8916b60 | |||
| 5545200823 | |||
| 0f33caf778 | |||
| 63f9aa5ad1 |
3
2021/day01/go.mod
Normal file
3
2021/day01/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module 2021/day01
|
||||
|
||||
go 1.25.4
|
||||
2000
2021/day01/input.txt
Normal file
2000
2021/day01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
51
2021/day01/main.go
Normal file
51
2021/day01/main.go
Normal 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
21
2021/day01/main_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user