Last active
December 7, 2025 11:48
-
-
Save dkaraush/8c94e1cd5ea6834e391950f54f82238d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
| "bytes" | |
| "fmt" | |
| "os" | |
| ) | |
| type Beam struct { | |
| X int | |
| Y int | |
| timelines int | |
| } | |
| func findInMatrix(rows [][]byte, char byte) (int, int) { | |
| height := len(rows) | |
| width := len(rows[0]) | |
| for y := 0; y < height; y++ { | |
| for x := 0; x < width; x++ { | |
| if rows[y][x] == char { | |
| return x, y | |
| } | |
| } | |
| } | |
| return -1, -1 | |
| } | |
| func findInSlice(list []Beam, x int, y int) *Beam { | |
| for idx := range list { | |
| if list[idx].X == x && list[idx].Y == y { | |
| return &list[idx] | |
| } | |
| } | |
| return nil | |
| } | |
| func addOrMerge(list []Beam, x int, y int, timelines int) []Beam { | |
| if b := findInSlice(list, x, y); b != nil { | |
| b.timelines += timelines | |
| return list | |
| } | |
| return append(list, Beam{X: x, Y: y, timelines: timelines}) | |
| } | |
| func main() { | |
| filebytes, err := os.ReadFile("input.txt") | |
| if err != nil { | |
| panic(err) | |
| } | |
| rows := bytes.Split(filebytes, []byte("\n")) | |
| height := len(rows) | |
| width := len(rows[0]) | |
| sx, sy := findInMatrix(rows, 'S') | |
| splits := 0 | |
| timelines := 0 | |
| beams := []Beam{} | |
| beams = append(beams, Beam{sx, sy + 1, 1}) | |
| for len(beams) > 0 { | |
| nextBeams := []Beam{} | |
| for _, beam := range beams { | |
| nextY := beam.Y + 1 | |
| if nextY >= height { | |
| timelines += beam.timelines | |
| continue | |
| } | |
| switch rows[beam.Y+1][beam.X] { | |
| case '.': | |
| nextBeams = addOrMerge(nextBeams, beam.X, nextY, beam.timelines) | |
| case '^': | |
| if leftX := beam.X - 1; leftX >= 0 { | |
| nextBeams = addOrMerge(nextBeams, leftX, nextY, beam.timelines) | |
| } | |
| if rightX := beam.X + 1; rightX < width { | |
| nextBeams = addOrMerge(nextBeams, rightX, nextY, beam.timelines) | |
| } | |
| splits++ | |
| } | |
| } | |
| beams = nextBeams | |
| } | |
| fmt.Println(splits) | |
| fmt.Println(timelines) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment