-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
290 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#..####.##..#...#..#...#...###.#.#.#..#....#.##..#...##...#..#.....##..#####....#.##..##....##.#.... | ||
.#..#..#..#.###...##..#.##.....#...#..##....#####.##............####.#..######..#.#.##.#...#..#...## | ||
#.....##.##.##.#..##.#..###...#.#.#..##..###.####.####.#.####.#...##.#..###.........#.###...#....### | ||
#.###..#######..##..#.....##.#.#.###.#.##..#.##..##.##.#.##...###.#...#.#####.#.##..#.#####..#.##### | ||
#.##.##.###.##..###.#.##.##...##.#.#..##..###.########.#.####..####...#####...#..#...##....##.##.##. | ||
..#.#.#.#..#.#.###....###...#...#.##..####.###.....#.####.###.###.#......#.#.###..#..#.#....#.#####. | ||
...#.###.#....#.###...#.#.#...#...#.#####....#....#...#####..#..#.#..######..#.##.#.##.#..###.#...## | ||
.###...#...#.#..#.#.####.#...#.....##...###.#....#..##.###....#.##....###..#.#####...###.#.##.####.. | ||
#.#....##.#.....#####.#.##..#######.#.####..###.##.#####.##.#...###...#.#...###..#...#.#.###.###.### | ||
...##.##.....##..#.##...#.#...#...#.#####.#...#.#.#.#####.##.#...#.#..##.##..#...#....####..###.###. | ||
#..#....######...#...###.#....#####....#.#.#....#....#.#######.#####..#....#....#.##..#.##.###..#... | ||
#####.#.######.#.#####.#..##..##..####..#....#...#######....##..##.#..###..###.###..###...#...###### | ||
#...##..##...###....##..##.##..#.#.#.#....##.#.......###..###..###...###..##.##.##.#.#.#..#.#..#..#. | ||
..###....##.###..#.#..########...###...##..#######....##..###..#####.##.#....###..##.##.##.#...##.#. | ||
###..#.#..#.#.##.##...##.....#..###.#..##.##.#....##.#.######..##..#.#.##.###...#..####...#.#..#.### | ||
.######....#..##..#.####.##..#.#..#.#..#....#..##.#..#.#...####..#....#.####.#.###.#...####.#...#.#. | ||
#.######.##..###.###..#..###.#...#..#...#...###.##....#.#......#...#.##.#.###..#.#####.#.#..###..#.# | ||
...#..#...####..###.########.....###.###.#..##.##....######..#..#.....#.##.##.#..##..#..##...#..#..# | ||
#..#..##..#.#.########.##.#.####..#.#####.#.###.##....###..##..#.#.###..#.##..##.##.####...######.## | ||
.######.###....#...##...#..#....##..#.#...###.######.##...#....##.##.#.#.##..#...###.###.#....#..##. | ||
####.#.##..##.##.###...#.###.##..##....###..####.##..#.#.##..###.#..##...####...#..####.#.#..##...#. | ||
.#.#..#.....##...#..#...#.#...#.#.##..#....#..#......#####.#######....#.#..#..###..##.#.########..## | ||
.##.#..#..##..#..####.#...####...#...#..##.#..###.#..######..#.#...###.##...#..#####..##.#..##.#.##. | ||
.###..##.##.##....###.###..#.#...##.#.#...#.#######.####..#..###.#######.#...#.#...#.##...#..####..# | ||
##.########..#..#....#.###..##.##.#.##.#..#......####..##.##.#..####..#####..#.....#####.###..#.#.#. | ||
.#..####..##.#.#..#####.##..#..#.#....#.#####.#####...######........##.##..##.#.#.###..#.#.#.#..##.# | ||
.##..##..#.######..###....#.#.###.#........#..###..#.########.....#.##...#.....#..#...##...#..#.###. | ||
##.##.#..####....####.#######.....#.#.#...#.######.#.....####.####...###..####.##.##....###..#..#... | ||
#.#..####...#......#...###...##....##.#######..#.###.#...###.##.##...####..#.####..#......##..#####. | ||
.#.#...##...#....#.####.##.....#....#.#.#######..###.#.....#.....####...##...#.#.##.####..##.###.#.# | ||
####.#.#.####...#...####.#.....#.#######.#.......####......###..###.#...######..#.##.#.##..#..##..## | ||
..##.###..#..####..####.......######.##..#.....##.##...##.##......#.###..###...#.##.#####.#.######.# | ||
.###..####.###..#..#.......#.##...##...##.######.....#..####.#......#.#...#...#...###...#.#.##..#### | ||
.####....##.##.#.....##.###.####.#.......#.......#.#..#.#.#.....###.#.#####.#..#.#.#####.#####.###.# | ||
.##.#.###.#####..#..#....###.#.#.#..#..###..##..####..##.###....#..####.####.#..###.#..######.###### | ||
####.#.....##..###....#.....#.##.#.##..##..########.#####..###.####....##.....######.#.#.##.......#. | ||
#.#.##.....#.....##.###.#..#.##.##....#..##....##.#.###.##.#..#..##.##.###.#..##.###...##..###.##### | ||
#.###.#.#.#.#.#.#.#...#..#.###..####.##...#..####.###....#..#..##.#....####..##.##....#.#.##.##....# | ||
...######....#..####...#.#..#.#.#..#.##.#.#.......#..#......##..#...#..#..##...##.#...#.#.#...##.##. | ||
.#####..#...####....#..###..##....#####..###.#.#...###..###.###..##...#......#...#...#.#.#...#.##..# | ||
......#####.#...#.#.#.##..#.###..##..#.#...###..###....##..#####..#######.#..#.###....###...##.#..#. | ||
..##.########.##..#....##.#...##.##.#.#..#.##..#.#.#.##....#.#.#.#.##....##....#....#####.##..#.##.# | ||
####...#....##.#.###......##.##.#..##...#..#####..#.#....##..#####...#.#.##...#.####.####..##.###### | ||
.##.###.##.#...#.#....###.#######...##...##..#..##.###.#.####..#..###......#.#.##.#.#....#..##...#.. | ||
.#.###.#.###.###.#.##.#..#......####.##...#..##.#..####.....#...#.###.##.##.#..#.##..#.###......#..# | ||
...##.####......#.#.#..###..#....###....#.##.#####..#..#..#...#.#.###...#.#.#.##....###.####..###.#. | ||
##..#.#.#.#....####...#.##.###..####....#..#####.######..#.##.##..#####.#.....#.#...##.#.##.##.#.#.. | ||
#..##.#.#.#.###.#.#.###...#.#...##..#..#.#.#.##..###...#..##.#..#.#.#..#.....#.######.#.###..###.#.. | ||
....#.#.##.###.##...#.##.#....#..##.#..##...#...#.##.####...##..####.#.........#..##..#...#...##.#.. | ||
.##.......##...###.##.#.##.###.##.#..#..#..####...#...#....#####...###..##..#..#..##...#....#..##### | ||
..####..#...#...#..###....##.#.#####..#..#.....#......#...#.......##....####...##....##.##.#.#####.# | ||
##.#.#.#..##..##..#.####.##.##.###.#...###.#....#.....#.###...#######..###.####.###.####.##...##.#.. | ||
..#.#...##.#....#..#..##.####.....#.#.#...#..#..###.#..###.#####.#.#####.#.#.#.#.###.##.###..#....## | ||
.###.#...#....###..#...####....####..#.##..#..##.###..#.#.#.#..#...###.#.#...#......#...#.##.##.#... | ||
..####.####.##.#.##....#...##....#..#....#..###..#...#..###.#####.....#####..##.#.#.#.#.#.##.####... | ||
...##.#.##.####..##.###..#.#.#.#.#.#.#..###...#.##..#.####.##...#.#.##......###..#...###....#.#.###. | ||
##...##..#.#.##..#.#.#....#.####.......#.#.#######.#..#....#.###.#...###.##....###.#.#..#.#.##.####. | ||
...##.......######.....##....#...#..#.##.###.#..#.##.###.#.###.#.#.#...#.#...##.##.##..#.##########. | ||
###..#....#.#.....#....###.#...##.......##.#.#..#.#...########......###..##.#..#..####.##..####...#. | ||
......##.###.#.###.....#..#...#.#......##....#....#........#..#...##.##.....#...##.##.........##.... | ||
.##.##.#.#...#....######..##....##..##.#.#.##.#.##..##...#..###......##......#.#....#.#.#.......###. | ||
.......#.##..##.#...#.##..#..#####.#..#.######.........###.#####.####.#...##...........##...##..#### | ||
#......#.#..#...#...##..#.#.###.##.##.#.#..#.###.##.#..###..#.###..#...###.##..###..#...#..###...#.. | ||
####.##..#####..####.#...#..#..###..##.#.#...#...#...#.##.####.##.###....###...#.#.#..####.######.## | ||
.....#..####...#.#.#.####..####..##.###......#.....########.#...#.#..#..#...#.###..##.#####..###.### | ||
.#######.#.##..###.#...###.#####............##.###...#.##.#.##..##.#.#..#.######..######..#..#..#### | ||
...##..#.####...#..#.#.##.#....#.####..#..###.###..#.#...#....##.##.#......##..##..#.#.#.###..#..#.. | ||
........#...#.##.#.#..#....####....#.##...###..####...###.#.#..######..###..##.#####.###.###.#.#...# | ||
##......##.#..###.####.##.#.###.#.......#.##..####..#.###.##..##..##...##...#.###...#.#..#..#.#####. | ||
##..#.#.....##.####.#..##.#.##.#.#...#...#.#...####.#.#.##...##....##.###..###.####.#...#.###..##### | ||
.#####.####.####.####.#.##.##......###....###.####...###...#...#..#.##.#.#####.###..##.#..###...##.. | ||
.#...#..##...##...#....#.#.#..##..#.##..#.###.#.###..###.#.#.###.#....#######.####.##..#..#...####.. | ||
..##.##..#.##..#.#.###..#.##.########...####.#.###.##..#..###.###...##..##.#..#.######.##.#....###.# | ||
##.#####.###.##.#.##.##.##.###..##..##..#.#.#.#.####..#......#.#.#.#.#.#.##...#####.####...#.#...#.# | ||
.#..###..##.#####.#.##.#..##...##..##...#####.#.####..#...##.....######.#.#...##.#..#######.###.###. | ||
#.#..##.#.#####.#.#.....###.###.#..##.#####....#.###.##.##.#.#..##..#.#....#######.###.#.#.....#.### | ||
....###...#.###.####....###.....##....#####.##.###.###.##.##.##.#..###..######...####.#.#..####..#.. | ||
###.....#..####..#.####..#..#...##.##..##.######.####.....#...##....#..#.##.#####..###.##.#.####...# | ||
.##.##.#...#..####...##.##.###...#...#..#.#.#####.....####...#.#.#..#.####...####.#...###.#......### | ||
###.##....#.#.#...#.###....####..##...##.##.##.#..#...####..#..#..##...#####.####.####...##.#..###.# | ||
..####.....##..###.#.#.###.########..#...#.##..#.#.#.......#.##.#..#...####.##.#..#.######..#.#...#. | ||
#.#.##.#.#.##.#....##......##......#######.#..#.##...##..#.#.###...#.#..#..###...#..###.....##.....# | ||
..#.##.#.##.#.##..##.....#.#..#.#..#...##..#..#.#....###.#####....####.####..#####.##.###...#..###.# | ||
#....#.###..#..########.###..#.#.#.##...##.#..##.###..#..#..#.#.##..###...###.#.##..#.##.#..#.#.#### | ||
#.......#######......#...#...##.##...###.#....##.#..#....####.#.##.###...#.#####...##.###........##. | ||
.##.####.....###.##......####.###.########..#.####..#.##.#.####.....#...#.##....#######.##..#......# | ||
#.#.##.##....##..##.#.###..#.##.#..#..#.#..##.....###..###.##.##.####.##.#.#.##...####..#.#..##.#.#. | ||
...##.#.#.#...###.#.......#.#.....#.#...##....##.##.##.####...#.#..#..#..#.#.##.#..#.#.#....###..#.# | ||
....#.#.###.#####.##..###..##..#...#.##.#......##.####.#..####.#.##..####.#.#...##..#####..##.#.#... | ||
..###.#.##..#....#..#.#.....##.#####..##....#.#...#.##..##.#.#..#...##.##..##..##....#...#..#..#..## | ||
##.#.##.#...#.###.##.##.##.##..##.##...#..##.#..#######.#..#...#.#.##..#....##.#..####.###........#. | ||
.##.#..#.....#####..##.#.#.#.#..###.#######.###.###....##....#.#.#.###....###.#..#.#....#.#..###...# | ||
...###.#.#.###..#...#..###.######..##.#.#..#...####.#####.##..#..###...#..#..#..###..##.#.#...#.###. | ||
#......#.#..#..##.##.#.##.#.###.#.##.#.#..#....#.##..#..##..##.#.#.#....##.###.###.####.#.#####...## | ||
...#.##..#.######.......#.#.###.....#####....##.#.#.###........#.#.###.#.#########.##.##.#..##..#... | ||
##..###..###....####.##.##..##.###....####..##...####.####..####..###.####..##.#...###.#####.##.##.# | ||
###...##.#.#.#####..#..#####...##.#...#.#.###.#..##..###.##.#.#.....####.##.#..##.###.#...##.##...## | ||
...#.#.##.##..##....#..#.#####.##.###..#.#.#........####.###.##....##....####..#.#....#.#.#.###..#.# | ||
..#.#.#.#.###...#....##..######.##....#.#.##..###..#.#.###..#.##..#.#.###......#..#..#.####..#...##. | ||
.....####.#.....###.#.##.#..##.#..###.#####.#..##...###.#..###..#..##....###.#..##.#..#.##.#..#...## |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package main | ||
|
||
import ( | ||
"aoc2015/util" | ||
"strings" | ||
) | ||
|
||
func getEnabledNeighbors(neighbors []string) int { | ||
return len( | ||
util.ArrayFilter(neighbors, func(item string) bool { | ||
return item == "#" | ||
}), | ||
) | ||
} | ||
|
||
func getEnabledLights(lights [][]string) int { | ||
enabledLights := 0 | ||
for _, row := range lights { | ||
for _, cell := range row { | ||
if cell == "#" { | ||
enabledLights++ | ||
} | ||
} | ||
} | ||
|
||
return enabledLights | ||
} | ||
|
||
func part1(lights [][]string) int { | ||
steps := 100 | ||
lightsState := lights | ||
nextLightsState := util.CloneMatrix(lights) | ||
|
||
for i := range nextLightsState { | ||
nextLightsState[i] = make([]string, len(lightsState[0])) | ||
} | ||
|
||
for range steps { | ||
for x := 0; x < len(lights); x++ { | ||
for y := 0; y < len(lights[0]); y++ { | ||
neighbors := util.GetNeighbors(x, y, lightsState) | ||
enabledNeighbors := getEnabledNeighbors(neighbors) | ||
|
||
if lightsState[x][y] == "#" { | ||
if enabledNeighbors == 2 || enabledNeighbors == 3 { | ||
nextLightsState[x][y] = "#" | ||
} else { | ||
nextLightsState[x][y] = "." | ||
} | ||
} else { | ||
if enabledNeighbors == 3 { | ||
nextLightsState[x][y] = "#" | ||
} else { | ||
nextLightsState[x][y] = "." | ||
} | ||
} | ||
} | ||
} | ||
|
||
lightsState = util.CloneMatrix(nextLightsState) | ||
} | ||
|
||
return getEnabledLights(lightsState) | ||
} | ||
|
||
func part2(lights [][]string) int { | ||
steps := 100 | ||
|
||
lightsState := lights | ||
nextLightsState := util.CloneMatrix(lights) | ||
|
||
// Corner lights always on | ||
lightsState[0][0] = "#" | ||
lightsState[0][len(lightsState[0])-1] = "#" | ||
lightsState[len(lightsState)-1][0] = "#" | ||
lightsState[len(lightsState)-1][len(lightsState[0])-1] = "#" | ||
|
||
for i := range nextLightsState { | ||
nextLightsState[i] = make([]string, len(lightsState[0])) | ||
} | ||
|
||
for range steps { | ||
for x := 0; x < len(lights); x++ { | ||
for y := 0; y < len(lights[0]); y++ { | ||
if (x == 0 && y == 0) || (x == 0 && y == len(lightsState[0])-1) || (x == len(lightsState)-1 && y == 0) || (x == len(lightsState)-1 && y == len(lightsState[0])-1) { | ||
nextLightsState[x][y] = "#" | ||
|
||
continue | ||
} | ||
|
||
neighbors := util.GetNeighbors(x, y, lightsState) | ||
enabledNeighbors := getEnabledNeighbors(neighbors) | ||
|
||
if lightsState[x][y] == "#" { | ||
if enabledNeighbors == 2 || enabledNeighbors == 3 { | ||
nextLightsState[x][y] = "#" | ||
} else { | ||
nextLightsState[x][y] = "." | ||
} | ||
} else { | ||
if enabledNeighbors == 3 { | ||
nextLightsState[x][y] = "#" | ||
} else { | ||
nextLightsState[x][y] = "." | ||
} | ||
} | ||
} | ||
} | ||
|
||
lightsState = util.CloneMatrix(nextLightsState) | ||
} | ||
|
||
// 4 lights always on, we only fixed them at start of the algo | ||
return getEnabledLights(lightsState) | ||
} | ||
|
||
func main() { | ||
reader := func(name string) [][]string { | ||
lines := util.ReadLines(name) | ||
|
||
return util.ArrayMap(lines, func(line string) []string { | ||
return strings.Split(line, "") | ||
}) | ||
} | ||
|
||
util.TestRuns("18", reader, part1, part2) | ||
util.SolutionRuns("18", reader, part1, part2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.#.#.# | ||
...##. | ||
#....# | ||
..#... | ||
#.#..# | ||
####.. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package util | ||
|
||
func CloneMatrix[T any](matrix [][]T) [][]T { | ||
clonedMatrix := make([][]T, len(matrix)) | ||
for i := range clonedMatrix { | ||
clonedMatrix[i] = make([]T, len(matrix[0])) | ||
} | ||
|
||
for i := 0; i < len(matrix); i++ { | ||
copy(clonedMatrix[i], matrix[i]) | ||
} | ||
|
||
return clonedMatrix | ||
} | ||
|
||
func GetNeighbors[T any](x, y int, matrix [][]T) []T { | ||
neighbors := []T{} | ||
|
||
// Top row | ||
if y > 0 && x > 0 { | ||
neighbors = append(neighbors, matrix[x-1][y-1]) | ||
} | ||
|
||
if y > 0 { | ||
neighbors = append(neighbors, matrix[x][y-1]) | ||
} | ||
|
||
if y > 0 && x < len(matrix[0])-1 { | ||
neighbors = append(neighbors, matrix[x+1][y-1]) | ||
} | ||
|
||
// Middle row | ||
if x > 0 { | ||
neighbors = append(neighbors, matrix[x-1][y]) | ||
} | ||
|
||
if x < len(matrix[0])-1 { | ||
neighbors = append(neighbors, matrix[x+1][y]) | ||
} | ||
|
||
// Bottom row | ||
if y < len(matrix)-1 && x > 0 { | ||
neighbors = append(neighbors, matrix[x-1][y+1]) | ||
} | ||
|
||
if y < len(matrix)-1 { | ||
neighbors = append(neighbors, matrix[x][y+1]) | ||
} | ||
|
||
if y < len(matrix)-1 && x < len(matrix[0])-1 { | ||
neighbors = append(neighbors, matrix[x+1][y+1]) | ||
} | ||
|
||
return neighbors | ||
} |