diff --git a/2025/day07/input b/2025/day07/input new file mode 100644 index 0000000..4edbf16 --- /dev/null +++ b/2025/day07/input @@ -0,0 +1,142 @@ +......................................................................S...................................................................... +............................................................................................................................................. +......................................................................^...................................................................... +............................................................................................................................................. +.....................................................................^.^..................................................................... +............................................................................................................................................. +....................................................................^.^.^.................................................................... +............................................................................................................................................. +...................................................................^.^...^................................................................... +............................................................................................................................................. +..................................................................^...^.^.^.................................................................. +............................................................................................................................................. +.................................................................^.^...^.^.^................................................................. +............................................................................................................................................. +................................................................^...^.^.^.^.^................................................................ +............................................................................................................................................. +...............................................................^.............^............................................................... +............................................................................................................................................. +..............................................................^...^.^.^...^.^.^.............................................................. +............................................................................................................................................. +.............................................................^.^.^...^.....^.^.^............................................................. +............................................................................................................................................. +............................................................^...^.^.^...^.^...^.^............................................................ +............................................................................................................................................. +...........................................................^.^...^.^...^.^.^...^.^........................................................... +............................................................................................................................................. +..........................................................^.^.^.^...^...^...^.....^.......................................................... +............................................................................................................................................. +.........................................................^.^.^.....^.^.^.^...^.....^......................................................... +............................................................................................................................................. +........................................................^.^.^.^.^.......^...^.^.^...^........................................................ +............................................................................................................................................. +.......................................................^.^.^.^.^.^.^.^...^.^.^.^.^.^.^....................................................... +............................................................................................................................................. +......................................................^.....^...^.^.^...........^...^.^...................................................... +............................................................................................................................................. +.....................................................^...^.^.^.^...^.....^.^.^...^...^.^..................................................... +............................................................................................................................................. +....................................................^.....^.^.^.^.....^.^.^.^.^.^...^.^.^.................................................... +............................................................................................................................................. +...................................................^...^.^.^.^.^.^.^...^.^.^.^...^.^.^.^.^................................................... +............................................................................................................................................. +..................................................^.^.^.^.^...^.^...^.^...^.^.^.^.^.^...^.^.................................................. +............................................................................................................................................. +.................................................^...^.....^...^.^.^.^...^.....^.^...^.^...^................................................. +............................................................................................................................................. +................................................^.^.^.....^.^.^.^.....^.^.^.^...^.....^.^.^.^................................................ +............................................................................................................................................. +...............................................^.....^.......^.....^...^.^...^.^.^...^.......^............................................... +............................................................................................................................................. +..............................................^.^...^...^.^...^...^...^.^...^.^.^.^...^...^.^.^.............................................. +............................................................................................................................................. +.............................................^...^...^.^.........^.^...^.^.....^.^.^.^.^.^...^.^............................................. +............................................................................................................................................. +............................................^.^.^...^.^.^.^.^...^.^.^...^.^...^.^...^.^.^.^.^.^.^............................................ +............................................................................................................................................. +...........................................^.....^.........^.^.^.^.^.^.^...^.^.^...^.^.^.^...^...^........................................... +............................................................................................................................................. +..........................................^.^.^.....^.......^.^...^.^.^.^.^.^.....^...^.^.^.^.^...^.......................................... +............................................................................................................................................. +.........................................^.^.^.^.^...^.^.....^.....^.^.^.^.^.^.^...^...^.^.^.^.^.^.^......................................... +............................................................................................................................................. +........................................^.^.^...^...^.^.^.^.^.^.^.^...^...^...^.^...^.^...^...^.....^........................................ +............................................................................................................................................. +.......................................^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.....^.^.^.^.^.^.^.........^.^....................................... +............................................................................................................................................. +......................................^.^.^.^.^.^.^.^.^.^.^...^.....^.......^.^.^.^.^.^.^.^...^...^...^...................................... +............................................................................................................................................. +.....................................^.^.^.^.^...^...^.^.......^...^.^.....^...^.^.^.^.^.....^.^.^.^.^.^..................................... +............................................................................................................................................. +....................................^.....^.^...^...^.^.^.^.^.^...^.^.^...^.^.^.^.....^...^.^.^.^.^.^...^.................................... +............................................................................................................................................. +...................................^.....^.^.........^...^.^.^.^.^.^.^...^.^.^...^.^.^.^.^...^.^...^.^...^................................... +............................................................................................................................................. +..................................^.^.^.^...^.^.^.^.^...^.^...^.^.^.^.^.....^.^.^.^.^.....^...^.....^.^.^.^.................................. +............................................................................................................................................. +.................................^.^.........^...^.....^.^...^.^...^.^.^...^.^...^...^.^...^.^.^.^.^.....^.^................................. +............................................................................................................................................. +................................^.^...^.^.^.^.^...^...^...............^.....^.....^.^.^.^.^.^.^.^.^.^.....^.^................................ +............................................................................................................................................. +...............................^.^.^...^.^...^.^.^...^...^.^.....^.^.^...^.^.^.^...^.^.^.^.^.^.^.^.^...^.^.^.^............................... +............................................................................................................................................. +..............................^...^.^.^.^.^.^.....^.^.^.^.....^.......^.^.^.^.^...^.^.^.^...^...^...^.^.^.....^.............................. +............................................................................................................................................. +.............................^.^.^...^.^.^.^.^.^...^...^.^.^.^.^.^...^.^.^.^.^.......^.^.^.^...^.^.^.^...^.^...^............................. +............................................................................................................................................. +............................^.....^.^...^.^...^.^.....^.^...^.......^...^.^.....^...^.^...^...^.^.^.....^.^.....^............................ +............................................................................................................................................. +...........................^...^...^...^.^.......^.....^.^.....^.^.^.^.^...^.^.^.^.....^.^.^...^.^.^.....^...^...^........................... +............................................................................................................................................. +..........................^.....^.^.^.......^.^...^.^.^.^.^.^.^.^.^.^.....^.....^.^...^.^...^.^.^...^...^.^.^.^...^.......................... +............................................................................................................................................. +.........................^.^.^.^.^.^.^.^.^.^...^.....^.^...^.^.^.^.^.^.^.^.^...^.^.^.^.^.^...^.^.^...^.^...^.^.^.^.^......................... +............................................................................................................................................. +........................^.^.^...^.^.^...^.^...^.........^.^.^...^.^.^.^.....^.^...^.^.^.^.^...^.^.^...^.^.....^.....^........................ +............................................................................................................................................. +.......................^.^.^.^...^.^.....^...^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.....^.......^.^.^.^...^.^...^....................... +............................................................................................................................................. +......................^.^...^.^.^.^.....^.^.^.^.^.....^.^...^.^.^.^.^.^.^...^...^.^.^.^...^.....^.^...^.^.^.^.^.^.^.^.^...................... +............................................................................................................................................. +.....................^.^.^.^.^...^.^...^...^.^...^.^...^.^...^.......^.^.^.^.....^.^...^.^.^.^...^.^.^.^.^.^.^.^...^.^.^..................... +............................................................................................................................................. +....................^...^.^.^.....^.^.^.^.^.^.^.^.^.^.^.^.^...^...^.^.^.^.......^...^.....^.^...^...^.^.....^.^.^...^.^.^.................... +............................................................................................................................................. +...................^.^.^.^.^.^.......^.^.^.......^...^.^.^.^...^.^.^...^.^.^.^...^.^.^.^.....^.^.^.^.^.^...^.^...^.....^.^................... +............................................................................................................................................. +..................^.^...^.......^.^.^.^.....^.^...^.^...^...^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^...^.^...^.^.^.^.^...^.^.^...^.................. +............................................................................................................................................. +.................^.....^.^...^...^.^.^.^...^.^.^.^...^.......^.^...^...^.^.^.....^.^.^...^.^...^.....^.^...^.^.^.^.^.^.^.^.^................. +............................................................................................................................................. +................^.^.^.....^.^.^.^.^.^.^.......^.^.^.....^...^.^...^.^.^.^.^...^.^...^.^.^...^...^.^.^.^...^.^.^.^...^.^.....^................ +............................................................................................................................................. +...............^...^.^.^.^.^.^...^.^...^...^.^.^.^.^.^.^...^...^.^...............^...^.^.^.^.....^.....^.^.^.^.....^.^...^.^.^............... +............................................................................................................................................. +..............^.^...^.^.^.........^.........^...^.^.^.....^.^...^.^.^.^.^.^...^...^.^...^.^.^.^.......^.^...^.....^.^.^.^.^.^.^.............. +............................................................................................................................................. +.............^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.^...^.^.^.^.....^.^.^.^...^.^.^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.^............. +............................................................................................................................................. +............^.^.^.^...^.^.^.^.^...^...^.....^.^.^.^.^.^.^.^.^.^.^.^.....^.^.^.^.^...^.^.^...^...^.....^.^...^.^.^.^.^.......^...^............ +............................................................................................................................................. +...........^.....^.^.^.^.....^.^...^.^.^.^.^.....^.^.^...^.^.^...^.^.....^.......^.^.^.....^.^.^.^...^.^.^.^.^.^.^.^.^.^...^...^.^........... +............................................................................................................................................. +..........^.^.^.^.........^.^.....^.......^.^...^.^.^.......^.......^.^.^.^.....^.^...^.^...^.^.^.^...^...^.^.^.^.^.^.^...^.^.^.^.^.......... +............................................................................................................................................. +.........^.^.^...^.^.........^...^.^...^.^.^.^.^.^.^.^.....^.^.^.^...^.^.^...^...^...^.^.^.^.^.^.^.^.^.^.^...^.^...^.^...^.^.^...^.^......... +............................................................................................................................................. +........^.^.^.^.^...^.^...^.^.^.^...^.^.^.^.^.....^...^...^.^...^.^...^.^.^.^...^...^.^...^.......^.^.^.^...^.....^...^.^.^.....^.^.^........ +............................................................................................................................................. +.......^.^.^...^.^.^.....^.^.^.^.....^.....^.^...^.^.^.....^.^...^...........^...^.^.^.^.....^...^.^.^...^.^.^.^.......^.^.^.^.^.....^....... +............................................................................................................................................. +......^.^...^.....^.^...^.....^...^...^.^.^.^.^.^.^.^.^.^.^.^.^.....^.^.^...^.^.^...^.^.^.^.^.^.^.^...^.....^.^.....^.^.^.^.^...^...^.^...... +............................................................................................................................................. +.....^.^.^.^.^.^...^.^.......^.^.^...^...^.^.^.^.^.^.^.....^.^.....^...^.....^.^.^.^...^.^.^.^.^.^.^...^.^.^.^.^.^.^.^...^...^.^.^...^.^..... +............................................................................................................................................. +....^.^.^.....^.^...^.^...^.^.......^...^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.....^.^...^...^.^.^.^.^.^.^.... +............................................................................................................................................. +...^.^.......^.^.....^.^.^.^.^...^.^...^.^.^.^.^.^.^.^.^.^.^...^...^.^.^.^.^.....^.^.^...^.^.^...^...^.^.^...^...^.^.^.^...^.^...^.^...^.^... +............................................................................................................................................. +..^.....^.^.^...^.........^.....^...^.^.^...^.^.....^...^.^.^.^.^.^...^.........^...^.^...^...^.^...^...^.......^.^.....^.^.......^.^.^...^.. +............................................................................................................................................. +.^.^...^.....^.^.^.^...^.^.....^.^.^.^.^.^.^.^...^...^.^.......^.^...^.^.....^.^.^...^.....^.^.^.^.^.^.......^.^.^...^.^.^.^.^...^...^.^.^.^. +............................................................................................................................................. diff --git a/2025/day07/main.go b/2025/day07/main.go new file mode 100644 index 0000000..2bf2052 --- /dev/null +++ b/2025/day07/main.go @@ -0,0 +1,138 @@ +package main + +import ( + "fmt" + "sync" + + h "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + inp := h.StdinToStringSlice() + part1(inp) + fmt.Println() + part2(inp) +} + +func part1(inp []string) { + var ret int + m := h.StringSliceToCoordByteMap(inp) + ok := true + for ok { + var splits int + splits, ok = progBeams(&m) + ret += splits + } + fmt.Println("# Part 1") + fmt.Println(ret) +} + +func progBeams(m *h.CoordByteMap) (int, bool) { + var splits int + beam, err := m.FindLast('|') + if err != nil { + beam, _ = m.FindFirst('S') + } + if beam.Y == m.BRY { + return 0, false + } + beams := m.FindAllOnRow(beam.Y, '|') + if len(beams) == 0 { + beams = append(beams, beam) + } + + for i := range beams { + nxt := m.Get(beams[i].South()) + switch nxt { + case '.': + m.Put(beams[i].South(), '|') + case '^': + splits++ + m.Put(beams[i].South().East(), '|') + m.Put(beams[i].South().West(), '|') + } + } + return splits, true +} + +func part2(inp []string) { + m := h.StringSliceToCoordByteMap(inp) + fmt.Println("# Part 2") + fmt.Println(quantumRevKickoff(&m)) +} + +// quantumRevKickoff starts checks for each potential beam output +func quantumRevKickoff(m *h.CoordByteMap) int { + var ret int + var wg sync.WaitGroup + status := make([]byte, m.BRX) + for i := range status { + status[i] = '.' + } + track := &WorldTracker{ + t: make(map[h.Coordinate]int), + } + for x := m.TLX; x < m.BRX; x++ { + c := h.Coordinate{X: x, Y: m.BRY} + wg.Go(func() { + if r, v := quantumRevCalc(m, c, track); v { + ret += r + status[c.X] = 'O' + } else { + status[c.X] = 'X' + } + }) + } + wg.Wait() + return ret + 1 +} + +type WorldTracker struct { + t map[h.Coordinate]int + lock sync.Mutex +} + +func (t *WorldTracker) Get(k h.Coordinate) (int, bool) { + t.lock.Lock() + defer t.lock.Unlock() + v, ok := t.t[k] + return v, ok +} +func (t *WorldTracker) Set(k h.Coordinate, v int) { + t.lock.Lock() + defer t.lock.Unlock() + t.t[k] = v +} + +// quantumRevCalc calculates how many worlds would provide a beam at +// the given coordinate +func quantumRevCalc(m *h.CoordByteMap, c h.Coordinate, track *WorldTracker) (int, bool) { + if v, ok := track.Get(c); ok { + return v, ok + } + var ret int + var valid bool + if m.Get(c.East()) == '^' { + if r, v := quantumRevCalc(m, c.East(), track); v { + ret += r + valid = true + } + } + if m.Get(c.West()) == '^' { + if r, v := quantumRevCalc(m, c.West(), track); v { + ret += r + valid = true + } + } + if m.Get(c.North()) == '.' { + if r, v := quantumRevCalc(m, c.North(), track); v { + ret += r + valid = true + } + } + track.Set(c, ret) + if m.Get(c.North()) == 'S' { + return 1, true + } + return ret, valid +} diff --git a/2025/day07/problem b/2025/day07/problem new file mode 100644 index 0000000..8a2547e --- /dev/null +++ b/2025/day07/problem @@ -0,0 +1,274 @@ +Advent of Code + br0xen [7](AoC++) 14* + +--- Day 7: Laboratories --- + + You thank the cephalopods for the help and exit the trash compactor, + finding yourself in the [16]familiar [17]halls of a North Pole research + wing. + + Based on the large sign that says "teleporter hub", they seem to be + researching teleportation; you can't help but try it for yourself and step + onto the large yellow teleporter pad. + + Suddenly, you find yourself in an unfamiliar room! The room has no doors; + the only way out is the teleporter. Unfortunately, the teleporter seems to + be leaking [18]magic smoke. + + Since this is a teleporter lab, there are lots of spare parts, manuals, + and diagnostic equipment lying around. After connecting one of the + diagnostic tools, it helpfully displays error code 0H-N0, which apparently + means that there's an issue with one of the tachyon manifolds. + + You quickly locate a diagram of the tachyon manifold (your puzzle input). + A tachyon beam enters the manifold at the location marked S; tachyon beams + always move downward. Tachyon beams pass freely through empty space (.). + However, if a tachyon beam encounters a splitter (^), the beam is stopped; + instead, a new tachyon beam continues from the immediate left and from the + immediate right of the splitter. + + For example: + + .......S....... + ............... + .......^....... + ............... + ......^.^...... + ............... + .....^.^.^..... + ............... + ....^.^...^.... + ............... + ...^.^...^.^... + ............... + ..^...^.....^.. + ............... + .^.^.^.^.^...^. + ............... + + In this example, the incoming tachyon beam (|) extends downward from S + until it reaches the first splitter: + + .......S....... + .......|....... + .......^....... + ............... + ......^.^...... + ............... + .....^.^.^..... + ............... + ....^.^...^.... + ............... + ...^.^...^.^... + ............... + ..^...^.....^.. + ............... + .^.^.^.^.^...^. + ............... + + At that point, the original beam stops, and two new beams are emitted from + the splitter: + + .......S....... + .......|....... + ......|^|...... + ............... + ......^.^...... + ............... + .....^.^.^..... + ............... + ....^.^...^.... + ............... + ...^.^...^.^... + ............... + ..^...^.....^.. + ............... + .^.^.^.^.^...^. + ............... + + Those beams continue downward until they reach more splitters: + + .......S....... + .......|....... + ......|^|...... + ......|.|...... + ......^.^...... + ............... + .....^.^.^..... + ............... + ....^.^...^.... + ............... + ...^.^...^.^... + ............... + ..^...^.....^.. + ............... + .^.^.^.^.^...^. + ............... + + At this point, the two splitters create a total of only three tachyon + beams, since they are both dumping tachyons into the same place between + them: + + .......S....... + .......|....... + ......|^|...... + ......|.|...... + .....|^|^|..... + ............... + .....^.^.^..... + ............... + ....^.^...^.... + ............... + ...^.^...^.^... + ............... + ..^...^.....^.. + ............... + .^.^.^.^.^...^. + ............... + + This process continues until all of the tachyon beams reach a splitter or + exit the manifold: + + .......S....... + .......|....... + ......|^|...... + ......|.|...... + .....|^|^|..... + .....|.|.|..... + ....|^|^|^|.... + ....|.|.|.|.... + ...|^|^|||^|... + ...|.|.|||.|... + ..|^|^|||^|^|.. + ..|.|.|||.|.|.. + .|^|||^||.||^|. + .|.|||.||.||.|. + |^|^|^|^|^|||^| + |.|.|.|.|.|||.| + + To repair the teleporter, you first need to understand the beam-splitting + properties of the tachyon manifold. In this example, a tachyon beam is + split a total of 21 times. + + Analyze your manifold diagram. How many times will the beam be split? + + Your puzzle answer was 1600. + +--- Part Two --- + + With your analysis of the manifold complete, you begin fixing the + teleporter. However, as you open the side of the teleporter to replace the + broken manifold, you are surprised to discover that it isn't a classical + tachyon manifold - it's a quantum tachyon manifold. + + With a quantum tachyon manifold, only a single tachyon particle is sent + through the manifold. A tachyon particle takes both the left and right + path of each splitter encountered. + + Since this is impossible, the manual recommends the many-worlds + interpretation of quantum tachyon splitting: each time a particle reaches + a splitter, it's actually time itself which splits. In one timeline, the + particle went left, and in the other timeline, the particle went right. + + To fix the manifold, what you really need to know is the number of + timelines active after a single particle completes all of its possible + journeys through the manifold. + + In the above example, there are many timelines. For instance, there's the + timeline where the particle always went left: + + .......S....... + .......|....... + ......|^....... + ......|........ + .....|^.^...... + .....|......... + ....|^.^.^..... + ....|.......... + ...|^.^...^.... + ...|........... + ..|^.^...^.^... + ..|............ + .|^...^.....^.. + .|............. + |^.^.^.^.^...^. + |.............. + + Or, there's the timeline where the particle alternated going left and + right at each splitter: + + .......S....... + .......|....... + ......|^....... + ......|........ + ......^|^...... + .......|....... + .....^|^.^..... + ......|........ + ....^.^|..^.... + .......|....... + ...^.^.|.^.^... + .......|....... + ..^...^|....^.. + .......|....... + .^.^.^|^.^...^. + ......|........ + + Or, there's the timeline where the particle ends up at the same point as + the alternating timeline, but takes a totally different path to get there: + + .......S....... + .......|....... + ......|^....... + ......|........ + .....|^.^...... + .....|......... + ....|^.^.^..... + ....|.......... + ....^|^...^.... + .....|......... + ...^.^|..^.^... + ......|........ + ..^..|^.....^.. + .....|......... + .^.^.^|^.^...^. + ......|........ + + In this example, in total, the particle ends up on 40 different timelines. + + Apply the many-worlds interpretation of quantum tachyon splitting to your + manifold diagram. In total, how many different timelines would a single + tachyon particle end up on? + + Your puzzle answer was 8632253783011. + + Both parts of this puzzle are complete! They provide two gold stars: ** + + At this point, you should [19]return to your Advent calendar and try + another puzzle. + + If you still want to see it, you can [20]get your puzzle input. + +References + + Visible links + 1. https://adventofcode.com/ + 2. https://adventofcode.com/2025/about + 3. https://adventofcode.com/2025/events + 4. https://adventofcode.com/2025/shop + 5. https://adventofcode.com/2025/settings + 6. https://adventofcode.com/2025/auth/logout + 7. Advent of Code Supporter + https://adventofcode.com/2025/support + 8. https://adventofcode.com/2025 + 9. https://adventofcode.com/2025 + 10. https://adventofcode.com/2025/support + 11. https://adventofcode.com/2025/sponsors + 12. https://adventofcode.com/2025/leaderboard/private + 13. https://adventofcode.com/2025/stats + 14. https://adventofcode.com/2025/sponsors + 16. https://adventofcode.com/2024/day/6 + 17. https://adventofcode.com/2018/day/4 + 18. https://en.wikipedia.org/wiki/Magic_smoke + 19. https://adventofcode.com/2025 + 20. https://adventofcode.com/2025/day/7/input diff --git a/2025/day07/testinput b/2025/day07/testinput new file mode 100644 index 0000000..db77af0 --- /dev/null +++ b/2025/day07/testinput @@ -0,0 +1,16 @@ + .......S....... + ............... + .......^....... + ............... + ......^.^...... + ............... + .....^.^.^..... + ............... + ....^.^...^.... + ............... + ...^.^...^.^... + ............... + ..^...^.....^.. + ............... + .^.^.^.^.^...^. + ............... diff --git a/go.mod b/go.mod index 3af9a93..901a79d 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module git.bullercodeworks.com/brian/adventofcode -go 1.23.0 +go 1.25.1 require ( github.com/br0xen/termbox-screen v0.0.0-20190712162752-c91f70ac38c6 diff --git a/helpers/coordinateByteMap.go b/helpers/coordinateByteMap.go index e61e924..e0ba5f3 100644 --- a/helpers/coordinateByteMap.go +++ b/helpers/coordinateByteMap.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "math" + "slices" ) type CoordByteMap struct { @@ -93,14 +94,14 @@ func (m *CoordByteMap) GetCol(x int) []byte { func (m *CoordByteMap) AddRow(row []byte) { y := m.BRY + 1 - for x := 0; x < len(row); x++ { + for x := range row { m.Put(Coordinate{X: x + m.TLX, Y: y}, row[x]) } } func (m *CoordByteMap) AddCol(col []byte) { x := m.BRX + 1 - for y := 0; y < len(col); y++ { + for y := range col { m.Put(Coordinate{X: x, Y: y + m.TLY}, col[y]) } } @@ -308,20 +309,29 @@ func (m *CoordByteMap) FindAll(b ...byte) []Coordinate { return ret } +func (m *CoordByteMap) FindAllOnRow(row int, b ...byte) []Coordinate { + var ret []Coordinate + if row < m.TLY || row > m.BRX { + return ret + } + for x := m.TLX; x <= m.BRX; x++ { + c := Coordinate{X: x, Y: row} + for i := range b { + if m.Get(c) == b[i] { + ret = append(ret, c) + } + } + } + return ret +} + func (m *CoordByteMap) FindAllNot(b ...byte) map[Coordinate]byte { ret := make(map[Coordinate]byte) for y := m.TLY; y <= m.BRY; y++ { for x := m.TLX; x <= m.BRX; x++ { c := Coordinate{X: x, Y: y} v := m.Get(c) - var is bool - for i := range b { - if v == b[i] { - is = true - break - } - } - if !is { + if !slices.Contains(b, v) { ret[c] = v } } diff --git a/helpers/helpers.go b/helpers/helpers.go index 0ed477c..03869df 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -3,10 +3,10 @@ package aoc import ( "bufio" "fmt" - "io/ioutil" "log" "math" "os" + "slices" "strconv" "strings" ) @@ -65,7 +65,7 @@ func Gcd(x, y int) int { // Lcm finds the least common multiple, using gcd func Lcm(a, b int, integers ...int) int { result := a * b / Gcd(a, b) - for i := 0; i < len(integers); i++ { + for i := range integers { result = Lcm(result, integers[i]) } return result @@ -92,8 +92,8 @@ func ArgIsSet(a string) bool { // GetArgValue returns the argument with the asked for key or "" func GetArgValue(a string) string { for i := range os.Args { - if strings.HasPrefix(os.Args[i], a+"=") { - return strings.TrimPrefix(os.Args[i], a+"=") + if ret, ok := strings.CutPrefix(os.Args[i], a+"="); ok { + return ret } } return "" @@ -206,8 +206,7 @@ func FileToString(fn string) string { func FileToBytes(fn string) []byte { var c []byte var err error - c, err = ioutil.ReadFile(fn) - if err != nil { + if c, err = os.ReadFile(fn); err != nil { fmt.Println("Unable to read file: " + fn) os.Exit(1) } @@ -322,24 +321,16 @@ func StringSliceIndex(h []string, n string) int { // StringSliceContains takes a string slice and a string and return true // if the string is in the slice +// Created before we had a slices.Contains. Still here for old solutions func StringSliceContains(h []string, n string) bool { - for _, v := range h { - if v == n { - return true - } - } - return false + return slices.Contains(h, n) } // IntSliceContains takes an int slice and an int and return true // if the int is in the slice +// Created before we had a slices.Contains. Still here for old solutions func IntSliceContains(h []int, n int) bool { - for _, v := range h { - if v == n { - return true - } - } - return false + return slices.Contains(h, n) } // AppendString returns a slice of strings that are base+group[...] @@ -375,7 +366,7 @@ func IntersectStringSlice(list1, list2 []string) []string { // Unique removes all duplicates func UniqueStringSlice(list []string) []string { var ret []string - for k := 0; k < len(list); k++ { + for k := range list { var dupe bool for j := k; j < len(list); j++ { if list[k] == list[j] {