-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18.go
147 lines (130 loc) · 3.52 KB
/
day18.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package day18 solves AoC 2023 day 18.
package day18
import (
"fmt"
"strconv"
"github.com/fis/aoc/glue"
"github.com/fis/aoc/util"
)
func init() {
glue.RegisterSolver(2023, 18, glue.RegexpSolver{
Solver: glue.WithParser(parseDigPlan, solve),
Regexp: `([UDLR]) (\d+) \(#([0-9a-f]{6})\)`,
})
}
func solve(plan []instruction) ([]string, error) {
p1 := polygonArea(traceLoop(plan, instruction.small))
p2 := polygonArea(traceLoop(plan, instruction.big))
return glue.Ints(p1, p2), nil
}
func traceLoop(plan []instruction, f func(instruction) (direction, int)) (verts cycle) {
startI, startX, startY := 0, 0, 0
x, y := 0, 0
cubes := make(cycle, len(plan))
for i, inst := range plan {
cubes[i] = util.P{x, y}
if y < startY || (y == startY && x < startX) {
startI, startX, startY = i, x, y
}
dir, dist := f(inst)
dx, dy := dir.delta()
x, y = x+dist*dx, y+dist*dy
}
if x != 0 || y != 0 {
panic("not a closed loop")
}
verts = make(cycle, len(cubes))
di := 0
if a, b, c := cubes.at(startI-1), cubes.at(startI), cubes.at(startI+1); a.X == b.X && a.Y > b.Y && c.X > b.X && c.Y == b.Y {
di = 1
} else if a.X > b.X && a.Y == b.Y && c.X == b.X && c.Y > b.Y {
di = -1
} else {
panic("top-left corner is not right")
}
for i := 0; i < len(cubes); i++ {
bi := startI + i*di
a, b, c := cubes.at(bi-di), cubes.at(bi), cubes.at(bi+di)
dx, dy := 0, 0
if c.Y > b.Y || a.Y < b.Y {
dx = 1
}
if c.X < b.X || a.X > b.X {
dy = 1
}
verts[i] = util.P{b.X + dx, b.Y + dy}
}
return verts
}
func polygonArea(verts cycle) (area int) {
for i := 0; i < len(verts); i += 2 {
a, b := verts.at(i), verts.at(i+1)
area += a.Y * (a.X - b.X)
}
return area
}
type cycle []util.P
func (c cycle) at(i int) util.P { return c[(i+len(c))%len(c)] }
type direction byte
const (
dirR direction = 0
dirD direction = 1
dirL direction = 2
dirU direction = 3
)
var dirDelta = [4]util.P{
dirR: {1, 0},
dirD: {0, 1},
dirL: {-1, 0},
dirU: {0, -1},
}
func (d direction) delta() (dx, dy int) { return dirDelta[d].X, dirDelta[d].Y }
var dirLetters = map[byte]direction{
'R': dirR,
'D': dirD,
'L': dirL,
'U': dirU,
}
type instruction struct {
smallDir direction
smallDist int
bigDir direction
bigDist int
}
func (i instruction) small() (direction, int) { return i.smallDir, i.smallDist }
func (i instruction) big() (direction, int) { return i.bigDir, i.bigDist }
func parseDigPlan(parts []string) (instruction, error) {
smallDir, ok := dirLetters[parts[0][0]]
if !ok {
return instruction{}, fmt.Errorf("bad direction: %q", parts[0])
}
smallDist, err := strconv.Atoi(parts[1])
if err != nil {
return instruction{}, err
}
hex, err := strconv.ParseInt(parts[2], 16, 64)
if err != nil {
return instruction{}, err
}
hexDir := hex & 0xf
if hexDir >= 4 {
return instruction{}, fmt.Errorf("bad hex direction: %x", hexDir)
}
return instruction{
smallDir: smallDir, smallDist: smallDist,
bigDir: direction(hexDir), bigDist: int(hex >> 4),
}, nil
}