Day01
This commit is contained in:
54
2024/gareth/day01/day01.go
Normal file
54
2024/gareth/day01/day01.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package day01
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Part1(input string) int {
|
||||
var leftList, rightList []float64
|
||||
total := 0.0
|
||||
lines := strings.Split(input, "\n")
|
||||
for _, line := range lines {
|
||||
sides := strings.Fields(line)
|
||||
leftNum, _ := strconv.ParseFloat(sides[0], 64)
|
||||
rightNum, _ := strconv.ParseFloat(sides[1], 64)
|
||||
leftList = append(leftList, leftNum)
|
||||
rightList = append(rightList, rightNum)
|
||||
}
|
||||
|
||||
sort.Float64s(leftList)
|
||||
sort.Float64s(rightList)
|
||||
|
||||
for i := 0; i < len(leftList); i++ {
|
||||
total += math.Abs(leftList[i] - rightList[i])
|
||||
}
|
||||
return int(total)
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
var leftList, rightList []int
|
||||
|
||||
lines := strings.Split(input, "\n")
|
||||
for _, line := range lines {
|
||||
sides := strings.Fields(line)
|
||||
leftNum, _ := strconv.Atoi(sides[0])
|
||||
rightNum, _ := strconv.Atoi(sides[1])
|
||||
leftList = append(leftList, leftNum)
|
||||
rightList = append(rightList, rightNum)
|
||||
}
|
||||
|
||||
counts := make(map[int]int)
|
||||
for _, num := range rightList {
|
||||
counts[num]++
|
||||
}
|
||||
|
||||
similarityScore := 0
|
||||
for _, num := range leftList {
|
||||
similarityScore += num * counts[num]
|
||||
}
|
||||
|
||||
return similarityScore
|
||||
}
|
||||
27
2024/gareth/day01/day01_test.go
Normal file
27
2024/gareth/day01/day01_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package day01
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
r := Part1(`3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3`)
|
||||
assert.Equal(t, 11, r)
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
r := Part2(`3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3`)
|
||||
assert.Equal(t, 31, r)
|
||||
}
|
||||
1000
2024/gareth/day01/input.txt
Normal file
1000
2024/gareth/day01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
11
2024/gareth/go.mod
Normal file
11
2024/gareth/go.mod
Normal file
@@ -0,0 +1,11 @@
|
||||
module aoc2024
|
||||
|
||||
go 1.23.2
|
||||
|
||||
require github.com/stretchr/testify v1.10.0
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
9
2024/gareth/go.sum
Normal file
9
2024/gareth/go.sum
Normal file
@@ -0,0 +1,9 @@
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
13
2024/gareth/main.go
Normal file
13
2024/gareth/main.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"aoc2024/day01"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
data, _ := os.ReadFile("day01/input.txt")
|
||||
fmt.Printf("part 1: %d\n", day01.Part1(string(data)))
|
||||
fmt.Printf("part 2: %d\n", day01.Part2(string(data)))
|
||||
}
|
||||
Reference in New Issue
Block a user