Piotr Czajkowski vor 2 Wochen
Ursprung
Commit
19f1fb1134
2 geänderte Dateien mit 71 neuen und 0 gelöschten Zeilen
  1. 37 0
      04/code.go
  2. 34 0
      04/description.txt

+ 37 - 0
04/code.go

@@ -67,6 +67,42 @@ func part1(matrix [][]byte, word string) int {
 	return count
 }
 
+func isXmas(i, j int, matrix [][]byte, rows, cols int) bool {
+	if i-1 < 0 || i+1 >= rows || j-1 < 0 || j+1 >= cols {
+		return false
+	}
+
+	opposites := [][]int{{i - 1, j - 1}, {i - 1, j + 1}, {i + 1, j - 1}, {i + 1, j + 1}}
+	for _, opposite := range opposites {
+		if matrix[opposite[0]][opposite[1]] != 'M' && matrix[opposite[0]][opposite[1]] != 'S' {
+			return false
+		}
+	}
+
+	if matrix[opposites[0][0]][opposites[0][1]] == matrix[opposites[3][0]][opposites[3][1]] || matrix[opposites[1][0]][opposites[1][1]] == matrix[opposites[2][0]][opposites[2][1]] {
+		return false
+	}
+
+	return true
+}
+
+func part2(matrix [][]byte) int {
+	var count int
+	rows, cols := len(matrix), len(matrix[0])
+
+	for i := 0; i < rows; i++ {
+		for j := 0; j < cols; j++ {
+			if matrix[i][j] == 'A' {
+				if isXmas(i, j, matrix, rows, cols) {
+					count++
+				}
+			}
+		}
+	}
+
+	return count
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("You need to specify a file!")
@@ -80,4 +116,5 @@ func main() {
 
 	matrix := readInput(file)
 	fmt.Println("Part1:", part1(matrix, "XMAS"))
+	fmt.Println("Part2:", part2(matrix))
 }

+ 34 - 0
04/description.txt

@@ -39,3 +39,37 @@ S.S.S.S.SS
 .X.X.XMASX
 
 Take a look at the little Elf's word search. How many times does XMAS appear?
+
+Your puzzle answer was 2458.
+--- Part Two ---
+
+The Elf looks quizzically at you. Did you misunderstand the assignment?
+
+Looking for the instructions, you flip over the word search to find that this isn't actually an XMAS puzzle; it's an X-MAS puzzle in which you're supposed to find two MAS in the shape of an X. One way to achieve that is like this:
+
+M.S
+.A.
+M.S
+
+Irrelevant characters have again been replaced with . in the above diagram. Within the X, each MAS can be written forwards or backwards.
+
+Here's the same example from before, but this time all of the X-MASes have been kept instead:
+
+.M.S......
+..A..MSMS.
+.M.S.MAA..
+..A.ASMSM.
+.M.S.M....
+..........
+S.S.S.S.S.
+.A.A.A.A..
+M.M.M.M.M.
+..........
+
+In this example, an X-MAS appears 9 times.
+
+Flip the word search from the instructions back over to the word search side and try again. How many times does an X-MAS appear?
+
+Your puzzle answer was 1945.
+
+Both parts of this puzzle are complete! They provide two gold stars: **