diff --git a/d17/main.go b/d17/main.go index c8fc0ae..02314e4 100644 --- a/d17/main.go +++ b/d17/main.go @@ -53,6 +53,6 @@ func main() { }) } - util.TestRuns("15", reader, part1, part2) + util.TestRuns("17", reader, part1, part2) util.SolutionRuns("17", reader, part1, part2) } diff --git a/d18/input.txt b/d18/input.txt new file mode 100644 index 0000000..dc69240 --- /dev/null +++ b/d18/input.txt @@ -0,0 +1,100 @@ +#..####.##..#...#..#...#...###.#.#.#..#....#.##..#...##...#..#.....##..#####....#.##..##....##.#.... +.#..#..#..#.###...##..#.##.....#...#..##....#####.##............####.#..######..#.#.##.#...#..#...## +#.....##.##.##.#..##.#..###...#.#.#..##..###.####.####.#.####.#...##.#..###.........#.###...#....### +#.###..#######..##..#.....##.#.#.###.#.##..#.##..##.##.#.##...###.#...#.#####.#.##..#.#####..#.##### +#.##.##.###.##..###.#.##.##...##.#.#..##..###.########.#.####..####...#####...#..#...##....##.##.##. +..#.#.#.#..#.#.###....###...#...#.##..####.###.....#.####.###.###.#......#.#.###..#..#.#....#.#####. +...#.###.#....#.###...#.#.#...#...#.#####....#....#...#####..#..#.#..######..#.##.#.##.#..###.#...## +.###...#...#.#..#.#.####.#...#.....##...###.#....#..##.###....#.##....###..#.#####...###.#.##.####.. +#.#....##.#.....#####.#.##..#######.#.####..###.##.#####.##.#...###...#.#...###..#...#.#.###.###.### +...##.##.....##..#.##...#.#...#...#.#####.#...#.#.#.#####.##.#...#.#..##.##..#...#....####..###.###. +#..#....######...#...###.#....#####....#.#.#....#....#.#######.#####..#....#....#.##..#.##.###..#... +#####.#.######.#.#####.#..##..##..####..#....#...#######....##..##.#..###..###.###..###...#...###### +#...##..##...###....##..##.##..#.#.#.#....##.#.......###..###..###...###..##.##.##.#.#.#..#.#..#..#. +..###....##.###..#.#..########...###...##..#######....##..###..#####.##.#....###..##.##.##.#...##.#. +###..#.#..#.#.##.##...##.....#..###.#..##.##.#....##.#.######..##..#.#.##.###...#..####...#.#..#.### +.######....#..##..#.####.##..#.#..#.#..#....#..##.#..#.#...####..#....#.####.#.###.#...####.#...#.#. +#.######.##..###.###..#..###.#...#..#...#...###.##....#.#......#...#.##.#.###..#.#####.#.#..###..#.# +...#..#...####..###.########.....###.###.#..##.##....######..#..#.....#.##.##.#..##..#..##...#..#..# +#..#..##..#.#.########.##.#.####..#.#####.#.###.##....###..##..#.#.###..#.##..##.##.####...######.## +.######.###....#...##...#..#....##..#.#...###.######.##...#....##.##.#.#.##..#...###.###.#....#..##. +####.#.##..##.##.###...#.###.##..##....###..####.##..#.#.##..###.#..##...####...#..####.#.#..##...#. +.#.#..#.....##...#..#...#.#...#.#.##..#....#..#......#####.#######....#.#..#..###..##.#.########..## +.##.#..#..##..#..####.#...####...#...#..##.#..###.#..######..#.#...###.##...#..#####..##.#..##.#.##. +.###..##.##.##....###.###..#.#...##.#.#...#.#######.####..#..###.#######.#...#.#...#.##...#..####..# +##.########..#..#....#.###..##.##.#.##.#..#......####..##.##.#..####..#####..#.....#####.###..#.#.#. +.#..####..##.#.#..#####.##..#..#.#....#.#####.#####...######........##.##..##.#.#.###..#.#.#.#..##.# +.##..##..#.######..###....#.#.###.#........#..###..#.########.....#.##...#.....#..#...##...#..#.###. +##.##.#..####....####.#######.....#.#.#...#.######.#.....####.####...###..####.##.##....###..#..#... +#.#..####...#......#...###...##....##.#######..#.###.#...###.##.##...####..#.####..#......##..#####. +.#.#...##...#....#.####.##.....#....#.#.#######..###.#.....#.....####...##...#.#.##.####..##.###.#.# +####.#.#.####...#...####.#.....#.#######.#.......####......###..###.#...######..#.##.#.##..#..##..## +..##.###..#..####..####.......######.##..#.....##.##...##.##......#.###..###...#.##.#####.#.######.# +.###..####.###..#..#.......#.##...##...##.######.....#..####.#......#.#...#...#...###...#.#.##..#### +.####....##.##.#.....##.###.####.#.......#.......#.#..#.#.#.....###.#.#####.#..#.#.#####.#####.###.# +.##.#.###.#####..#..#....###.#.#.#..#..###..##..####..##.###....#..####.####.#..###.#..######.###### +####.#.....##..###....#.....#.##.#.##..##..########.#####..###.####....##.....######.#.#.##.......#. +#.#.##.....#.....##.###.#..#.##.##....#..##....##.#.###.##.#..#..##.##.###.#..##.###...##..###.##### +#.###.#.#.#.#.#.#.#...#..#.###..####.##...#..####.###....#..#..##.#....####..##.##....#.#.##.##....# +...######....#..####...#.#..#.#.#..#.##.#.#.......#..#......##..#...#..#..##...##.#...#.#.#...##.##. +.#####..#...####....#..###..##....#####..###.#.#...###..###.###..##...#......#...#...#.#.#...#.##..# +......#####.#...#.#.#.##..#.###..##..#.#...###..###....##..#####..#######.#..#.###....###...##.#..#. +..##.########.##..#....##.#...##.##.#.#..#.##..#.#.#.##....#.#.#.#.##....##....#....#####.##..#.##.# +####...#....##.#.###......##.##.#..##...#..#####..#.#....##..#####...#.#.##...#.####.####..##.###### +.##.###.##.#...#.#....###.#######...##...##..#..##.###.#.####..#..###......#.#.##.#.#....#..##...#.. +.#.###.#.###.###.#.##.#..#......####.##...#..##.#..####.....#...#.###.##.##.#..#.##..#.###......#..# +...##.####......#.#.#..###..#....###....#.##.#####..#..#..#...#.#.###...#.#.#.##....###.####..###.#. +##..#.#.#.#....####...#.##.###..####....#..#####.######..#.##.##..#####.#.....#.#...##.#.##.##.#.#.. +#..##.#.#.#.###.#.#.###...#.#...##..#..#.#.#.##..###...#..##.#..#.#.#..#.....#.######.#.###..###.#.. +....#.#.##.###.##...#.##.#....#..##.#..##...#...#.##.####...##..####.#.........#..##..#...#...##.#.. +.##.......##...###.##.#.##.###.##.#..#..#..####...#...#....#####...###..##..#..#..##...#....#..##### +..####..#...#...#..###....##.#.#####..#..#.....#......#...#.......##....####...##....##.##.#.#####.# +##.#.#.#..##..##..#.####.##.##.###.#...###.#....#.....#.###...#######..###.####.###.####.##...##.#.. +..#.#...##.#....#..#..##.####.....#.#.#...#..#..###.#..###.#####.#.#####.#.#.#.#.###.##.###..#....## +.###.#...#....###..#...####....####..#.##..#..##.###..#.#.#.#..#...###.#.#...#......#...#.##.##.#... +..####.####.##.#.##....#...##....#..#....#..###..#...#..###.#####.....#####..##.#.#.#.#.#.##.####... +...##.#.##.####..##.###..#.#.#.#.#.#.#..###...#.##..#.####.##...#.#.##......###..#...###....#.#.###. +##...##..#.#.##..#.#.#....#.####.......#.#.#######.#..#....#.###.#...###.##....###.#.#..#.#.##.####. +...##.......######.....##....#...#..#.##.###.#..#.##.###.#.###.#.#.#...#.#...##.##.##..#.##########. +###..#....#.#.....#....###.#...##.......##.#.#..#.#...########......###..##.#..#..####.##..####...#. +......##.###.#.###.....#..#...#.#......##....#....#........#..#...##.##.....#...##.##.........##.... +.##.##.#.#...#....######..##....##..##.#.#.##.#.##..##...#..###......##......#.#....#.#.#.......###. +.......#.##..##.#...#.##..#..#####.#..#.######.........###.#####.####.#...##...........##...##..#### +#......#.#..#...#...##..#.#.###.##.##.#.#..#.###.##.#..###..#.###..#...###.##..###..#...#..###...#.. +####.##..#####..####.#...#..#..###..##.#.#...#...#...#.##.####.##.###....###...#.#.#..####.######.## +.....#..####...#.#.#.####..####..##.###......#.....########.#...#.#..#..#...#.###..##.#####..###.### +.#######.#.##..###.#...###.#####............##.###...#.##.#.##..##.#.#..#.######..######..#..#..#### +...##..#.####...#..#.#.##.#....#.####..#..###.###..#.#...#....##.##.#......##..##..#.#.#.###..#..#.. +........#...#.##.#.#..#....####....#.##...###..####...###.#.#..######..###..##.#####.###.###.#.#...# +##......##.#..###.####.##.#.###.#.......#.##..####..#.###.##..##..##...##...#.###...#.#..#..#.#####. +##..#.#.....##.####.#..##.#.##.#.#...#...#.#...####.#.#.##...##....##.###..###.####.#...#.###..##### +.#####.####.####.####.#.##.##......###....###.####...###...#...#..#.##.#.#####.###..##.#..###...##.. +.#...#..##...##...#....#.#.#..##..#.##..#.###.#.###..###.#.#.###.#....#######.####.##..#..#...####.. +..##.##..#.##..#.#.###..#.##.########...####.#.###.##..#..###.###...##..##.#..#.######.##.#....###.# +##.#####.###.##.#.##.##.##.###..##..##..#.#.#.#.####..#......#.#.#.#.#.#.##...#####.####...#.#...#.# +.#..###..##.#####.#.##.#..##...##..##...#####.#.####..#...##.....######.#.#...##.#..#######.###.###. +#.#..##.#.#####.#.#.....###.###.#..##.#####....#.###.##.##.#.#..##..#.#....#######.###.#.#.....#.### +....###...#.###.####....###.....##....#####.##.###.###.##.##.##.#..###..######...####.#.#..####..#.. +###.....#..####..#.####..#..#...##.##..##.######.####.....#...##....#..#.##.#####..###.##.#.####...# +.##.##.#...#..####...##.##.###...#...#..#.#.#####.....####...#.#.#..#.####...####.#...###.#......### +###.##....#.#.#...#.###....####..##...##.##.##.#..#...####..#..#..##...#####.####.####...##.#..###.# +..####.....##..###.#.#.###.########..#...#.##..#.#.#.......#.##.#..#...####.##.#..#.######..#.#...#. +#.#.##.#.#.##.#....##......##......#######.#..#.##...##..#.#.###...#.#..#..###...#..###.....##.....# +..#.##.#.##.#.##..##.....#.#..#.#..#...##..#..#.#....###.#####....####.####..#####.##.###...#..###.# +#....#.###..#..########.###..#.#.#.##...##.#..##.###..#..#..#.#.##..###...###.#.##..#.##.#..#.#.#### +#.......#######......#...#...##.##...###.#....##.#..#....####.#.##.###...#.#####...##.###........##. +.##.####.....###.##......####.###.########..#.####..#.##.#.####.....#...#.##....#######.##..#......# +#.#.##.##....##..##.#.###..#.##.#..#..#.#..##.....###..###.##.##.####.##.#.#.##...####..#.#..##.#.#. +...##.#.#.#...###.#.......#.#.....#.#...##....##.##.##.####...#.#..#..#..#.#.##.#..#.#.#....###..#.# +....#.#.###.#####.##..###..##..#...#.##.#......##.####.#..####.#.##..####.#.#...##..#####..##.#.#... +..###.#.##..#....#..#.#.....##.#####..##....#.#...#.##..##.#.#..#...##.##..##..##....#...#..#..#..## +##.#.##.#...#.###.##.##.##.##..##.##...#..##.#..#######.#..#...#.#.##..#....##.#..####.###........#. +.##.#..#.....#####..##.#.#.#.#..###.#######.###.###....##....#.#.#.###....###.#..#.#....#.#..###...# +...###.#.#.###..#...#..###.######..##.#.#..#...####.#####.##..#..###...#..#..#..###..##.#.#...#.###. +#......#.#..#..##.##.#.##.#.###.#.##.#.#..#....#.##..#..##..##.#.#.#....##.###.###.####.#.#####...## +...#.##..#.######.......#.#.###.....#####....##.#.#.###........#.#.###.#.#########.##.##.#..##..#... +##..###..###....####.##.##..##.###....####..##...####.####..####..###.####..##.#...###.#####.##.##.# +###...##.#.#.#####..#..#####...##.#...#.#.###.#..##..###.##.#.#.....####.##.#..##.###.#...##.##...## +...#.#.##.##..##....#..#.#####.##.###..#.#.#........####.###.##....##....####..#.#....#.#.#.###..#.# +..#.#.#.#.###...#....##..######.##....#.#.##..###..#.#.###..#.##..#.#.###......#..#..#.####..#...##. +.....####.#.....###.#.##.#..##.#..###.#####.#..##...###.#..###..#..##....###.#..##.#..#.##.#..#...## \ No newline at end of file diff --git a/d18/main.go b/d18/main.go new file mode 100644 index 0000000..c33d8f8 --- /dev/null +++ b/d18/main.go @@ -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) +} diff --git a/d18/test-runs/0.txt b/d18/test-runs/0.txt new file mode 100644 index 0000000..51dc5ce --- /dev/null +++ b/d18/test-runs/0.txt @@ -0,0 +1,6 @@ +.#.#.# +...##. +#....# +..#... +#.#..# +####.. \ No newline at end of file diff --git a/util/matrix.go b/util/matrix.go new file mode 100644 index 0000000..25b8f0e --- /dev/null +++ b/util/matrix.go @@ -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 +}