aoc2023/day-14/one.go

59 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-12-14 22:22:41 +00:00
package main
2023-12-14 22:24:56 +00:00
// getload returns the load of all boulders for a single image
2023-12-14 22:22:41 +00:00
func getload(raw []string) int {
load := 0
for _, line := range raw {
for i, r := range line {
if r == 'O' {
load += (i + 1)
}
}
}
return load
}
2023-12-14 22:24:56 +00:00
// spacesToRight determines the number of spaces right of a boulder
2023-12-14 22:22:41 +00:00
func spacesToRight(i int, line string) int {
num := 0
outer:
for i += 1; i < len(line); i++ {
switch line[i] {
case '.':
num++
case '#', 'O':
break outer
}
}
return num
}
2023-12-14 22:24:56 +00:00
// rollRight rolls a boulder right until there's no space
2023-12-14 22:22:41 +00:00
func rollRight(line string) string {
for i := len(line) - 1; i >= 0; i-- {
if line[i] == 'O' {
n := spacesToRight(i, line)
if n == 0 {
continue
}
line = replaceAtIndex(line, '.', i)
line = replaceAtIndex(line, 'O', i+n)
}
}
return line
}
2023-12-15 01:16:30 +00:00
// one returns the load of all boulders after a single image transposition+roll
2023-12-14 22:22:41 +00:00
func one(lines []string) int {
image := newImg(len(lines))
copy(image.raw, lines)
for i, line := range image.transposed() {
image.raw[i] = rollRight(line)
}
return getload(image.raw)
}