chore: setup folders for aoc 2024
Some checks failed
Go Bump / bump (push) Failing after 9s
Go Test / build (push) Successful in 38s

This commit is contained in:
xuu
2024-10-26 11:38:38 -06:00
parent 50af2114d4
commit 3c9af95ec4
92 changed files with 72 additions and 1 deletions

10
aoc2023/day16/example.txt Normal file
View File

@@ -0,0 +1,10 @@
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....

110
aoc2023/day16/input.txt Normal file
View File

@@ -0,0 +1,110 @@
\.......................\..........................................-..................-.../................-..
....../.......\...-......-.|...\..............\.................|............././....-........................
...............-...-........................................\......\..-.............|......-....\....-........
..\..|.|...........................-.|........................................./.|....|................|......
|../................|............................................................\........../..\..............
........\............../................/..........................\...\....|............./....-.........\\...
.................-...-...................\../........-|.................../......-...........|................
........-............/...|-............-.....\.|..............-....-../\../..................\...\././........
........................-|................................................................-........-..........
-......./.................|.....\.................\.....//............-........./..../....|...................
....-.............-........-..........-|.......|...../...\...................\.....................|..........
...|...\.........|.................|.....|....-....\.......\./........................-.../..../.....-........
....|....-.-..............\.........|.......................-.......-..............\................/.........
.....|.\...........-.................-.-........../...\............|...............|...|......./.......|...../
.....\.........................-.../..-./...\...................../......\...-.......|.\........-|............
............|.....................................-....../....\.............../-...|...\..\................./.
......-./|....../............|.........\............-../...-......./.\..\....../......|/...\..................
................................|.......|..-..../.............................../.-..................-..-/..|.
...............................-......./......................|./...............-............./....../........
..................../....................\........\...........................-............/.../..............
.|||......../.................|.................../..\............................-......./..|...|/.-.........
......-......./\....../.........|....../.......-...............|.\........-............/.../..................
.....................-.\/|................//.../.........|.|......\.-..|.........|.........\..................
.........................\/.......|.|............................|........|.............................././..
.................................|.................\../..|/.................\.................-...-........-/.
......................-...-.........../...........................\....\......-..-.......\.-....\....|........
......-......../..............\/..................\..|........-..............|...-...../......|...............
|.......|.../....|..............//.....................|..........|/......../................|................
...|.|..................../|..../.-.......-...........|.....|./....\......-......-................|...........
......\/./.|\..../|.|..|..........-....|...........|.-.|...................-.\.....|....................\.....
......../.\......................|.....|...........|.........../.......|....|...|.......-......../......\...-.
...|...-...||......-..|..-...........|.-.................|.................................-..................
..............-.\.-........|......\..........................................\./.\........../.....|...........
../\.........|...............|............|.\/.-/.................-....|...-.................-.....|..../.....
....................../......../..../-.....\........................\..-.\....|....-.........../-.............
.................................|................|.........|................../|.....|....\\............/.\..
...///./.....\.................|/.....................\...................-............|................|.....
............-...........................--........\................/.............-\................\.....-.../
...............\.........../|............................|.....................\...-...\.....................-
...\.............|.............|................\............\|...../...|.........................|...........
..................|......|..........-.-..............\.........................\..............................
....-................../.........../...|................/...............|./.|.../....\..../......./......//...
...../....../..............................|...\../..........-..........-........-..//..../...................
...............-......|......................|./........./............/..-...........|../.....-........-./....
..\.........................|...-.......\.................|..............-........\....-.............-....|...
.......|......|....../........\...........|......-........../......-..................../....................\
.......................|....-..|........./.|.-...............|..\......\.....-.......\...-/....|/.........\...
\......\..-.....................\..........................\................................../...........\...
-....-......../............................................................../.\.......|..............|....-..
........|......................|.............-....\................................./..\.......|..........-...
...-...............|........./......\...../.......|...|...............\.../...\.../......|.....-.\......\.....
............./.....|......................|.....................................\...||.................-....|.
./.....-................/........|./....-......../......./..............\..................................\..
.../..../\........-..........................|.-/.|..\................\\-.......|......./...||.......|........
.....-.........................-...............\...........|.............-.\.../.|......-.|......../...-.....-
......\....................|...../......................|.../......-..|............|......./.../.........|....
...|...........-....................................../.|............./....|..../........./.....\../..........
....../.../................\........................\...\....\......\............../|...............\.-.......
./....\.../.............|............................-.../............../............/...........\...|........
............................|.................|/..........//.........../..-...............|...................
.....\......\.................|.....|...\-./...............\................|....../.....-..\........-....-...
........................./-........\................./........-/.|....-.......\............-/\......|.\...-...
..../../..|.|...............\.......................................-.........................................
...........|...................................................-...-......|..................\............../.
/....|......................................................................|\.../.......-...........-...|....
......|.-.\......-........|.....-..|.\..............-......./......-|.........................-...............
............\......................................|........................|.-...............................
.........-.........../....\./....../.-...-..//.........../.......\\...........................................
........\......../............./...-......\.../..|..|............................../......../..\..............
--.........|.....-...|............../...|.......|..............\|............|.........................|.-....
-\......./..............-....-....\...|.-........\/.\./............-/...\........|-.........../../...\........
.\|\..............-......-.\...-....\..........................\........|...........-......./.............../.
..........................|...\..../.../..........\.............-...//...............\..................-.....
........./..........................\............|-.....|..-...|......../........-...........................\
.......\.../.....\....../.......|..|...............-......................|....-...................-.|........
......|.......\.../..............................-..............................|......-.-...................\
/.....|.......-............/|.....|-......./..../......-........../..|......../.-..\..../....../...|..........
............/..--..........-............\.......\................\........................../../....|.........
..|..-................|.|..|................/.......\............-//.......................\..\...............
.......................-......-..................|....\................-......\.|....\....|..../../|.-........
........................-..................-...........................-..-................-................|.
/.\....|.-.................................\..-.....\./.........../-.|..............\..\.....--|/....\........
.......-..-../..................\..--|..............|...............|\........|...--................|./.\.....
..........|................-.......|..................................................|...................|...
-.-..|....-.|........-./...|.-.........\.....\/........-........\........../................./.../........\...
..................../...|/......................................../............\.|./../................/\|....
...//.........................\....../..............\../.......-..\....|......................................
............................./..|./../.........-/..\..........................................................
......\.....|..................|..................\.....\.-..\...../............................../...........
..........\...........-..........-.....-....\/................../.................................\.....|..\..
..../.\...................................\............\...\....|.................|.....................-.....
...........-...........................\.......|-../.|....|.....|......................-...../................
.......|.........\..............|....\...-......../...........-.....-..-...........-..................\...\.|.
...........|.......................|....-..........\............................\.................../.\.......
..../.................\.......-..|.................../........................|...............\...........-.|.
............./..|.-\..../.....-............-....|..\......|......../............-.........|................\..
...................\...\/......\............|......................\....|...................-./-..............
..-\...........|.................../.....|....|............................-..|............\.............\....
.|.....\....\........|..................\...............|..-/.........|.-/...........-............-...........
.....\...../......-............|........./..|............-........................\....-......................
......\......../........|..............\...-..-...|.......................................................|...
...................|.................................\.....\...........................|../.|.-......-........
..................|..............\.....\............|........../....../...............\.........\.............
..|...........-..../....\.........--...\................../.............-...-....................-..../....|..
.|...\...................|......-...........-\..........\.....|.....|..........|.........|....|-../...........
.............\|...................../\...............\....../.......-....\.....................-..............
.......//.....................\....\......\......\.........|...................../......../.........\......-..
.\./.........-...-./-.\.............../......|................................\|....\...../....-..............
.......|......../......../......./.........../...............|............................../..|......../.....
............./.........|.....-./.........../....-......./|......-................|............................

188
aoc2023/day16/main.go Normal file
View File

@@ -0,0 +1,188 @@
package main
import (
"bufio"
_ "embed"
"fmt"
aoc "go.sour.is/advent-of-code"
)
// var log = aoc.Log
func main() { aoc.MustResult(aoc.Runner(run)) }
type result struct {
valuePT1 int
valuePT2 int
}
func (r result) String() string { return fmt.Sprintf("%#v", r) }
func run(scan *bufio.Scanner) (*result, error) {
var m Map
for scan.Scan() {
text := scan.Text()
m = append(m, []rune(text))
}
rows := len(m)
cols := len(m[0])
options := make([]int, 2*(rows+cols)+2)
i := 0
for j := 0; j <= rows-1; j++ {
options[i+0] = runCycle(m, ray{[2]int{j, -1}, RT})
options[i+1] = runCycle(m, ray{[2]int{j, cols}, LF})
i += 2
}
for j := 0; j <= cols-1; j++ {
options[i+0] = runCycle(m, ray{[2]int{-1, j}, DN})
options[i+1] = runCycle(m, ray{[2]int{rows, j}, UP})
i += 2
}
// fmt.Println(options)
return &result{valuePT1: options[0], valuePT2: aoc.Max(options[0], options[1:]...)}, nil
}
type stack[T any] []T
func (s *stack[T]) Push(v T) {
if s == nil {
panic("nil stack")
}
*s = append(*s, v)
}
func (s *stack[T]) Pop() T {
if s == nil || len(*s) == 0 {
panic("empty stack")
}
defer func() { *s = (*s)[:len(*s)-1] }()
return (*s)[len(*s)-1]
}
var (
ZERO = [2]int{0, -1}
UP = [2]int{-1, 0}
DN = [2]int{1, 0}
LF = [2]int{0, -1}
RT = [2]int{0, 1}
)
type ray struct {
pos [2]int
dir [2]int
}
func (r *ray) next() [2]int {
r.pos[0] += r.dir[0]
r.pos[1] += r.dir[1]
return r.pos
}
type Map [][]rune
func (m *Map) Get(p [2]int) rune {
if p[0] < 0 || p[0] >= len((*m)) {
return 0
}
if p[1] < 0 || p[1] >= len((*m)[0]) {
return 0
}
return (*m)[p[0]][p[1]]
}
func runCycle(m Map, r ray) int {
current := r
s := stack[ray]{}
s.Push(current)
energized := make(map[[2]int]bool)
// energized[current.pos] = true
cycle := make(map[[4]int]bool)
for len(s) > 0 {
current = s.Pop()
r := m.Get(current.next())
// fmt.Println("pos", current.pos, current.dir, string(r), len(s))
if r == 0 {
continue
}
energized[current.pos] = true
v := [4]int{
current.pos[0],
current.pos[1],
current.dir[0],
current.dir[1],
}
if _, ok := cycle[v]; ok {
// fmt.Println("cycle")
continue
}
cycle[v] = true
switch r {
case '|':
switch current.dir {
case UP, DN:
// pass
case LF, RT:
current.dir = UP
s.Push(ray{current.pos, DN})
}
case '-':
switch current.dir {
case LF, RT:
// pass
case UP, DN:
current.dir = LF
s.Push(ray{current.pos, RT})
}
case '/':
switch current.dir {
case UP:
current.dir = RT
case DN:
current.dir = LF
case LF:
current.dir = DN
case RT:
current.dir = UP
}
case '\\':
switch current.dir {
case UP:
current.dir = LF
case DN:
current.dir = RT
case LF:
current.dir = UP
case RT:
current.dir = DN
}
}
s.Push(current)
}
// for i := range m {
// for j := range m[i] {
// if v := energized[[2]int{i,j}]; v {
// fmt.Print("#")
// } else {
// fmt.Print(".")
// }
// }
// fmt.Println("")
// }
return len(energized)
}

View File

@@ -0,0 +1,50 @@
package main
import (
"bufio"
"bytes"
"testing"
_ "embed"
"github.com/matryer/is"
)
//go:embed example.txt
var example []byte
//go:embed input.txt
var input []byte
func TestExample(t *testing.T) {
is := is.New(t)
scan := bufio.NewScanner(bytes.NewReader(example))
result, err := run(scan)
is.NoErr(err)
t.Log(result)
is.Equal(result.valuePT1, 46)
is.Equal(result.valuePT2, 51)
}
func TestSolution(t *testing.T) {
is := is.New(t)
scan := bufio.NewScanner(bytes.NewReader(input))
result, err := run(scan)
is.NoErr(err)
t.Log(result)
is.Equal(result.valuePT1, 8098)
is.Equal(result.valuePT2, 8335)
}
func TestStack(t *testing.T) {
is := is.New(t)
s := stack[int]{}
s.Push(5)
is.Equal(s.Pop(), 5)
}