-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday01.go
More file actions
70 lines (55 loc) · 1.53 KB
/
day01.go
File metadata and controls
70 lines (55 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"AdventOfCode-go/advent2024/utils"
"fmt"
"slices"
)
// countNumber used in part b to count the number of times an element appears in a list
func countNumber(listToSearch []int, intToFind int) int {
var count int
for _, item := range listToSearch {
if item == intToFind {
count++
}
}
return count
}
// calcDistance used in part a to calc the distance between slice elements
func calcDistance(firstList []int, secondList []int) int {
var result int
for i := 0; i < len(firstList); i ++ {
result += utils.Abs(firstList[i] - secondList[i])
}
return result
}
func day01(filename string, part byte, debug bool) int {
var result int
puzzleInput, _ := utils.ReadFile(filename)
inputLength := len(puzzleInput)
firstList := make([]int, inputLength)
secondList := make([]int, inputLength)
for i, puzzleLine := range puzzleInput {
fmt.Sscanf(puzzleLine, "%d %d\n", &firstList[i], &secondList[i])
}
if part == 'a' {
// Part 1: Find the distances between the 2 lists.
slices.Sort(firstList)
slices.Sort(secondList)
return calcDistance(firstList, secondList)
}
// Part B - find the similarity score between the two lists
for _, item := range firstList {
count := countNumber(secondList, item)
result += count * item
}
return result
}
// Main routine
func main() {
filenamePtr, execPart, debug := utils.CatchUserInput()
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
fmt.Printf("Result is: %d\n", day01(filenamePtr, execPart, debug))
}
}