|
| 1 | +// [Day 7: Laboratories](https://adventofcode.com/2025/day/7) |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// Coord represents a position in the grid. |
| 14 | +type Coord struct { |
| 15 | + X, Y int |
| 16 | +} |
| 17 | + |
| 18 | +// Grid is a simple 2D grid of bytes. |
| 19 | +type Grid struct { |
| 20 | + data [][]byte |
| 21 | + width int |
| 22 | + height int |
| 23 | +} |
| 24 | + |
| 25 | +// Parse creates a grid from multiline input. |
| 26 | +func Parse(input []byte) *Grid { |
| 27 | + |
| 28 | + var data [][]byte |
| 29 | + start := 0 |
| 30 | + height := 0 |
| 31 | + width := 0 |
| 32 | + |
| 33 | + for i, b := range input { |
| 34 | + if b == '\n' { |
| 35 | + if i-start > width { |
| 36 | + width = i - start |
| 37 | + } |
| 38 | + height += 1 |
| 39 | + data = append(data, input[start:i]) |
| 40 | + start = i + 1 |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return &Grid{ |
| 45 | + data: data, |
| 46 | + width: width, |
| 47 | + height: height, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// At returns the cell value at (x, y). |
| 52 | +func (g *Grid) At(x, y int) byte { |
| 53 | + return g.data[y][x] |
| 54 | +} |
| 55 | + |
| 56 | +// Width returns grid width. |
| 57 | +func (g *Grid) Width() int { |
| 58 | + return g.width |
| 59 | +} |
| 60 | + |
| 61 | +// Height returns grid height. |
| 62 | +func (g *Grid) Height() int { |
| 63 | + return g.height |
| 64 | +} |
| 65 | + |
| 66 | +const ( |
| 67 | + START = byte('S') |
| 68 | + SPLITTER = byte('^') |
| 69 | +) |
| 70 | + |
| 71 | +// puzzle is the input data. |
| 72 | +type puzzle struct { |
| 73 | + grid *Grid // grid is the diagram of the tachyon manifold. |
| 74 | + start Coord // start is the position where the tachyon beam enters the manifold. |
| 75 | +} |
| 76 | + |
| 77 | +// newPuzzle reads the manifold diagram and start position from a multiline input. |
| 78 | +func newPuzzle(data []byte) *puzzle { |
| 79 | + grid := Parse(data) |
| 80 | + |
| 81 | + var start Coord |
| 82 | + found := false |
| 83 | + |
| 84 | + for y := 0; y < grid.Height(); y++ { |
| 85 | + for x := 0; x < grid.Width(); x++ { |
| 86 | + if grid.At(x, y) == START { |
| 87 | + start = Coord{X: x, Y: y} |
| 88 | + found = true |
| 89 | + break |
| 90 | + } |
| 91 | + } |
| 92 | + if found { |
| 93 | + break |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return &puzzle{grid: grid, start: start} |
| 98 | +} |
| 99 | + |
| 100 | +// part1 counts times the beam will be split. |
| 101 | +func (p *puzzle) part1() uint64 { |
| 102 | + var splits uint64 |
| 103 | + |
| 104 | + beams := map[int]struct{}{} |
| 105 | + beams[p.start.X] = struct{}{} |
| 106 | + |
| 107 | + for y := p.start.Y; y < p.grid.Height(); y++ { |
| 108 | + nextBeams := map[int]struct{}{} |
| 109 | + |
| 110 | + for x := range beams { |
| 111 | + if p.grid.At(x, y) == SPLITTER { |
| 112 | + splits++ |
| 113 | + |
| 114 | + if x > 0 { |
| 115 | + nextBeams[x-1] = struct{}{} |
| 116 | + } |
| 117 | + if x < p.grid.Width()-1 { |
| 118 | + nextBeams[x+1] = struct{}{} |
| 119 | + } |
| 120 | + } else { |
| 121 | + nextBeams[x] = struct{}{} |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + beams = nextBeams |
| 126 | + } |
| 127 | + |
| 128 | + return splits |
| 129 | +} |
| 130 | + |
| 131 | +// part2 count the timelines for a single tachyon particle. |
| 132 | +func (p *puzzle) part2() uint64 { |
| 133 | + |
| 134 | + timelines := map[int]uint64{} |
| 135 | + timelines[p.start.X] = 1 |
| 136 | + |
| 137 | + for y := p.start.Y; y < p.grid.Height(); y++ { |
| 138 | + nextTimelines := map[int]uint64{} |
| 139 | + |
| 140 | + for x, ways := range timelines { |
| 141 | + if p.grid.At(x, y) == SPLITTER { |
| 142 | + if x > 0 { |
| 143 | + nextTimelines[x-1] += ways |
| 144 | + } |
| 145 | + if x < p.grid.Width()-1 { |
| 146 | + nextTimelines[x+1] += ways |
| 147 | + } |
| 148 | + } else { |
| 149 | + nextTimelines[x] += ways |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + timelines = nextTimelines |
| 154 | + } |
| 155 | + |
| 156 | + var total uint64 |
| 157 | + for _, v := range timelines { |
| 158 | + total += v |
| 159 | + } |
| 160 | + |
| 161 | + return total |
| 162 | +} |
| 163 | + |
| 164 | +// solve solves parts one and two of the puzzle. |
| 165 | +func solve(data []byte) (uint64, uint64) { |
| 166 | + |
| 167 | + puzzle := newPuzzle(data) |
| 168 | + |
| 169 | + return puzzle.part1(), puzzle.part2() |
| 170 | +} |
| 171 | + |
| 172 | +func main() { |
| 173 | + filename := "input.txt" |
| 174 | + elapsed := false |
| 175 | + |
| 176 | + for i := 1; i < len(os.Args); i++ { |
| 177 | + arg := os.Args[i] |
| 178 | + if arg == "--elapsed" { |
| 179 | + elapsed = true |
| 180 | + } else if !strings.HasPrefix(arg, "-") { |
| 181 | + filename = arg |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + data, err := os.ReadFile(filename) |
| 186 | + if err != nil { |
| 187 | + log.Fatal(err) |
| 188 | + } |
| 189 | + |
| 190 | + if elapsed { |
| 191 | + start := time.Now() |
| 192 | + result1, result2 := solve(data) |
| 193 | + duration := time.Since(start) |
| 194 | + |
| 195 | + fmt.Println(result1) |
| 196 | + fmt.Println(result2) |
| 197 | + fmt.Printf("elapsed: %f ms\n", duration.Seconds()*1000.0) |
| 198 | + } else { |
| 199 | + result1, result2 := solve(data) |
| 200 | + |
| 201 | + fmt.Println(result1) |
| 202 | + fmt.Println(result2) |
| 203 | + } |
| 204 | +} |
0 commit comments