Skip to content

Instantly share code, notes, and snippets.

@ohbarye
Last active January 20, 2021 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohbarye/396e542c9898837c859f66fb1501e9c5 to your computer and use it in GitHub Desktop.
Save ohbarye/396e542c9898837c859f66fb1501e9c5 to your computer and use it in GitHub Desktop.
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)
}
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