this post was submitted on 04 Dec 2024
18 points (95.0% liked)

Advent Of Code

996 readers
4 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
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

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 4: Ceres Search

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 1 points 1 month ago

Go

Just a bunch of ifs and bounds checking. Part 2 was actually simpler.

Code

func part1(W [][]rune) {
	m := len(W)
	n := len(W[0])
	xmasCount := 0

	for i := 0; i < m; i++ {
		for j := 0; j < n; j++ {
			if W[i][j] != 'X' {
				continue
			}
			if j < n-3 && W[i][j+1] == 'M' && W[i][j+2] == 'A' && W[i][j+3] == 'S' {
				// Horizontal left to right
				xmasCount++
			}
			if j >= 3 && W[i][j-1] == 'M' && W[i][j-2] == 'A' && W[i][j-3] == 'S' {
				// Horizontal right to left
				xmasCount++
			}
			if i < m-3 && W[i+1][j] == 'M' && W[i+2][j] == 'A' && W[i+3][j] == 'S' {
				// Vertical up to down
				xmasCount++
			}
			if i >= 3 && W[i-1][j] == 'M' && W[i-2][j] == 'A' && W[i-3][j] == 'S' {
				// Vertical down to up
				xmasCount++
			}
			if j < n-3 && i < m-3 && W[i+1][j+1] == 'M' && W[i+2][j+2] == 'A' && W[i+3][j+3] == 'S' {
				// Diagonal left to right and up to down
				xmasCount++
			}
			if j >= 3 && i < m-3 && W[i+1][j-1] == 'M' && W[i+2][j-2] == 'A' && W[i+3][j-3] == 'S' {
				// Diagonal right to left and up to down
				xmasCount++
			}
			if j < n-3 && i >= 3 && W[i-1][j+1] == 'M' && W[i-2][j+2] == 'A' && W[i-3][j+3] == 'S' {
				// Diagonal left to right and down to up
				xmasCount++
			}
			if j >= 3 && i >= 3 && W[i-1][j-1] == 'M' && W[i-2][j-2] == 'A' && W[i-3][j-3] == 'S' {
				// Diagonal right to left and down to up
				xmasCount++
			}
		}
	}

	fmt.Println(xmasCount)
}

func part2(W [][]rune) {
	m := len(W)
	n := len(W[0])
	xmasCount := 0

	for i := 0; i <= m-3; i++ {
		for j := 0; j <= n-3; j++ {
			if W[i+1][j+1] != 'A' {
				continue
			}
			if W[i][j] == 'M' && W[i][j+2] == 'M' && W[i+2][j] == 'S' && W[i+2][j+2] == 'S' {
				xmasCount++
			} else if W[i][j] == 'M' && W[i][j+2] == 'S' && W[i+2][j] == 'M' && W[i+2][j+2] == 'S' {
				xmasCount++
			} else if W[i][j] == 'S' && W[i][j+2] == 'S' && W[i+2][j] == 'M' && W[i+2][j+2] == 'M' {
				xmasCount++
			} else if W[i][j] == 'S' && W[i][j+2] == 'M' && W[i+2][j] == 'S' && W[i+2][j+2] == 'M' {
				xmasCount++
			}
		}
	}

	fmt.Println(xmasCount)
}

func main() {
	file, _ := os.Open("input.txt")
	defer file.Close()
	scanner := bufio.NewScanner(file)

	var W [][]rune
	for scanner.Scan() {
		line := scanner.Text()
		W = append(W, []rune(line))
	}

	part1(W)
	part2(W)
}