This commit is contained in:
2024-12-01 12:25:06 +01:00
commit d267bc70e0
4 changed files with 1187 additions and 0 deletions

130
.gitignore vendored Normal file
View File

@@ -0,0 +1,130 @@
# Created by https://www.toptal.com/developers/gitignore/api/goland+all,go,linux
# Edit at https://www.toptal.com/developers/gitignore?templates=goland+all,go,linux
### 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
### Linux ###
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
# End of https://www.toptal.com/developers/gitignore/api/goland+all,go,linux

3
01/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module adventofcode2024/01
go 1.23

1000
01/input.txt Normal file

File diff suppressed because it is too large Load Diff

54
01/main.go Normal file
View File

@@ -0,0 +1,54 @@
package main
import (
"flag"
"fmt"
"math"
"os"
"regexp"
"sort"
"strconv"
)
var (
inputFile = flag.String("input", "input.txt", "input filename")
reLine = regexp.MustCompile(`(?m)^(\d+)\s+(\d+)$`)
)
func main() {
flag.Parse()
input, err := os.ReadFile(*inputFile)
if err != nil {
panic(err)
}
var leftList, rightList []int
for _, match := range reLine.FindAllStringSubmatch(string(input), -1) {
leftNumber, err := strconv.Atoi(match[1])
if err != nil {
panic(err)
}
rightNumber, err := strconv.Atoi(match[2])
if err != nil {
panic(err)
}
leftList = append(leftList, leftNumber)
rightList = append(rightList, rightNumber)
}
sort.SliceStable(leftList, func(i, j int) bool {
return leftList[i] < leftList[j]
})
sort.SliceStable(rightList, func(i, j int) bool {
return rightList[i] < rightList[j]
})
totalDistance := 0
for i := 0; i < len(leftList); i++ {
totalDistance += int(math.Abs(float64(leftList[i] - rightList[i])))
}
fmt.Println(totalDistance)
}