2024 Day 1 Complete!
This commit is contained in:
parent
6578d5123f
commit
46871f3dbe
1000
2024/day01/input
Normal file
1000
2024/day01/input
Normal file
File diff suppressed because it is too large
Load Diff
77
2024/day01/main.go
Normal file
77
2024/day01/main.go
Normal file
@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
inp := helpers.StdinToStringSlice()
|
||||
part1(inp)
|
||||
part2(inp)
|
||||
}
|
||||
|
||||
func buildLists(inp []string) ([]int, []int) {
|
||||
var list1, list2 []int
|
||||
for i := range inp {
|
||||
var pt string
|
||||
for j := range inp[i] {
|
||||
if inp[i][j] != ' ' {
|
||||
pt = pt + string(inp[i][j])
|
||||
} else if strings.TrimSpace(pt) != "" {
|
||||
wrk, err := strconv.Atoi(pt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
list1 = append(list1, wrk)
|
||||
pt = ""
|
||||
}
|
||||
}
|
||||
// End of line
|
||||
wrk, err := strconv.Atoi(pt)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
list2 = append(list2, wrk)
|
||||
pt = ""
|
||||
}
|
||||
return list1, list2
|
||||
}
|
||||
|
||||
func part1(inp []string) {
|
||||
var diffs []int
|
||||
list1, list2 := buildLists(inp)
|
||||
slices.Sort(list1)
|
||||
slices.Sort(list2)
|
||||
for i := 0; i < len(list1); i++ {
|
||||
diffs = append(diffs, (list1[i] - list2[i]))
|
||||
}
|
||||
var total int
|
||||
for i := range diffs {
|
||||
if diffs[i] < 0 {
|
||||
total = total - diffs[i]
|
||||
} else {
|
||||
total = total + diffs[i]
|
||||
}
|
||||
}
|
||||
fmt.Println("# Part 1")
|
||||
fmt.Println("Total Distance:", total)
|
||||
}
|
||||
|
||||
func part2(inp []string) {
|
||||
var score int
|
||||
occ := make(map[int]int)
|
||||
list1, list2 := buildLists(inp)
|
||||
for i := range list2 {
|
||||
occ[list2[i]]++
|
||||
}
|
||||
for i := range list1 {
|
||||
score = score + (list1[i] * occ[list1[i]])
|
||||
}
|
||||
fmt.Println("# Part 2")
|
||||
fmt.Println("Similarity Score:", score)
|
||||
}
|
6
2024/day01/testinput
Normal file
6
2024/day01/testinput
Normal file
@ -0,0 +1,6 @@
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
Loading…
Reference in New Issue
Block a user