Last active
January 20, 2021 15:21
-
-
Save ohbarye/396e542c9898837c859f66fb1501e9c5 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"io/ioutil" | |
"strings" | |
) | |
func main() { | |
bytes, _ := ioutil.ReadFile("./input.txt") | |
input := strings.TrimRight(string(bytes), "\n") | |
pattern := make([][]string, 0, 0) | |
for i, row := range strings.Split(input, "\n") { | |
pattern = append(pattern, make([]string, 0, 0)) | |
for _, char := range row { | |
pattern[i] = append(pattern[i], string(char)) | |
} | |
} | |
encounter := 0 | |
x := 0 | |
for y := 0; y < len(pattern); y++ { | |
if pattern[y][x] == "#" { | |
encounter++ | |
} | |
x += 3 | |
if x >= len(pattern[y]) { | |
x -= len(pattern[y]) | |
} | |
} | |
println(encounter) | |
} |
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
package main | |
import ( | |
"io/ioutil" | |
"strings" | |
) | |
func main() { | |
bytes, _ := ioutil.ReadFile("./input.txt") | |
input := strings.TrimRight(string(bytes), "\n") | |
pattern := make([][]string, 0, 0) | |
for i, row := range strings.Split(input, "\n") { | |
pattern = append(pattern, make([]string, 0, 0)) | |
for _, char := range row { | |
pattern[i] = append(pattern[i], string(char)) | |
} | |
} | |
slopes := [][]int{ | |
{1, 1}, | |
{3, 1}, | |
{5, 1}, | |
{7, 1}, | |
{1, 2}, | |
} | |
r := 1 | |
for _, slope := range slopes { | |
r *= count(pattern, slope[0], slope[1]) | |
} | |
println(r) | |
} | |
func count(pattern [][]string, xp int, yp int) int { | |
encounter := 0 | |
x := 0 | |
for y := 0; y < len(pattern); y += yp { | |
if pattern[y][x] == "#" { | |
encounter++ | |
} | |
x += xp | |
if x >= len(pattern[y]) { | |
x -= len(pattern[y]) | |
} | |
} | |
return encounter | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment