Compare commits
2 Commits
e6fdf88f0d
...
4d27eb650b
Author | SHA1 | Date | |
---|---|---|---|
4d27eb650b | |||
a215df0b04 |
2006
Day01/input.json
2006
Day01/input.json
File diff suppressed because it is too large
Load Diff
1000
Day01/input.txt
Normal file
1000
Day01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,61 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func optimized() {
|
||||
fmt.Println("Optimized Code by ChatGPT")
|
||||
|
||||
// Read the file in one step
|
||||
content, err := os.ReadFile("./input.json")
|
||||
// Open the input file
|
||||
file, err := os.Open("./input.txt")
|
||||
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
|
||||
var payload struct {
|
||||
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)
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Fatalf("Error reading file: %v", err)
|
||||
}
|
||||
|
||||
// Part 1: Calculate the sum of absolute differences between sorted lists
|
||||
sort.Ints(payload.ListA)
|
||||
sort.Ints(payload.ListB)
|
||||
// Part 1: Calculate sum of absolute differences
|
||||
sort.Ints(listA)
|
||||
sort.Ints(listB)
|
||||
|
||||
sumPart1 := 0
|
||||
for i := range payload.ListA {
|
||||
sumPart1 += abs(payload.ListA[i] - payload.ListB[i])
|
||||
for i := 0; i < len(listA); i++ {
|
||||
diff := listA[i] - listB[i]
|
||||
if diff < 0 {
|
||||
diff = -diff
|
||||
}
|
||||
sumPart1 += diff
|
||||
}
|
||||
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
|
||||
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 {
|
||||
if countMap[v] > 0 {
|
||||
sumPart2 += v * countMap[v]
|
||||
countMap[v] = 0 // Avoid double-counting
|
||||
for _, num := range listA {
|
||||
if count, found := countMap[num]; found {
|
||||
sumPart2 += count * num
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@@ -1,31 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func original() {
|
||||
fmt.Println("Original Code")
|
||||
|
||||
content, err := ioutil.ReadFile("./input.json")
|
||||
file, err := os.Open("./input.txt")
|
||||
if err != nil {
|
||||
log.Fatal("Error when opening input.json: ", err)
|
||||
}
|
||||
|
||||
var payload map[string][]int
|
||||
err = json.Unmarshal(content, &payload)
|
||||
if err != nil {
|
||||
log.Fatal("Error when unmarshalling input.json: ", err)
|
||||
fileScanner := bufio.NewScanner(file)
|
||||
fileScanner.Split(bufio.ScanLines)
|
||||
|
||||
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
|
||||
listA := payload["list_a"]
|
||||
listB := payload["list_b"]
|
||||
file.Close()
|
||||
|
||||
// Part 1
|
||||
sort.Ints(listA)
|
||||
sort.Ints(listB)
|
||||
|
||||
@@ -41,9 +51,6 @@ func original() {
|
||||
fmt.Println("Sum of Part 1: ", sumPart1)
|
||||
|
||||
// Part 2
|
||||
listA = payload["list_a"]
|
||||
listB = payload["list_b"]
|
||||
|
||||
sumPart2 := 0
|
||||
|
||||
for _, v1 := range listA {
|
||||
|
@@ -1,7 +1,30 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func original() {
|
||||
fmt.Println("Original Code")
|
||||
|
||||
// Open the input file
|
||||
file, err := os.Open("./input.txt")
|
||||
if err != nil {
|
||||
log.Fatalf("Error opening input.txt: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 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())
|
||||
}
|
||||
//
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user