Skip to content

Commit 80792ca

Browse files
author
Miloš Rackov
committed
Day 23 Go
1 parent 92d79fb commit 80792ca

File tree

10 files changed

+300
-10
lines changed

10 files changed

+300
-10
lines changed

Day23/Go/go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module AoC2022D23
2+
3+
go 1.19

Day23/Go/main.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Miloš Rackov 2022
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE or copy at
4+
// https://www.boost.org/LICENSE_1_0.txt)
5+
6+
package main
7+
8+
import (
9+
"AoC2022D23/part1"
10+
"AoC2022D23/part2"
11+
"fmt"
12+
"os"
13+
)
14+
15+
func main() {
16+
inputDay23, err := os.ReadFile("input.txt")
17+
if err != nil {
18+
panic(err)
19+
}
20+
21+
p1 := part1.Part1(string(inputDay23))
22+
fmt.Printf("Result for part 1: %d\n", p1)
23+
p2 := part2.Part2(string(inputDay23))
24+
fmt.Printf("Result for part 2: %d\n", p2)
25+
26+
}

Day23/Go/part1/part1.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) Miloš Rackov 2022
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE or copy at
4+
// https://www.boost.org/LICENSE_1_0.txt)
5+
6+
package part1
7+
8+
import (
9+
"image"
10+
"strings"
11+
)
12+
13+
func Part1(input string) int {
14+
grid := make(map[image.Point]bool)
15+
for y, s := range strings.Fields(input) {
16+
for x, r := range s {
17+
if r == '#' {
18+
grid[image.Point{x, y}] = true
19+
}
20+
}
21+
}
22+
sides := [][]image.Point{
23+
{{0, -1}, {1, -1}, {-1, -1}}, // S
24+
{{0, 1}, {1, 1}, {-1, 1}}, // N
25+
{{-1, 0}, {-1, -1}, {-1, 1}}, // W
26+
{{1, 0}, {1, -1}, {1, 1}}, // E
27+
}
28+
for i := 0; ; i++ {
29+
proposed := map[image.Point]image.Point{}
30+
count := map[image.Point]int{}
31+
32+
for p := range grid {
33+
neighbours := make([]int, len(sides))
34+
for i := range sides {
35+
for _, q := range sides[i] {
36+
if grid[p.Add(q)] {
37+
neighbours[i]++
38+
}
39+
}
40+
}
41+
42+
if neighbours[0]+neighbours[1]+neighbours[2]+neighbours[3] == 0 {
43+
continue
44+
}
45+
46+
for d := 0; d < 4; d++ {
47+
if direction := (i + d) % 4; neighbours[direction] == 0 {
48+
proposed[p] = p.Add(sides[direction][0])
49+
count[proposed[p]]++
50+
break
51+
}
52+
}
53+
}
54+
55+
newGrid := make(map[image.Point]bool)
56+
for p := range grid {
57+
if _, ok := proposed[p]; ok && count[proposed[p]] == 1 {
58+
newGrid[proposed[p]] = true
59+
} else {
60+
newGrid[p] = true
61+
}
62+
}
63+
64+
if i == 9 {
65+
var r image.Rectangle
66+
for p := range newGrid {
67+
r = r.Union(image.Rectangle{p, p.Add(image.Point{1, 1})})
68+
}
69+
return r.Dx()*r.Dy() - len(newGrid)
70+
}
71+
grid = newGrid
72+
}
73+
}

Day23/Go/part1/part1_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Miloš Rackov 2022
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE or copy at
4+
// https://www.boost.org/LICENSE_1_0.txt)
5+
6+
package part1
7+
8+
import (
9+
"os"
10+
"testing"
11+
)
12+
13+
func TestPart1(t *testing.T) {
14+
testInput, err := os.ReadFile("../testinput.txt")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
19+
type args struct {
20+
input string
21+
}
22+
tests := []struct {
23+
name string
24+
args args
25+
want int
26+
}{
27+
{
28+
name: "Provided input",
29+
args: args{
30+
input: string(testInput),
31+
},
32+
want: 110,
33+
},
34+
}
35+
for _, tt := range tests {
36+
t.Run(tt.name, func(t *testing.T) {
37+
if got := Part1(tt.args.input); got != tt.want {
38+
t.Errorf("Part1() = %v, want %v", got, tt.want)
39+
}
40+
})
41+
}
42+
}
43+
44+
func BenchmarkPart1(b *testing.B) {
45+
testInput, err := os.ReadFile("../input.txt")
46+
if err != nil {
47+
b.Fatal(err)
48+
}
49+
b.ResetTimer()
50+
51+
for i := 0; i < b.N; i++ {
52+
Part1(string(testInput))
53+
}
54+
}

Day23/Go/part2/part2.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) Miloš Rackov 2022
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE or copy at
4+
// https://www.boost.org/LICENSE_1_0.txt)
5+
6+
package part2
7+
8+
import (
9+
"image"
10+
"reflect"
11+
"strings"
12+
)
13+
14+
func Part2(input string) int {
15+
grid := make(map[image.Point]bool)
16+
for y, s := range strings.Fields(input) {
17+
for x, r := range s {
18+
if r == '#' {
19+
grid[image.Point{x, y}] = true
20+
}
21+
}
22+
}
23+
sides := [][]image.Point{
24+
{{0, -1}, {1, -1}, {-1, -1}}, // S
25+
{{0, 1}, {1, 1}, {-1, 1}}, // N
26+
{{-1, 0}, {-1, -1}, {-1, 1}}, // W
27+
{{1, 0}, {1, -1}, {1, 1}}, // E
28+
}
29+
for i := 0; ; i++ {
30+
proposed := map[image.Point]image.Point{}
31+
count := map[image.Point]int{}
32+
33+
for p := range grid {
34+
neighbours := make([]int, len(sides))
35+
for i := range sides {
36+
for _, q := range sides[i] {
37+
if grid[p.Add(q)] {
38+
neighbours[i]++
39+
}
40+
}
41+
}
42+
43+
if neighbours[0]+neighbours[1]+neighbours[2]+neighbours[3] == 0 {
44+
continue
45+
}
46+
47+
for d := 0; d < 4; d++ {
48+
if direction := (i + d) % 4; neighbours[direction] == 0 {
49+
proposed[p] = p.Add(sides[direction][0])
50+
count[proposed[p]]++
51+
break
52+
}
53+
}
54+
}
55+
56+
newGrid := make(map[image.Point]bool)
57+
for p := range grid {
58+
if _, ok := proposed[p]; ok && count[proposed[p]] == 1 {
59+
newGrid[proposed[p]] = true
60+
} else {
61+
newGrid[p] = true
62+
}
63+
}
64+
65+
if reflect.DeepEqual(grid, newGrid) {
66+
return i + 1
67+
}
68+
69+
grid = newGrid
70+
}
71+
}

Day23/Go/part2/part2_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Miloš Rackov 2022
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE or copy at
4+
// https://www.boost.org/LICENSE_1_0.txt)
5+
6+
package part2
7+
8+
import (
9+
"os"
10+
"testing"
11+
)
12+
13+
func TestPart2(t *testing.T) {
14+
testInput, err := os.ReadFile("../testinput.txt")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
19+
type args struct {
20+
input string
21+
}
22+
tests := []struct {
23+
name string
24+
args args
25+
want int
26+
}{
27+
{
28+
name: "Provided input",
29+
args: args{
30+
input: string(testInput),
31+
},
32+
want: 20,
33+
},
34+
}
35+
for _, tt := range tests {
36+
t.Run(tt.name, func(t *testing.T) {
37+
if got := Part2(tt.args.input); got != tt.want {
38+
t.Errorf("Part1() = %v, want %v", got, tt.want)
39+
}
40+
})
41+
}
42+
}
43+
44+
func BenchmarkPart2(b *testing.B) {
45+
testInput, err := os.ReadFile("../input.txt")
46+
if err != nil {
47+
b.Fatal(err)
48+
}
49+
b.ResetTimer()
50+
51+
for i := 0; i < b.N; i++ {
52+
Part2(string(testInput))
53+
}
54+
}

Day23/Go/testinput.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../testinput.txt

Day23/PowerShell/testinput.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../testinput.txt

Day23/testinput.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
....#..
2+
..###.#
3+
#...#.#
4+
.#...##
5+
#.###..
6+
##.#.##
7+
.#..#..

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
# Advent of Code 2022
22

3-
![day](https://img.shields.io/badge/day%20📅-21-blue)
4-
![stars](https://img.shields.io/badge/stars%20⭐-38x-yellow)
3+
![day](https://img.shields.io/badge/day%20📅-24-blue)
4+
![stars](https://img.shields.io/badge/stars%20⭐-40x-yellow)
55

66
[Advent of Code](https://adventofcode.com)
77

88
## Local Leaderboard JPMC
99

1010
|Order|Name|Stars|Local Score|
1111
|--:|:--|--:|--:|
12-
|1|andrewpickett|42|2491|
13-
|2|gschaefer3|42|2439|
14-
|3|dratcliff|40|2334|
15-
|_4_|_dpajkovic_|_38_|_2044_|
16-
|5|Igor Dvoeglazov|36|1631|
17-
|6|cmbeam|32|1573|
12+
|1|andrewpickett|46|2727|
13+
|2|gschaefer3|46|2679|
14+
|3|dratcliff|46|2678|
15+
|_4_|_dpajkovic_|_40_|_2157_|
16+
|5|cmbeam|38|1913|
17+
|6|Igor Dvoeglazov|40|1855|
1818
|7|evgeniya-sotirova|32|1561|
1919
|8|David Hanley|30|1540|
20-
|9|JKerwood|29|1378|
21-
|10|Mihajlo|29|1337|
20+
|9|Mihajlo|31|1444|
21+
|10|Ben Ferenchak|29|1392|
2222

0 commit comments

Comments
 (0)