Skip to content

Commit

Permalink
solve day 5
Browse files Browse the repository at this point in the history
  • Loading branch information
SylivanKenobi committed Dec 5, 2024
1 parent 1623e5d commit 7a236bc
Show file tree
Hide file tree
Showing 5 changed files with 1,487 additions and 7 deletions.
2 changes: 0 additions & 2 deletions 2024/day3/day3.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package day3

import (
"adventofcode/utils"
"fmt"
"regexp"
"strconv"
"strings"
Expand All @@ -23,7 +22,6 @@ func Part1(file string) int {
num2, _ := strconv.Atoi(numbers[1])
result += (num1 * num2)
}
fmt.Println(result)
return result
}

Expand Down
10 changes: 5 additions & 5 deletions 2024/day4/day4.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ func Part1(file string) int {
// right
if len(letters)-3 > j {
straight := strings.Join(letters[j:j+4], "")
if Holliday(straight) {
if Holiday(straight) {
result += 1
}
}

// down
if len(lines)-3 > i {
down := strings.Join([]string{string(lines[i][j]), string(lines[i+1][j]), string(lines[i+2][j]), string(lines[i+3][j])}, "")
if Holliday(down) {
if Holiday(down) {
// fmt.Println(down)
result += 1
}
Expand All @@ -33,14 +33,14 @@ func Part1(file string) int {
// diagonal down right
if len(lines)-3 > i && len(letters)-3 > j {
ddr := strings.Join([]string{string(lines[i][j]), string(lines[i+1][j+1]), string(lines[i+2][j+2]), string(lines[i+3][j+3])}, "")
if Holliday(ddr) {
if Holiday(ddr) {
result += 1
}
}
// diagonal down left
if len(lines)-3 > i && 3 <= j {
ddl := strings.Join([]string{string(lines[i][j]), string(lines[i+1][j-1]), string(lines[i+2][j-2]), string(lines[i+3][j-3])}, "")
if Holliday(ddl) {
if Holiday(ddl) {
result += 1
}
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func Part2(file string) int {
return result
}

func Holliday(holliday string) bool {
func Holiday(holliday string) bool {
return (holliday == "XMAS" || holliday == "SAMX")
}

Expand Down
97 changes: 97 additions & 0 deletions 2024/day5/day5.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package day4

import (
"adventofcode/utils"
"regexp"
"slices"
"strconv"
"strings"
)

func Part1(file string) int {
lines := utils.ReadFileByLine(file)

result := 0
rules, updates := splitInput(lines)
var validUpdates []int
for _, update := range updates {
valid := true
for _, rule := range rules {
i := slices.Index(update, rule[0])
j := slices.Index(update, rule[1])
if i >= 0 && j >= 0 && i > j {
valid = false
}
}
if valid {
middle := update[len(update)/2]
validUpdates = append(validUpdates, middle)
}
}
for _, validUpdate := range validUpdates {
result += validUpdate
}
return result
}

func Part2(file string) int {
lines := utils.ReadFileByLine(file)

result := 0
rules, updates := splitInput(lines)
var validUpdates []int
for _, update := range updates {
inValid := false
pass := false
for !pass {
pass = true
for _, rule := range rules {
i := slices.Index(update, rule[0])
j := slices.Index(update, rule[1])
if i >= 0 && j >= 0 && i > j {
tmpi := update[i]
update[i] = update[j]
update[j] = tmpi
inValid = true
pass = false
}
}
}
if inValid {
middle := update[len(update)/2]
validUpdates = append(validUpdates, middle)
}
}
for _, validUpdate := range validUpdates {
result += validUpdate
}
return result
}

func splitInput(input []string) ([][]int, [][]int) {
blank := false
var rules [][]int
var updates [][]int
for _, line := range input {
if line != "" {
if !blank {
rule := strToIntArr(regexp.MustCompile(`[0-9]*`).FindAllString(line, -1))
rules = append(rules, rule)
} else {
updates = append(updates, strToIntArr(strings.Split(line, ",")))
}
} else {
blank = true
}
}
return rules, updates
}

func strToIntArr(input []string) []int {
var r []int
for _, n := range input {
a, _ := strconv.Atoi(n)
r = append(r, a)
}
return r
}
Loading

0 comments on commit 7a236bc

Please sign in to comment.