Day01
This commit is contained in:
170
Day01/.gitignore
vendored
Normal file
170
Day01/.gitignore
vendored
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
# Created by https://www.toptal.com/developers/gitignore/api/go,goland+all,jetbrains+all
|
||||||
|
# Edit at https://www.toptal.com/developers/gitignore?templates=go,goland+all,jetbrains+all
|
||||||
|
|
||||||
|
### Go ###
|
||||||
|
# If you prefer the allow list template instead of the deny list, see community template:
|
||||||
|
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||||
|
#
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
|
|
||||||
|
# Dependency directories (remove the comment below to include it)
|
||||||
|
# vendor/
|
||||||
|
|
||||||
|
# Go workspace file
|
||||||
|
go.work
|
||||||
|
|
||||||
|
### GoLand+all ###
|
||||||
|
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||||
|
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||||
|
|
||||||
|
# User-specific stuff
|
||||||
|
.idea/**/workspace.xml
|
||||||
|
.idea/**/tasks.xml
|
||||||
|
.idea/**/usage.statistics.xml
|
||||||
|
.idea/**/dictionaries
|
||||||
|
.idea/**/shelf
|
||||||
|
|
||||||
|
# AWS User-specific
|
||||||
|
.idea/**/aws.xml
|
||||||
|
|
||||||
|
# Generated files
|
||||||
|
.idea/**/contentModel.xml
|
||||||
|
|
||||||
|
# Sensitive or high-churn files
|
||||||
|
.idea/**/dataSources/
|
||||||
|
.idea/**/dataSources.ids
|
||||||
|
.idea/**/dataSources.local.xml
|
||||||
|
.idea/**/sqlDataSources.xml
|
||||||
|
.idea/**/dynamic.xml
|
||||||
|
.idea/**/uiDesigner.xml
|
||||||
|
.idea/**/dbnavigator.xml
|
||||||
|
|
||||||
|
# Gradle
|
||||||
|
.idea/**/gradle.xml
|
||||||
|
.idea/**/libraries
|
||||||
|
|
||||||
|
# Gradle and Maven with auto-import
|
||||||
|
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||||
|
# since they will be recreated, and may cause churn. Uncomment if using
|
||||||
|
# auto-import.
|
||||||
|
# .idea/artifacts
|
||||||
|
# .idea/compiler.xml
|
||||||
|
# .idea/jarRepositories.xml
|
||||||
|
# .idea/modules.xml
|
||||||
|
# .idea/*.iml
|
||||||
|
# .idea/modules
|
||||||
|
# *.iml
|
||||||
|
# *.ipr
|
||||||
|
|
||||||
|
# CMake
|
||||||
|
cmake-build-*/
|
||||||
|
|
||||||
|
# Mongo Explorer plugin
|
||||||
|
.idea/**/mongoSettings.xml
|
||||||
|
|
||||||
|
# File-based project format
|
||||||
|
*.iws
|
||||||
|
|
||||||
|
# IntelliJ
|
||||||
|
out/
|
||||||
|
|
||||||
|
# mpeltonen/sbt-idea plugin
|
||||||
|
.idea_modules/
|
||||||
|
|
||||||
|
# JIRA plugin
|
||||||
|
atlassian-ide-plugin.xml
|
||||||
|
|
||||||
|
# Cursive Clojure plugin
|
||||||
|
.idea/replstate.xml
|
||||||
|
|
||||||
|
# SonarLint plugin
|
||||||
|
.idea/sonarlint/
|
||||||
|
|
||||||
|
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||||
|
com_crashlytics_export_strings.xml
|
||||||
|
crashlytics.properties
|
||||||
|
crashlytics-build.properties
|
||||||
|
fabric.properties
|
||||||
|
|
||||||
|
# Editor-based Rest Client
|
||||||
|
.idea/httpRequests
|
||||||
|
|
||||||
|
# Android studio 3.1+ serialized cache file
|
||||||
|
.idea/caches/build_file_checksums.ser
|
||||||
|
|
||||||
|
### GoLand+all Patch ###
|
||||||
|
# Ignore everything but code style settings and run configurations
|
||||||
|
# that are supposed to be shared within teams.
|
||||||
|
|
||||||
|
.idea/*
|
||||||
|
|
||||||
|
!.idea/codeStyles
|
||||||
|
!.idea/runConfigurations
|
||||||
|
|
||||||
|
### JetBrains+all ###
|
||||||
|
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||||
|
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||||
|
|
||||||
|
# User-specific stuff
|
||||||
|
|
||||||
|
# AWS User-specific
|
||||||
|
|
||||||
|
# Generated files
|
||||||
|
|
||||||
|
# Sensitive or high-churn files
|
||||||
|
|
||||||
|
# Gradle
|
||||||
|
|
||||||
|
# Gradle and Maven with auto-import
|
||||||
|
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||||
|
# since they will be recreated, and may cause churn. Uncomment if using
|
||||||
|
# auto-import.
|
||||||
|
# .idea/artifacts
|
||||||
|
# .idea/compiler.xml
|
||||||
|
# .idea/jarRepositories.xml
|
||||||
|
# .idea/modules.xml
|
||||||
|
# .idea/*.iml
|
||||||
|
# .idea/modules
|
||||||
|
# *.iml
|
||||||
|
# *.ipr
|
||||||
|
|
||||||
|
# CMake
|
||||||
|
|
||||||
|
# Mongo Explorer plugin
|
||||||
|
|
||||||
|
# File-based project format
|
||||||
|
|
||||||
|
# IntelliJ
|
||||||
|
|
||||||
|
# mpeltonen/sbt-idea plugin
|
||||||
|
|
||||||
|
# JIRA plugin
|
||||||
|
|
||||||
|
# Cursive Clojure plugin
|
||||||
|
|
||||||
|
# SonarLint plugin
|
||||||
|
|
||||||
|
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||||
|
|
||||||
|
# Editor-based Rest Client
|
||||||
|
|
||||||
|
# Android studio 3.1+ serialized cache file
|
||||||
|
|
||||||
|
### JetBrains+all Patch ###
|
||||||
|
# Ignore everything but code style settings and run configurations
|
||||||
|
# that are supposed to be shared within teams.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# End of https://www.toptal.com/developers/gitignore/api/go,goland+all,jetbrains+all
|
3
Day01/go.mod
Normal file
3
Day01/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module Day01
|
||||||
|
|
||||||
|
go 1.23
|
2006
Day01/input.json
Normal file
2006
Day01/input.json
Normal file
File diff suppressed because it is too large
Load Diff
18
Day01/main.go
Normal file
18
Day01/main.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
const DAY int = 01
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("||++++++++++++++++++++++++++++||")
|
||||||
|
fmt.Printf("|| AdventOfCode 2024 - Day %02d ||\n", DAY)
|
||||||
|
fmt.Println("||++++++++++++++++++++++++++++||\n")
|
||||||
|
|
||||||
|
fmt.Println("Original Code")
|
||||||
|
original()
|
||||||
|
fmt.Println("\nOptimized Code by ChatGPT")
|
||||||
|
optimized()
|
||||||
|
}
|
59
Day01/optimized.go
Normal file
59
Day01/optimized.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
func optimized() {
|
||||||
|
// Read the file in one step
|
||||||
|
content, err := os.ReadFile("./input.json")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error when opening input.json: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Part 1: Calculate the sum of absolute differences between sorted lists
|
||||||
|
sort.Ints(payload.ListA)
|
||||||
|
sort.Ints(payload.ListB)
|
||||||
|
|
||||||
|
sumPart1 := 0
|
||||||
|
for i := range payload.ListA {
|
||||||
|
sumPart1 += abs(payload.ListA[i] - payload.ListB[i])
|
||||||
|
}
|
||||||
|
fmt.Printf("Sum of Part 1: %d\n", sumPart1)
|
||||||
|
|
||||||
|
// Part 2: Calculate weighted sum of common elements
|
||||||
|
sumPart2 := 0
|
||||||
|
countMap := make(map[int]int)
|
||||||
|
for _, v := range payload.ListB {
|
||||||
|
countMap[v]++
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range payload.ListA {
|
||||||
|
if countMap[v] > 0 {
|
||||||
|
sumPart2 += v * countMap[v]
|
||||||
|
countMap[v] = 0 // Avoid double-counting
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
58
Day01/original.go
Normal file
58
Day01/original.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
func original() {
|
||||||
|
content, err := ioutil.ReadFile("./input.json")
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Part 1
|
||||||
|
listA := payload["list_a"]
|
||||||
|
listB := payload["list_b"]
|
||||||
|
|
||||||
|
sort.Ints(listA)
|
||||||
|
sort.Ints(listB)
|
||||||
|
|
||||||
|
sumPart1 := 0
|
||||||
|
for i := 0; i < len(listA); i++ {
|
||||||
|
if listA[i] > listB[i] {
|
||||||
|
sumPart1 += listA[i] - listB[i]
|
||||||
|
} else {
|
||||||
|
sumPart1 += listB[i] - listA[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Sum of Part 1: ", sumPart1)
|
||||||
|
|
||||||
|
// Part 2
|
||||||
|
listA = payload["list_a"]
|
||||||
|
listB = payload["list_b"]
|
||||||
|
|
||||||
|
sumPart2 := 0
|
||||||
|
|
||||||
|
for _, v1 := range listA {
|
||||||
|
counter := 0
|
||||||
|
for _, v2 := range listB {
|
||||||
|
if v1 == v2 {
|
||||||
|
counter++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sumPart2 += counter * v1
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Sum of Part 2: ", sumPart2)
|
||||||
|
}
|
Reference in New Issue
Block a user