Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5784313

Browse files
committed
Day 12 done. Part two gets a bit slow but its easy.
1 parent 2956b1b commit 5784313

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

‎2018/day12/input.txt‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
initial state: #.####...##..#....#####.##.......##.#..###.#####.###.##.###.###.#...#...##.#.##.#...#..#.##..##.#.##
2+
3+
.##.. => .
4+
..##. => #
5+
.#..# => #
6+
.#.#. => .
7+
..#.. => #
8+
###.. => #
9+
##..# => .
10+
##... => #
11+
#.### => #
12+
.##.# => #
13+
#.... => .
14+
###.# => .
15+
..... => .
16+
.#... => #
17+
....# => .
18+
#.#.. => .
19+
...#. => #
20+
#...# => .
21+
##.#. => .
22+
.#.## => #
23+
..#.# => #
24+
#.#.# => .
25+
.#### => .
26+
##### => .
27+
..### => .
28+
...## => .
29+
#..## => .
30+
#.##. => .
31+
#..#. => #
32+
.###. => #
33+
##.## => #
34+
####. => .

‎2018/day12/twelve.go‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
)
8+
9+
func main() {
10+
f, _ := os.ReadFile("input.txt")
11+
lines := strings.Split(strings.TrimSpace(string(f)), "\n")
12+
initialState := strings.TrimPrefix(strings.TrimSpace(lines[0]), "initial state: ")
13+
rules := createRules(lines[2:])
14+
15+
tunnel := make([]string, 500)
16+
applyInitialState(&tunnel, initialState)
17+
for range 20 {
18+
applyRules(&tunnel, rules)
19+
}
20+
fmt.Println("Part 1 | Sum: ", calculateSum(&tunnel))
21+
22+
partTwo(initialState, rules)
23+
}
24+
25+
func partTwo(initialState string, rules map[string]bool) {
26+
// The tunnel has to be up sized for the ammount of generations we're computing.
27+
tunnel := make([]string, 1500)
28+
applyInitialState(&tunnel, initialState)
29+
lastValue := 0
30+
for i := range 500 {
31+
temp := make([]string, len(tunnel))
32+
copy(temp, tunnel)
33+
for range i {
34+
applyRules(&temp, rules)
35+
}
36+
sum := calculateSum(&temp)
37+
//@NOTE: Debug purposes, uncomment following print statements.
38+
// fmt.Printf("Part 2 | For %d Generations, Sum is: %d\n", i, sum)
39+
// You'll see this value converges to 72 in my case.
40+
// fmt.Printf("Gen %d | Difference to the last generation: %d\n", i, sum-lastValue)
41+
lastValue = sum
42+
}
43+
// The fifty billion iterations, minus the ones we did above (499) times the constant.
44+
lastValue += ((50000000000 - 499) * (72))
45+
fmt.Printf("Part 2 | Answer: %d\n", lastValue)
46+
}
47+
48+
func calculateSum(tunnel *[]string) int {
49+
center := len(*tunnel) / 2
50+
sum := 0
51+
for i, value := range *tunnel {
52+
if value == "#" {
53+
sum += (i - center)
54+
}
55+
}
56+
return sum
57+
}
58+
59+
func applyRules(tunnel *[]string, rules map[string]bool) {
60+
str := strings.Builder{}
61+
// We cant write straight to the array, because we override the state of the adjacent positions while we iterate.
62+
// Instead we keep a note of the changes to apply after. Its better than to copy the initial state i think.
63+
changes := map[int]string{}
64+
65+
for i := 2; i < len(*tunnel)-2; i++ {
66+
// Write to the temporary string the 5 adjacent values of this position
67+
str.WriteString((*tunnel)[i-2])
68+
str.WriteString((*tunnel)[i-1])
69+
str.WriteString((*tunnel)[i])
70+
str.WriteString((*tunnel)[i+1])
71+
str.WriteString((*tunnel)[i+2])
72+
73+
// If this string matches any rule:
74+
if value, ok := rules[str.String()]; ok {
75+
if value {
76+
changes[i] = "#"
77+
} else {
78+
changes[i] = "."
79+
}
80+
} else {
81+
changes[i] = "."
82+
}
83+
// Reset the temp string
84+
str.Reset()
85+
}
86+
// Apply changes
87+
for key, value := range changes {
88+
(*tunnel)[key] = value
89+
}
90+
}
91+
92+
func applyInitialState(tunnel *[]string, initialState string) {
93+
center := len(*tunnel) / 2
94+
for i := range len(*tunnel) {
95+
(*tunnel)[i] = "."
96+
}
97+
for i, char := range initialState {
98+
if char == '#' {
99+
(*tunnel)[center+i] = "#"
100+
} else {
101+
(*tunnel)[center+i] = "."
102+
}
103+
}
104+
105+
}
106+
107+
func createRules(lines []string) map[string]bool {
108+
rules := map[string]bool{}
109+
for _, line := range lines {
110+
fields := strings.Fields(strings.TrimSpace(line))
111+
if fields[2] == "#" {
112+
rules[fields[0]] = true
113+
} else {
114+
rules[fields[0]] = false
115+
}
116+
}
117+
return rules
118+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /