Skip to content

Commit

Permalink
2023-05
Browse files Browse the repository at this point in the history
  • Loading branch information
dbut2 committed Dec 5, 2023
1 parent d6e7564 commit dbd84ea
Show file tree
Hide file tree
Showing 7 changed files with 655 additions and 1 deletion.
92 changes: 92 additions & 0 deletions 2023/05/01.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package main

import (
"embed"
"fmt"
"strings"

"github.com/dbut2/advent-of-code/pkg/sti"
"github.com/dbut2/advent-of-code/pkg/test"
"github.com/dbut2/advent-of-code/pkg/utils"
)

//go:embed input.txt
var input string

//go:embed test*.txt
var tests embed.FS

func main() {
t := test.Register(tests, solve)
t.Expect(1, 35)
fmt.Println(solve(input))
}

func solve(input string) int {
s := utils.ParseInput(input)

var seeds []int
var mappings []mapping

currentMapping := mapping{}
for _, line := range s {
if line == "" {
continue
}

if strings.Contains(line, "seeds: ") {
seedsString := strings.Split(line, "seeds: ")
seedList := strings.Split(seedsString[1], " ")
for _, seedItem := range seedList {
seeds = append(seeds, sti.Sti(seedItem))
}
continue
}

if strings.Contains(line, "-") {
if len(currentMapping) > 0 {
mappings = append(mappings, currentMapping)
}
currentMapping = mapping{}
continue
}

values := strings.Split(line, " ")

currentMapping = append(currentMapping, submapping{
source: sti.Sti(values[1]),
size: sti.Sti(values[2]),
offset: sti.Sti(values[0]) - sti.Sti(values[1]),
})
}
if len(currentMapping) > 0 {
mappings = append(mappings, currentMapping)
}

lowest := -1
for _, seed := range seeds {
val := seed

for _, mapping := range mappings {
for _, submapping := range mapping {
if val >= submapping.source && val <= submapping.source+submapping.size {
val += submapping.offset
break
}
}
}

if lowest == -1 {
lowest = val
}
lowest = min(lowest, val)
}

return lowest
}

type mapping []submapping

type submapping struct {
source, size, offset int
}
119 changes: 119 additions & 0 deletions 2023/05/02.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package main

import (
"embed"
"fmt"
"strings"

"github.com/dbut2/advent-of-code/pkg/sti"
"github.com/dbut2/advent-of-code/pkg/test"
"github.com/dbut2/advent-of-code/pkg/utils"
)

//go:embed input.txt
var input string

//go:embed test*.txt
var tests embed.FS

func main() {
t := test.Register(tests, solve)
t.Expect(2, 46)
fmt.Println(solve(input))
}

func solve(input string) int {
s := utils.ParseInput(input)

var seedPairs [][2]int
var mappings []mapping

currentMapping := mapping{}
for _, line := range s {
if line == "" {
continue
}

if strings.Contains(line, "seeds: ") {
seedsString := strings.Split(line, "seeds: ")
seedList := strings.Split(seedsString[1], " ")

for i := 0; i < len(seedList); i += 2 {
seedPairs = append(seedPairs, [2]int{sti.Sti(seedList[i]), sti.Sti(seedList[i]) + sti.Sti(seedList[i+1])})
}
continue
}

if strings.Contains(line, "-") {
if len(currentMapping) > 0 {
mappings = append(mappings, currentMapping)
}
currentMapping = mapping{}
continue
}

values := strings.Split(line, " ")

currentMapping = append(currentMapping, submapping{
source: sti.Sti(values[1]),
size: sti.Sti(values[2]),
offset: sti.Sti(values[0]) - sti.Sti(values[1]),
})
}
if len(currentMapping) > 0 {
mappings = append(mappings, currentMapping)
}

lowest := -1

for _, pair := range seedPairs {
ranges := [][2]int{pair}

for _, mapping := range mappings {
for _, submapping := range mapping {
ranges = splitRangesAt(ranges, submapping.source)
}

for i := range ranges {
ranges[i][0] = mapping.convert(ranges[i][0])
ranges[i][1] = mapping.convert(ranges[i][1])
}
}

for i := range ranges {
if lowest == -1 {
lowest = ranges[i][0]
}

lowest = min(lowest, ranges[i][0])
}
}

return lowest
}

type mapping []submapping

type submapping struct {
source, size, offset int
}

func (m mapping) convert(in int) int {
for _, c := range m {
if in >= c.source && in <= c.source+c.size {
return in + c.offset
}
}
return in
}

func splitRangesAt(s [][2]int, n int) [][2]int {
for i, ss := range s {
if n > ss[0] && n <= ss[1] {
s[i][1] = n - 1
s = append(s, [2]int{n, ss[1]})
return s
}
}
return s
}
Loading

0 comments on commit dbd84ea

Please sign in to comment.