updated Day01 to use original input provided by adventofcode

This commit is contained in:
2024-12-11 20:11:23 +01:00
parent e6fdf88f0d
commit a215df0b04
5 changed files with 1067 additions and 2050 deletions

File diff suppressed because it is too large Load Diff

1000
Day01/input.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,61 +1,77 @@
package main package main
import ( import (
"encoding/json" "bufio"
"fmt" "fmt"
"log" "log"
"os" "os"
"sort" "sort"
"strconv"
"strings"
) )
func optimized() { func optimized() {
fmt.Println("Optimized Code by ChatGPT") fmt.Println("Optimized Code by ChatGPT")
// Read the file in one step // Open the input file
content, err := os.ReadFile("./input.json") file, err := os.Open("./input.txt")
if err != nil { if err != nil {
log.Fatalf("Error when opening input.json: %v", err) log.Fatalf("Error opening input.txt: %v", err)
}
defer file.Close()
var (
listA, listB []int
)
// Read and parse input file
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.Fields(scanner.Text())
if len(line) != 2 {
log.Fatalf("Invalid input format: %s", scanner.Text())
}
num1, err1 := strconv.Atoi(line[0])
num2, err2 := strconv.Atoi(line[1])
if err1 != nil || err2 != nil {
log.Fatalf("Error converting input to integers: %v, %v", err1, err2)
}
listA = append(listA, num1)
listB = append(listB, num2)
} }
// Parse the JSON if err := scanner.Err(); err != nil {
var payload struct { log.Fatalf("Error reading file: %v", err)
ListA []int `json:"list_a"`
ListB []int `json:"list_b"`
}
if err := json.Unmarshal(content, &payload); err != nil {
log.Fatalf("Error when unmarshalling input.json: %v", err)
} }
// Part 1: Calculate the sum of absolute differences between sorted lists // Part 1: Calculate sum of absolute differences
sort.Ints(payload.ListA) sort.Ints(listA)
sort.Ints(payload.ListB) sort.Ints(listB)
sumPart1 := 0 sumPart1 := 0
for i := range payload.ListA { for i := 0; i < len(listA); i++ {
sumPart1 += abs(payload.ListA[i] - payload.ListB[i]) diff := listA[i] - listB[i]
if diff < 0 {
diff = -diff
}
sumPart1 += diff
} }
fmt.Printf("Sum of Part 1: %d\n", sumPart1) fmt.Printf("Sum of Part 1: %d\n", sumPart1)
// Part 2: Calculate weighted sum of common elements // Part 2: Calculate weighted sum of matches
sumPart2 := 0 sumPart2 := 0
countMap := make(map[int]int) countMap := make(map[int]int)
for _, v := range payload.ListB {
countMap[v]++ // Populate a map for quick lookup
for _, num := range listB {
countMap[num]++
} }
for _, v := range payload.ListA { for _, num := range listA {
if countMap[v] > 0 { if count, found := countMap[num]; found {
sumPart2 += v * countMap[v] sumPart2 += count * num
countMap[v] = 0 // Avoid double-counting
} }
} }
fmt.Printf("Sum of Part 2: %d\n", sumPart2) fmt.Printf("Sum of Part 2: %d\n", sumPart2)
} }
// Helper function for absolute value
func abs(x int) int {
if x < 0 {
return -x
}
return x
}

View File

@@ -1,31 +1,41 @@
package main package main
import ( import (
"encoding/json" "bufio"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os"
"sort" "sort"
"strconv"
"strings"
) )
func original() { func original() {
fmt.Println("Original Code") fmt.Println("Original Code")
content, err := ioutil.ReadFile("./input.json") file, err := os.Open("./input.txt")
if err != nil { if err != nil {
log.Fatal("Error when opening input.json: ", err) log.Fatal("Error when opening input.json: ", err)
} }
var payload map[string][]int fileScanner := bufio.NewScanner(file)
err = json.Unmarshal(content, &payload) fileScanner.Split(bufio.ScanLines)
if err != nil {
log.Fatal("Error when unmarshalling input.json: ", err) var (
listA, listB []int
)
for fileScanner.Scan() {
slice := strings.Fields(fileScanner.Text())
num1, _ := strconv.Atoi(slice[0])
num2, _ := strconv.Atoi(slice[1])
listA = append(listA, num1)
listB = append(listB, num2)
} }
// Part 1 file.Close()
listA := payload["list_a"]
listB := payload["list_b"]
// Part 1
sort.Ints(listA) sort.Ints(listA)
sort.Ints(listB) sort.Ints(listB)
@@ -41,9 +51,6 @@ func original() {
fmt.Println("Sum of Part 1: ", sumPart1) fmt.Println("Sum of Part 1: ", sumPart1)
// Part 2 // Part 2
listA = payload["list_a"]
listB = payload["list_b"]
sumPart2 := 0 sumPart2 := 0
for _, v1 := range listA { for _, v1 := range listA {