[TASK] Solved part 1

This commit is contained in:
2023-12-02 20:16:27 +01:00
commit 8a6f4ba6b0
11 changed files with 1267 additions and 0 deletions

86
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,86 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# 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

9
.idea/advent-of-code-2023-go.iml generated Normal file
View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/advent-of-code-2023-go.iml" filepath="$PROJECT_DIR$/.idea/advent-of-code-2023-go.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

79
day01/day01.go Normal file
View File

@@ -0,0 +1,79 @@
package day01
import (
"strings"
"unicode"
)
func Part1(input string) uint {
lines := strings.Split(input, "\n")
sum := uint(0)
for i := 0; i < len(lines); i++ {
line := lines[i]
for j := 0; j < len(line); j++ {
if unicode.IsNumber(rune(line[j])) {
sum = sum + uint(line[j]-'0')*10
break
}
}
for j := len(line) - 1; j >= 0; j-- {
if unicode.IsNumber(rune(line[j])) {
sum = sum + uint(line[j]-'0')
break
}
}
}
return sum
}
func Part2(input string) uint {
lines := strings.Split(input, "\n")
sum := uint(0)
for i := 0; i < len(lines); i++ {
line := lines[i]
for j := 0; j < len(line); j++ {
n, found := writtenNumberAt(line, j)
if found {
sum = sum + n*10
break
}
}
for j := len(line) - 1; j >= 0; j-- {
n, found := writtenNumberAt(line, j)
if found {
sum = sum + n
break
}
}
}
return sum
}
func writtenNumberAt(line string, i int) (n uint, found bool) {
if unicode.IsNumber(rune(line[i])) {
return uint(line[i] - '0'), true
} else if strings.HasPrefix(line[i:], "one") {
return 1, true
} else if strings.HasPrefix(line[i:], "two") {
return 2, true
} else if strings.HasPrefix(line[i:], "three") {
return 3, true
} else if strings.HasPrefix(line[i:], "four") {
return 4, true
} else if strings.HasPrefix(line[i:], "five") {
return 5, true
} else if strings.HasPrefix(line[i:], "six") {
return 6, true
} else if strings.HasPrefix(line[i:], "seven") {
return 7, true
} else if strings.HasPrefix(line[i:], "eight") {
return 8, true
} else if strings.HasPrefix(line[i:], "nine") {
return 9, true
} else {
return 0, false
}
}

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module advent-of-code-2023-go
go 1.21

1000
input/day01.txt Normal file

File diff suppressed because it is too large Load Diff

4
input/day01_example.txt Normal file
View File

@@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

7
input/day01_example2.txt Normal file
View File

@@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

27
main.go Normal file
View File

@@ -0,0 +1,27 @@
package main
import (
"advent-of-code-2023-go/day01"
"advent-of-code-2023-go/utils"
"os"
)
func main() {
d := day()
input := utils.Readfile(d, false)
switch d {
case 1:
println("Day 1, part 1: ", day01.Part1(input))
println("Day 1, part 1: ", day01.Part2(input))
}
}
func day() int {
latest := 1
if len(os.Args) == 1 {
return latest
}
day := utils.MustAtoi(os.Args[1])
return day
}

38
utils/utils.go Normal file
View File

@@ -0,0 +1,38 @@
package utils
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func PanicOnErr(err error) {
if err != nil {
panic(err)
}
}
func MustAtoi(s string) int {
v, err := strconv.Atoi(s)
PanicOnErr(err)
return v
}
func Readfile(day int, example bool) string {
filename := fmt.Sprintf("input/day%02d.txt", day)
if example {
filename = fmt.Sprintf("input/day%02d_example.txt", day)
}
file, err := os.Open(filename)
PanicOnErr(err)
defer file.Close()
reader := bufio.NewReader(file)
contents, err := io.ReadAll(reader)
PanicOnErr(err)
return strings.TrimSuffix(string(contents), "\n")
}