diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6d485f3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,136 @@ +# Created by https://www.toptal.com/developers/gitignore/api/goland+all,linux,windows +# Edit at https://www.toptal.com/developers/gitignore?templates=goland+all,linux,windows + +### 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* + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.toptal.com/developers/gitignore/api/goland+all,linux,windows + +adventofcode + diff --git a/go.mod b/01/go.mod similarity index 100% rename from go.mod rename to 01/go.mod diff --git a/main.go b/01/main.go similarity index 100% rename from main.go rename to 01/main.go diff --git a/02/go.mod b/02/go.mod new file mode 100644 index 0000000..16992bc --- /dev/null +++ b/02/go.mod @@ -0,0 +1,3 @@ +module adventofcode + +go 1.21 diff --git a/02/main.go b/02/main.go new file mode 100644 index 0000000..f8b4eb7 --- /dev/null +++ b/02/main.go @@ -0,0 +1,92 @@ +package main + +import ( + "flag" + "fmt" + "os" + "regexp" + "strconv" + "strings" +) + +var ( + inputFile = flag.String("input", "input.txt", "input filename") + nRegex = regexp.MustCompile(`(?m)\d`) + reMap = map[string]string{ + "one": "1", + "two": "2", + "three": "3", + "four": "4", + "five": "5", + "six": "6", + "seven": "7", + "eight": "8", + "nine": "9", + } +) + +func main() { + flag.Parse() + input, err := os.ReadFile(*inputFile) + if err != nil { + panic(err) + } + + var sum int + for _, line := range strings.Split(string(input), "\n") { + if strings.TrimSpace(line) == "" { + continue + } + + // forward-scan for spelled digits + var forwardReplacedLine, backwardReplacedLine string + for i := 0; i < len(line); i++ { + forwardReplacedLine += string(line[i]) + + // replace all potential spelled out digits by their non-word counterpart (e.g. "one" -> "1") + for key, val := range reMap { + forwardReplacedLine = strings.ReplaceAll(forwardReplacedLine, key, val) + } + } + fmt.Println(forwardReplacedLine) + + // backward-scan for spelled digits + for i := len(line) - 1; i >= 0; i-- { + if i == len(line)-1 { + backwardReplacedLine = string(line[i]) + } else { + backwardReplacedLine = string(line[i]) + backwardReplacedLine + } + + // replace all potential spelled out digits by their non-word counterpart (e.g. "one" -> "1") + for key, val := range reMap { + backwardReplacedLine = strings.ReplaceAll(backwardReplacedLine, key, val) + } + } + fmt.Println(backwardReplacedLine) + + // find single digits in forward-replaced line + firstMatch := nRegex.FindAllStringSubmatch(forwardReplacedLine, -1) + + // find single digits in backward-replaced line + lastMatch := nRegex.FindAllStringSubmatch(backwardReplacedLine, -1) + + // get first matchgroup, first match (aka first digit in forwardReplacedLine) + first := firstMatch[0][0] + + // get last matchgroup, first match (aka last digit in backwardReplacedLine) + last := lastMatch[len(lastMatch)-1:][0][0] + + fmt.Println(first + last) + + // concatenate digits and convert the resulting string to int + asInt, err := strconv.Atoi(first + last) + if err != nil { + panic(err) + } + + sum += asInt + } + + fmt.Println(sum) +}