Skip to content

Commit 2e13a57

Browse files
committed
2024 day 1 pt 1
1 parent 8b3107f commit 2e13a57

File tree

3 files changed

+1042
-0
lines changed

3 files changed

+1042
-0
lines changed

2024/day1/day1.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"os"
7+
"slices"
8+
"strconv"
9+
"strings"
10+
)
11+
12+
func main() {
13+
filenames := []string{"testInput", "input"}
14+
for _, fileName := range filenames {
15+
fileContents, err := os.ReadFile(fileName)
16+
if err != nil {
17+
panic(err)
18+
}
19+
fmt.Println(fileName)
20+
lines := strings.Split(string(fileContents), "\n")
21+
list1 := make([]int, len(lines))
22+
list2 := make([]int, len(lines))
23+
for index, line := range lines {
24+
split := strings.Fields(line)
25+
list1[index], _ = strconv.Atoi(strings.Trim(split[0], " "))
26+
list2[index], _ = strconv.Atoi(strings.Trim(split[1], " "))
27+
}
28+
slices.Sort(list1)
29+
slices.Sort(list2)
30+
var difference int = 0
31+
for index, _ := range list1 {
32+
difference += int(math.Abs(float64(list1[index] - list2[index])))
33+
}
34+
println(difference)
35+
}
36+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy