Skip to content

Commit

Permalink
2015 Day6 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jabrena committed Dec 30, 2024
1 parent e4b911a commit 77aabec
Show file tree
Hide file tree
Showing 7 changed files with 467 additions and 1 deletion.
19 changes: 19 additions & 0 deletions 2015/src/main/java/info/jab/aoc/day6/Day6.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package info.jab.aoc.day6;

import info.jab.aoc.Day;

/**
* https://adventofcode.com/2015/day/6
*/
public class Day6 implements Day<Integer> {

@Override
public Integer getPart1Result(String fileName) {
throw new UnsupportedOperationException("Not implemented");
}

@Override
public Integer getPart2Result(String fileName) {
throw new UnsupportedOperationException("Not implemented");
}
}
28 changes: 28 additions & 0 deletions 2015/src/main/java/info/jab/aoc/day6/LightController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package info.jab.aoc.day6;
public class LightController {
private LightGrid lightGrid;

public LightController() {
lightGrid = new LightGrid();
}

public void executeCommand(String command, int x1, int y1, int x2, int y2) {
switch (command.toLowerCase().trim()) {
case "turn on":
lightGrid.turnOn(x1, y1, x2, y2);
break;
case "turn off":
lightGrid.turnOff(x1, y1, x2, y2);
break;
case "toggle":
lightGrid.toggle(x1, y1, x2, y2);
break;
default:
throw new IllegalArgumentException("Comando no válido: " + command);
}
}

public int getTotalLightsOn() {
return lightGrid.getNumberOfLightsOn();
}
}
45 changes: 45 additions & 0 deletions 2015/src/main/java/info/jab/aoc/day6/LightGrid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package info.jab.aoc.day6;

public class LightGrid {
private boolean[][] grid;
private static final int GRID_SIZE = 1000;

public LightGrid() {
grid = new boolean[GRID_SIZE][GRID_SIZE];
// Inicialmente todas las luces están apagadas (false)
}

public void turnOn(int x1, int y1, int x2, int y2) {
for (int i = x1; i <= x2; i++) {
for (int j = y1; j <= y2; j++) {
grid[i][j] = true;
}
}
}

public void turnOff(int x1, int y1, int x2, int y2) {
for (int i = x1; i <= x2; i++) {
for (int j = y1; j <= y2; j++) {
grid[i][j] = false;
}
}
}

public void toggle(int x1, int y1, int x2, int y2) {
for (int i = x1; i <= x2; i++) {
for (int j = y1; j <= y2; j++) {
grid[i][j] = !grid[i][j];
}
}
}

public int getNumberOfLightsOn() {
int count = 0;
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = 0; j < GRID_SIZE; j++) {
if (grid[i][j]) count++;
}
}
return count;
}
}
33 changes: 33 additions & 0 deletions 2015/src/main/java/info/jab/aoc/day6/day6-part1.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Feature: Christmas Light Configuration
As a competitive resident
I want to follow Santa's light instructions
In order to win the holiday decoration contest

Background:
Given I have a 1000x1000 grid of lights
And all lights are initially turned off

Scenario Outline: Execute different lighting commands
When I execute the command "<command>" from <x1>,<y1> through <x2>,<y2>
Then I should see <result> lights turned on

Examples:
| command | x1 | y1 | x2 | y2 | result |
| turn on | 0 | 0 | 999 | 999 | 1000000 |
| toggle | 0 | 0 | 999 | 0 | 1000 |
| turn off | 499 | 499 | 500 | 500 | 0 |

Scenario: Process multiple instructions
When I execute the following commands:
| command | x1 | y1 | x2 | y2 |
| turn on | 0 | 0 | 999 | 999 |
| toggle | 0 | 0 | 999 | 0 |
| turn off | 499 | 499 | 500 | 500 |
Then the total number of lights turned on should be correct

Rules:
- Coordinates range from 0 to 999 in each direction
- Coordinates are inclusive
- "turn on" turns on all lights in the specified range
- "turn off" turns off all lights in the specified range
- "toggle" switches the state of all lights in the specified range
41 changes: 41 additions & 0 deletions 2015/src/test/java/info/jab/aoc/day6/Day5Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package info.jab.aoc.day6;

import static org.assertj.core.api.BDDAssertions.then;

import org.junit.jupiter.api.Test;

import com.putoet.utils.Timer;

class Day6Test {

@Test
void should_solve_day6_part1() {
Timer.run(() -> {
//Given
String fileName = "/day6/day6-input.txt";

//When
var day = new Day6();
var result = day.getPart1Result(fileName);

//Then
then(result).isEqualTo(238);
});
}

@Test
void should_solve_day6_part2() {
Timer.run(() -> {
//Given
String fileName = "/day6/day6-input.txt";

//When
var day = new Day6();
var result = day.getPart2Result(fileName);

//Then
then(result).isEqualTo(69);
});
}

}
Loading

0 comments on commit 77aabec

Please sign in to comment.