Advent of Code: Ceres Search


Day 4 assignment

2024-12-04-generated.png

“Looks like the Chief’s not here. Next!” One of The Historians pulls out a device and pushes the only button on it. After a brief flash, you recognize the interior of the Ceres monitoring station!

-- Day 3 - Advent of Code 2024

Solution in Java

Full source can be found: in GitHub

Part 1

For the first part of today I chose to re-use my CharGrid class. With this I can iterate of all of the columns and rows looking for the X in any spot.

For each of the matches I find the code looks at all the neighbours to find the letter M, and if that is found continues for the remainder A and S in the same direction.

 1public void part1() {  
 2    var counted = 0;  
 3    var grid = inputLoader.charGrid();  
 4  
 5    for (var start : grid.findChar('X')) {  
 6        var neighbours = start.allNeighbours();  
 7        for (var neighbour : neighbours) {  
 8            if (grid.at(neighbour) == 'M') {  
 9                var direction = new Vector(start, neighbour).direction();  
10                var aCheck = neighbour.translate(direction);  
11                var sCheck = aCheck.translate(direction);  
12  
13                if (grid.at(aCheck) == 'A' && grid.at(sCheck) == 'S') {  
14                    counted++;  
15                }  
16            }  
17        }  
18    }  
19  
20    validator.part1(counted);  
21}

Part 2

For part 2 the logic is changed to look for the MAS text in an X shape. Which simplifies to looking as I only have to locate all A characters and then start looking to the corners to find the letters M and opposite of it the S.

 1public void part2() {  
 2    var counted = 0;  
 3    var grid = inputLoader.charGrid();  
 4  
 5    for (var start : grid.findChar('A')) {  
 6        var tlbrValid = grid.at(start.left().up()) == 'M' && grid.at(start.right().down()) == 'S';  
 7        var brtlValid = grid.at(start.down().right()) == 'M' && grid.at(start.up().left()) == 'S';  
 8  
 9        var bltrValid = grid.at(start.left().down()) == 'M' && grid.at(start.right().up()) == 'S';  
10        var trblValid = grid.at(start.up().right()) == 'M' && grid.at(start.down().left()) == 'S';  
11  
12        if ((tlbrValid || brtlValid) && (bltrValid || trblValid)) {  
13            counted++;  
14        }  
15    }  
16  
17    validator.part2(counted);  
18}

See also