Implemtend Day 01 of 2015 with Golang
This commit is contained in:
0
2015/1/README.md
Normal file
0
2015/1/README.md
Normal file
1
2015/1/example.txt
Normal file
1
2015/1/example.txt
Normal file
@@ -0,0 +1 @@
|
||||
))(((((
|
||||
10
2015/1/golang/.vscode/settings.json
vendored
Normal file
10
2015/1/golang/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"[go]": {
|
||||
"editor.insertSpaces": false,
|
||||
"editor.formatOnSave": true,
|
||||
"editor.defaultFormatter": "golang.go",
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.organizeImports": "explicit"
|
||||
}
|
||||
}
|
||||
}
|
||||
3
2015/1/golang/go.mod
Normal file
3
2015/1/golang/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module aoc/day-1
|
||||
|
||||
go 1.24.3
|
||||
55
2015/1/golang/main.go
Normal file
55
2015/1/golang/main.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
data := readFile("../input.txt")
|
||||
characters := strings.Split(data, "")
|
||||
part1 := part1(characters)
|
||||
part2 := part2(characters)
|
||||
fmt.Printf("Part 1: %d\n", part1)
|
||||
fmt.Printf("Part 2: %d\n", part2)
|
||||
}
|
||||
|
||||
func part1(input []string) int {
|
||||
floorLevel := 0
|
||||
for _, v := range input {
|
||||
switch v {
|
||||
case "(":
|
||||
floorLevel += 1
|
||||
default:
|
||||
floorLevel -= 1
|
||||
}
|
||||
}
|
||||
return floorLevel
|
||||
}
|
||||
|
||||
func part2(input []string) int {
|
||||
floorLevel := 0
|
||||
position := 0
|
||||
for i, v := range input {
|
||||
switch v {
|
||||
case "(":
|
||||
floorLevel += 1
|
||||
default:
|
||||
floorLevel -= 1
|
||||
}
|
||||
if floorLevel == -1 {
|
||||
position = i + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
return position
|
||||
}
|
||||
|
||||
func readFile(filename string) string {
|
||||
content, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(content)
|
||||
}
|
||||
1
2015/1/input.txt
Normal file
1
2015/1/input.txt
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user