]> fbox.kageds.com Git - adventofcode.git/blob - 2023/go/day03/day03.go
day06
[adventofcode.git] / 2023 / go / day03 / day03.go
1 package day03
2
3 import (
4 "adventofcode2023/utils"
5 _ "adventofcode2023/utils"
6 _ "adventofcode2023/utils/grid2d"
7 "adventofcode2023/utils/inputs"
8 "errors"
9 "fmt"
10 "unicode"
11 )
12
13 type Coord struct {
14 x int
15 y int
16 }
17 type Part struct {
18 number string
19 location Coord
20 }
21
22 func Part1(input string) int {
23 grid := inputs.ToGrid2D(input, "\n", "", ".", func(c string) string { return c})
24 partLocs := []Coord{}
25 for j := 0; j < grid.SizeY(); j++ {
26 for i := 0; i < grid.SizeX(); i++ {
27 if !unicode.IsDigit(rune(grid.Get(i, j)[0])) { continue}
28 directions := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0},{-1,1},{1,1},{-1,-1},{1,-1}}
29 for _, dir := range directions {
30 x := i + dir[0]
31 y := j + dir[1]
32 if x < 0 || x >= grid.SizeX() || y < 0 || y >= grid.SizeY() {
33 continue
34 }
35 if !unicode.IsDigit(rune(grid.Get(x, y)[0])) && grid.Get(x, y) != "." {
36 fmt.Printf("partLoc x:%v y:%v %v\n", i, j, string(grid.Get(i, j)))
37 partLocs = append(partLocs, Coord{i, j})
38 break
39 }
40 }
41 }
42 }
43 fmt.Println(partLocs)
44 sum := 0
45 for _, partLoc := range partLocs {
46 if grid.Get(partLoc.x, partLoc.y) == "." {
47 continue
48 }
49 partNum := grid.Get(partLoc.x, partLoc.y)
50 for i:=1;unicode.IsDigit(rune(grid.Get(partLoc.x+i, partLoc.y)[0]));i++{
51 partNum += grid.Get(partLoc.x+i, partLoc.y)
52 grid.Set(partLoc.x+i, partLoc.y, ".")
53 }
54 for i:=-1;unicode.IsDigit(rune(grid.Get(partLoc.x+i, partLoc.y)[0]));i--{
55 partNum = grid.Get(partLoc.x+i, partLoc.y) + partNum
56 grid.Set(partLoc.x+i, partLoc.y, ".")
57 }
58 fmt.Println(partNum)
59 sum += utils.MustAtoi(partNum)
60 }
61 return sum
62 }
63
64 func Part2(input string) int {
65 grid := inputs.ToGrid2D(input, "\n", "", ".", func(c string) string { return c})
66 partLocs := []Coord{}
67 gearLocs := []Coord{}
68 for j := 0; j < grid.SizeY(); j++ {
69 for i := 0; i < grid.SizeX(); i++ {
70 if grid.Get(i, j) == "*" {
71 gearLocs = append(gearLocs, Coord{i, j})
72 }
73 if !unicode.IsDigit(rune(grid.Get(i, j)[0])) { continue}
74 directions := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0},{-1,1},{1,1},{-1,-1},{1,-1}}
75 for _, dir := range directions {
76 x := i + dir[0]
77 y := j + dir[1]
78 if x < 0 || x >= grid.SizeX() || y < 0 || y >= grid.SizeY() {
79 continue
80 }
81 if !unicode.IsDigit(rune(grid.Get(x, y)[0])) && grid.Get(x, y) != "." {
82 partLocs = append(partLocs, Coord{i, j})
83 break
84 }
85 }
86 }
87 }
88 fmt.Println(partLocs)
89 fmt.Println(gearLocs)
90
91 parts := []Part{}
92 for _, partLoc := range partLocs {
93 l := 0
94 if grid.Get(partLoc.x, partLoc.y) == "." {
95 continue
96 }
97 partNum := grid.Get(partLoc.x, partLoc.y)
98 for i:=1;unicode.IsDigit(rune(grid.Get(partLoc.x+i, partLoc.y)[0]));i++{
99 partNum += grid.Get(partLoc.x+i, partLoc.y)
100 grid.Set(partLoc.x+i, partLoc.y, ".")
101 }
102 l = partLoc.x
103 for i:=-1;unicode.IsDigit(rune(grid.Get(partLoc.x+i, partLoc.y)[0]));i--{
104 partNum = grid.Get(partLoc.x+i, partLoc.y) + partNum
105 grid.Set(partLoc.x+i, partLoc.y, ".")
106 l = partLoc.x+i
107 }
108 fmt.Println(partNum)
109 parts = append(parts, Part{number: partNum, location: Coord{x: l, y: partLoc.y}})
110 }
111 sum := 0
112 for _, gearLoc := range gearLocs {
113 var gearedParts [2]Part
114
115 directions := []Coord{{0,1},{0,-1},{1,0},{-1,0},{-1,1},{1,1},{-1,-1},{1,-1}}
116 for _, dir := range directions {
117 part, err := getPart(gearLoc, dir, parts)
118 if err != nil {continue}
119 if gearedParts[0] == (Part{}) {
120 gearedParts[0] = part
121 } else if gearedParts[0] == part {
122 fmt.Println("duplicate part found")
123 } else {
124 gearedParts[1] = part
125 }
126 }
127 fmt.Println(gearedParts)
128 if gearedParts[0] != (Part{}) && gearedParts[1] != (Part{}) {
129 sum += utils.MustAtoi(gearedParts[0].number) * utils.MustAtoi(gearedParts[1].number)
130 }
131 }
132 // fmt.Println(parts)
133 return sum
134 }
135
136 func getPart(gearloc Coord, offset Coord, parts []Part) (Part, error) {
137 coord := Coord{x: gearloc.x + offset.x, y: gearloc.y + offset.y}
138 for _, part := range parts {
139 if coord.x >= part.location.x &&
140 coord.x <= part.location.x + len(part.number) -1 &&
141 coord.y == part.location.y {
142 return part, nil
143 }
144 }
145 return Part{}, errors.New("Not Found")
146 }