Day 2 done
This commit is contained in:
parent
18f1bb5698
commit
fbb860804a
7
.vscode/launch.json
vendored
Normal file
7
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": []
|
||||
}
|
||||
13
cmd/02/main.go
Normal file
13
cmd/02/main.go
Normal file
@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"aoc2024/internal/common"
|
||||
"aoc2024/internal/day02"
|
||||
)
|
||||
|
||||
func main() {
|
||||
input := common.LoadInputs("02_1.txt")
|
||||
result1, result2 := day02.Handle(input)
|
||||
println(result1)
|
||||
println(result2)
|
||||
}
|
||||
1000
inputs/02_1.txt
Normal file
1000
inputs/02_1.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -17,7 +17,7 @@ func Must[T any](obj T, err error) T {
|
||||
|
||||
func LoadInputs(input string) []string {
|
||||
fmt.Println("Opening file...")
|
||||
bytes := Must(os.ReadFile("././inputs/" + input))
|
||||
bytes := Must(os.ReadFile("/home/g3po/Projects/advent_of_code_2024/inputs/" + input))
|
||||
content := strings.Split(string(bytes), "\n")
|
||||
return content[:len(content)-1]
|
||||
}
|
||||
|
||||
83
internal/day02/handler.go
Normal file
83
internal/day02/handler.go
Normal file
@ -0,0 +1,83 @@
|
||||
package day02
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"aoc2024/internal/common"
|
||||
)
|
||||
|
||||
type Difference int
|
||||
|
||||
const (
|
||||
None Difference = iota
|
||||
Positive
|
||||
Negative
|
||||
)
|
||||
|
||||
func (d Difference) IsTransitionOk(diff Difference) bool {
|
||||
if d == None && diff != None {
|
||||
return true
|
||||
}
|
||||
if d == diff && d != None {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func Handle(input []string) (int, int) {
|
||||
safe := 0
|
||||
dampened := 0
|
||||
for _, line := range input {
|
||||
levels := strings.Split(line, " ")
|
||||
|
||||
if isSafe(levels) {
|
||||
safe++
|
||||
dampened++
|
||||
} else if isSafeWithDeletion(levels) {
|
||||
dampened++
|
||||
}
|
||||
}
|
||||
return safe, dampened
|
||||
}
|
||||
|
||||
func getTrend(diff int) Difference {
|
||||
if diff >= 1 && diff <= 3 {
|
||||
return Positive
|
||||
}
|
||||
if diff <= -1 && diff >= -3 {
|
||||
return Negative
|
||||
}
|
||||
return None
|
||||
}
|
||||
|
||||
func isSafe(row []string) bool {
|
||||
trend := None
|
||||
for i := 0; i < len(row)-1; i++ {
|
||||
diff := common.StrToInt(row[i]) - common.StrToInt(row[i+1])
|
||||
newTrend := getTrend(diff)
|
||||
|
||||
if !trend.IsTransitionOk(newTrend) {
|
||||
return false
|
||||
|
||||
}
|
||||
trend = newTrend
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isSafeWithDeletion(row []string) bool {
|
||||
safe := false
|
||||
for i := range row {
|
||||
copyRow := make([]string, len(row))
|
||||
copy(copyRow, row)
|
||||
if i == len(row)-1 {
|
||||
safe = isSafe(copyRow[:i])
|
||||
} else {
|
||||
safe = isSafe(append(copyRow[:i], copyRow[i+1:]...))
|
||||
}
|
||||
if safe {
|
||||
return safe
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user