Start day 2

This commit is contained in:
bdnugget committed 2024-12-16 21:21:20 +01:00
1 parent 1fee125547
commit 148a2fcf18
3 files changed
+49 -5

No files matched your search

+25 -5
View File
@@ -25,9 +25,7 @@ func atoi(s string) int {
return v
}
func Part01() int {
lines := InputLines("./day01/input")
var leftCol, rightCol []int
func getSortedCols(lines []string) (leftCol []int, rightCol []int) {
for _, line := range lines {
left, right, found := strings.Cut(line, " ")
if found {
@@ -35,10 +33,16 @@ func Part01() int {
rightCol = append(rightCol, atoi(right))
}
}
slices.Sort(leftCol)
slices.Sort(rightCol)
return leftCol, rightCol
}
func Part01() int {
lines := InputLines("./day01/input")
leftCol, rightCol := getSortedCols(lines)
var diffCols []int
for i, val := range leftCol {
diffCols = append(diffCols, int(math.Abs(float64((val - rightCol[i])))))
@@ -49,6 +53,22 @@ func Part01() int {
sumDiff += diff
}
//fmt.Printf("%v", sumDiff)
return sumDiff
}
func Part02() int {
lines := InputLines("./day01/input")
leftCol, rightCol := getSortedCols(lines)
freqsRightCol := make(map[int]int)
for _, num := range rightCol {
freqsRightCol[num] += 1
}
var similarity int
for _, num := range leftCol {
similarity += freqsRightCol[num] * num
}
return similarity
}
+20
View File
@@ -0,0 +1,20 @@
package day02
import (
/*
"os"
"strings"
"strconv"
"fmt"
"slices"
"math"
*/
)
func Part01() int {
return 420
}
func Part02() int {
return 69
}
+4
View File
@@ -5,6 +5,7 @@ import (
"time"
"github.com/bdnugget/AoC_2024/day01"
"github.com/bdnugget/AoC_2024/day02"
)
func measureAndPrint[T any](label string, fn func() T) {
@@ -17,4 +18,7 @@ func measureAndPrint[T any](label string, fn func() T) {
func main() {
measureAndPrint("Day 01 part 01", day01.Part01)
measureAndPrint("Day 01 part 02", day01.Part02)
measureAndPrint("Day 02 part 01", day02.Part01)
measureAndPrint("Day 02 part 02", day02.Part02)
}