-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14-1.go
82 lines (74 loc) · 1.31 KB
/
day14-1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"slices"
)
type day14Map [][]byte
func day14part1(filename string) (string, error) {
m, err := day14ReadMap(filename)
if err != nil {
return "", err
}
var total int
for x := 0; x < len(m[0]); x++ {
col := m.column(x)
day14SlideLeft(col)
total += day14Weight(col)
}
return fmt.Sprint(total), nil
}
func day14Slide(s []byte, cmp func(byte, byte) int) {
var i, l int
for i < len(s) {
if l == i {
switch s[i] {
case 'O', '.':
l = i
case '#':
l = i + 1
}
i += 1
continue
}
if s[i] == '#' {
slices.SortFunc(s[l:i], cmp)
l = i + 1
} else if i == len(s)-1 {
slices.SortFunc(s[l:i+1], cmp)
}
i += 1
}
}
func day14SlideLeft(s []byte) {
day14Slide(s, func(a, b byte) int {
return int(b) - int(a)
})
}
func (m day14Map) column(x int) []byte {
var res []byte
for _, vy := range m {
res = append(res, vy[x])
}
return res
}
func day14ReadMap(filename string) (day14Map, error) {
var m day14Map
if err := forLineError(filename, func(line string) error {
m = append(m, []byte(line))
return nil
}); err != nil {
return m, err
}
return m, nil
}
// Weight as function of reverse distance from end.
func day14Weight(s []byte) int {
l := len(s)
var res int
for i, v := range s {
if v == 'O' {
res += l - i
}
}
return res
}