Piotr Czajkowski 1 тиждень тому
батько
коміт
283a81136c
2 змінених файлів з 98 додано та 1 видалено
  1. 48 1
      08/code.go
  2. 50 0
      08/input

+ 48 - 1
08/code.go

@@ -40,6 +40,52 @@ func readInput(file *os.File) (map[byte][]Point, [][]byte) {
 	return groups, matrix
 }
 
+func abs(a int) int {
+	if a < 0 {
+		return -a
+	}
+
+	return a
+}
+
+func checkAntinode(antinode Point, matrix [][]byte, key byte) bool {
+	if antinode.x < 0 || antinode.y < 0 || antinode.y >= len(matrix) || antinode.x >= len(matrix[0]) {
+		return false
+	}
+
+	if matrix[antinode.y][antinode.x] == key {
+		return false
+	}
+
+	return true
+}
+
+func getAntinodes(groups map[byte][]Point, matrix [][]byte) map[string]Point {
+	antinodes := make(map[string]Point)
+
+	for key, items := range groups {
+		edge := len(items)
+		for i := range items {
+			for j := i + 1; j < edge; j++ {
+				deltaX := items[j].x - items[i].x
+				deltaY := items[j].y - items[i].y
+
+				firstAntinode := Point{x: items[i].x - deltaX, y: items[i].y - deltaY}
+				if checkAntinode(firstAntinode, matrix, key) {
+					antinodes[firstAntinode.key()] = firstAntinode
+				}
+
+				secondAntinode := Point{x: items[j].x + deltaX, y: items[j].y + deltaY}
+				if checkAntinode(secondAntinode, matrix, key) {
+					antinodes[secondAntinode.key()] = secondAntinode
+				}
+			}
+		}
+	}
+
+	return antinodes
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("You need to specify a file!")
@@ -52,5 +98,6 @@ func main() {
 	}
 
 	groups, matrix := readInput(file)
-	fmt.Println(groups, matrix)
+	antinodes := getAntinodes(groups, matrix)
+	fmt.Println("Part1:", len(antinodes))
 }

+ 50 - 0
08/input

@@ -0,0 +1,50 @@
+.......................V.........e...O............
+..........q.pj8...............................u...
+...................8..............................
+.............8.....6.................J....l....u..
+........................6................J..Z..B..
+......e.........E...........................O.J...
+......Jq..........................5...............
+...............E...........e.Q..5.f...............
+..............................Q..A.....f..B.....O.
+....V...................j.....Af..................
+............8......n..............l...f....Z7.....
+...............n..........4........A........BD....
+...........j...................Q..z.......R....l..
+N.........6....q.....3....n.........D...........Z.
+.............a.6..3.F........D..I.................
+.............03.................Q.......h...2.....
+......................A.u.......................m.
+.V........F......L.............5..........z.R....Z
+.......N....q.................n.......L.E.........
+..................M.........y.....................
+......N............................m.L..y........R
+.o....................L...........I...7..R........
+......o..........9..............2.......D.........
+..od.............y...........................I....
+d........3.....M...........E.............I........
+......X.W....................p.2.....7...z....s...
+V......o........M.....9.................G......7..
+.................M.....................h..0....m..
+.......d.......F......p.........s.h........z......
+..r..........Y.i................9............s....
+.....W..a.Y..........y.............p..............
+.....g.......r........w...........................
+....r.....b...............g........x.s.....h......
+....a.....d.......................................
+.....................S.......w.............1......
+..Y...............................H...............
+...b...........Y........................e..t...0.v
+..........i..........w.........9....T........v....
+.................U...........2....................
+.........S........t......T........................
+....................U..................Gt.........
+....U...S..........................P.....1.B......
+.r...X............w.......P.....x.j...............
+...W......x..b........g........F.....a............
+S.i.................................1.......H.....
+.......U......b......x.....X..........G.1.........
+...i....X....................P..4........H........
+.................................H................
+......W...................T4...g................v.
+..........................v........GP..4.....t....