From 596f3e7533d8e9a7addf358f860cb267ba1c11ee Mon Sep 17 00:00:00 2001 From: xuu Date: Tue, 12 Dec 2023 13:44:28 -0700 Subject: [PATCH] chore: add day 11 --- day10/main.go | 30 ++---- day11/example.txt | 10 ++ day11/input.txt | 140 +++++++++++++++++++++++++++ day11/main.go | 214 ++++++++++++++++++++++++++++++++++++++++++ day11/main_test.go | 41 ++++++++ template/example.txt | 10 ++ template/input.txt | 0 template/main.go | 30 ++++++ template/main_test.go | 41 ++++++++ tools.go | 97 +++++++++++++++++++ 10 files changed, 592 insertions(+), 21 deletions(-) create mode 100644 day11/example.txt create mode 100644 day11/input.txt create mode 100644 day11/main.go create mode 100644 day11/main_test.go create mode 100644 template/example.txt create mode 100644 template/input.txt create mode 100644 template/main.go create mode 100644 template/main_test.go diff --git a/day10/main.go b/day10/main.go index ee53205..b82bf25 100644 --- a/day10/main.go +++ b/day10/main.go @@ -113,8 +113,7 @@ func (m *Path) readLine(text string) { } func (m *Path) buildPath() int { m.start() - for m.next() { - } + for m.next() {} return (len(m.p) + 1) / 2 } func (m *Path) start() { @@ -178,23 +177,15 @@ func (m *Path) next() bool { last := n.whence next := m.head.left.whence - switch (last<<4)|next { - case 0x11, 0x22: m.n.value = '|' // UP UP, DN DN + switch last<<4|next { + case UP<<4|UP, DN<<4|DN: m.head.value = '|' // UP UP, DN DN + case LF<<4|LF, RT<<4|RT: m.head.value = '-' // LF LF, RT RT - case 0x13: m.head.value = 'J' // UP LF - case 0x14: m.head.value = 'L' // UP RT - - case 0x23: m.head.value = '7' // DN LF - case 0x24: m.head.value = 'F' // DN RT - - case 0x33, 0x44: m.head.value = '-' // LF LF, RT RT - - case 0x31: m.head.value = '7' // LF UP - case 0x32: m.head.value = 'J' // LF DN - - case 0x41: m.head.value = 'F' // RT UP - case 0x42: m.head.value = 'L' // RT DN + case UP<<4|RT, LF<<4|DN: m.head.value = 'J' // UP RT, LT DN + case UP<<4|LF, RT<<4|DN: m.head.value = 'L' // UP RT, RT DN + case DN<<4|RT, LF<<4|UP: m.head.value = '7' // DN LF, LF UP + case RT<<4|UP, DN<<4|LF: m.head.value = 'F' // DN LF, RT UP } return false @@ -210,7 +201,7 @@ const ( ST int8 = iota UP DN - LF + LF RT ) @@ -255,7 +246,6 @@ func (m *Path) peek(d int8) *node { if any(r, 'J', '|', 'L', 'S') { return &node{value: r, whence: UP, pos: p} } - case LF: x, y := toXY(m.n.pos, m.w) if x == 0 { @@ -267,7 +257,6 @@ func (m *Path) peek(d int8) *node { if any(r, 'F', '-', 'L', 'S') { return &node{value: r, whence: RT, pos: p} } - case RT: x, y := toXY(m.n.pos, m.w) if x == m.w { @@ -278,7 +267,6 @@ func (m *Path) peek(d int8) *node { if any(r, '7', '-', 'J', 'S') { return &node{value: r, whence: LF, pos: p} } - } return nil } diff --git a/day11/example.txt b/day11/example.txt new file mode 100644 index 0000000..a0bda53 --- /dev/null +++ b/day11/example.txt @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... \ No newline at end of file diff --git a/day11/input.txt b/day11/input.txt new file mode 100644 index 0000000..639a7a9 --- /dev/null +++ b/day11/input.txt @@ -0,0 +1,140 @@ +..............................#.........................#.......................#..............................#.........................#.. +....#.....#...................................#............................................................................................. +.....................................#..................................#................................................................... +...................................................#....................................................................#................... +...............................................................#..................#...............................#......................... +...............#.......................................#...................................#.....#.......#.........................#........ +............................................................................................................................................ +............................................................................#............................................................... +.......#.......................#....................#....................................................................................... +........................#..................#....................................................................#........................... +..#.......................................................#.........#....................#....................................#............. +..............#...........................................................#..........................................................#...... +......................................#...........................................#..........................#.............................. +.....................................................#.........#..........................................................................#. +#.......#....................................#.................................................#............................................ +..............................#.......................................#..............#....................#..........#...................... +....#...................................#................................................................................................... +....................................................................................................#....................................... +.....................#...........#...............................................#..................................................#....... +...............................................#.........#..................................................#..................#............ +..........................................................................#...............#........................#........................ +.................#.......................................................................................................#.................. +......................................................................................#..................#..............................#... +.............#...................................................................................#.......................................... +#...........................#..........#.................................................................................................... +.........#........................................................................#...............................#......................... +.....................#.........................#.....................................................#....................#......#.......... +.....................................................................#...................................................................... +................................#.....................................................................................................#..... +...............#..........................................#.................#................#.............................................. +....................................#...........................#...........................................#............................... +.......#...................#................#.......................................#..............................#......................#. +......................#.................................................#................................................#.................. +....................................................#......................................#............#................................... +.............#........................#..........................................................#.............#............................ +...................#..............................................................#......................................................... +.........................................................#..................#..........#.................................................... +........................#.....................#..............................................#.........................................#.... +.................................................................#.........................................#...................#............ +...................................................#........#.....................................#......................#.................. +.........#....................#.......#................................................................#............#....................... +#.............................................................................#.....................................................#....... +..................#.......#................................................................................................................. +.............#........................................................#........................#................#..........#..............#. +.....................................................#...................................................................................... +.......#...............#.................#......................#................#.......................................................... +...................................#.....................................................#............................#..................... +............................................................................................................................................ +...............................#........................................#.........................#......................................... +.....#.........#...............................#...................#.....................................................................#.. +............................................................................................................................................ +..........#......................................................................#.......................................................... +.........................................#.............................................#......#...................#...................#..... +........................#.....#........................................................................#.................................... +...................................................#.....#...................#.............................................................. +....................................................................#...............#....................................#........#......... +...........................#..............................................................#................................................. +...#........................................#.........#.......................................................#..............#.............. +..........#....................................................#................#........................................................... +..................................#..................................................................................#...............#...... +.................#.......................................................................................................................... +............................................................................................................................................ +............................#.............#................................#.........................#.....................#................ +.........#.........................................#............#.........................#............................................#.... +....#................................................................#.........................#..............#............................. +...................#......................................#................................................................................. +..............................#......#..............................................#..............#................#....................... +.............................................................................#.............................................................. +.......#.........................................................................................................................#.......... +..............#............#....................#...................#............#.......#.................................#................ +................................#......................#................................................#.................................#. +............................................................................................................................................ +.#......................#......................................#...............................................#............................ +........................................................................#............................#...................................... +..................................................#..........................#..........................................#.......#........... +.........................................#..........................................#....................#..............................#... +......#...............#.................................#................................................................................... +....................................#....................................................................................................... +...............................................................#................#........#.........................#..........#............. +..#........#.....#..........#...................#...................................................#....................................... +.......................................#...........................#...........................................#............................ +.............................................................................#.............................................................. +.......................#..................................#............#...............#.......#.....................................#...... +............................................................................................................................................ +............#.....#................................#...........#...................................................#............#........... +...........................................#..............................................#................................................. +..................................#..............................................#.....................#.................................... +..................................................................#......................................................................... +...#..........................#............................................#................................................................ +.......................................................#..........................................#........#........#....................... +.......................................#.................................................................................#......#........... +.................................................#..........#.........#.......................#............................................. +............................................................................................................................................ +....#...........#........................................................................................#.......#...........#.............. +......................#...........#.......#....................#..............#.....#....................................................... +.........#...............................................................................#.................................................. +..................................................#......................................................................................... +...................#......................................#...........................................#...........................#......#.. +.............#..........#.....................#....................#........................................................................ +...............................................................................#.....#.....#.................#......#....................... +...#.........................#...................................................................#........................#................. +............................................................................................................................................ +.......#.........#..........................................#............................................................................... +.................................#..............................................................................................#........... +........................................................................#..............#.......#..................#........................# +.#...................................#......#......#........................................................................................ +............................................................................................................................................ +.............#................#............................................................#..............#................................. +......#.....................................................#.......................#................#...............................#...... +...................#..............................................#.......................................................#................. +....................................#.....................................#................................................................. +........................................................................................................#.......#........................... +....#.....................................#...............#...................#...............#.........................................#... +.....................................................................................#...................................................... +....................................................#...........#......................................................#............#....... +........................#...................................................................................#............................... +............................................................................#.....#.........#......................#........................ +...#.....#.....#.......................#.................................................................................................... +............................................................................................................................................ +......................................................................#..................................................................... +......................................................................................#............#..........................#............. +.....#.............#.........................................................#................#............................................. +..............................#..........#............#.....#...........................................................#.............#..... +........................................................................#................#.................................................. +..................................................................................#......................................................... +......................#.............#....................#........................................................#......................... +.................#................................#.............#.............#............................................................. +............................................................................................................................................ +...#..........................#.............#....................................................#.......#...............#.........#........ +........#...........#.................#..................................................................................................#.. +..............#.................................#.....#..........................................................#.......................... +...........................................................#......#......................................................................... +............................#..................................................#.....................................#...................... +...................................................#.......................................#................................................ +.#................#................#...............................................#...........................................#.....#...... +.............#....................................................................................................#......................... +.........................#...........................................#.....#......................#..........#.............................. +........................................#...................#...........................................#................................... +...#......#............................................................................#.................................................... +...............................................#.......................................................................#.................... diff --git a/day11/main.go b/day11/main.go new file mode 100644 index 0000000..ce3a4d1 --- /dev/null +++ b/day11/main.go @@ -0,0 +1,214 @@ +package main + +import ( + "bufio" + _ "embed" + "fmt" + "sort" + "strings" + + aoc "go.sour.is/advent-of-code-2023" +) + +// 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) { + m := NewMap() + + for scan.Scan() { + text := scan.Text() + m.readLine(text) + } + // solution9 := m.expand(9).sumPaths() + // fmt.Println(solution9) + + // solution99 := m.expand(99).sumPaths() + // fmt.Println(solution99) + + return &result{ + valuePT1: m.expand(1).sumPaths(), + valuePT2: m.expand(999_999).sumPaths(), + }, nil +} + +type Map struct { + rows int + cols int + + emptyRows map[int]bool + emptyCols map[int]bool + + *aoc.List[rune] +} + +func NewMap() *Map { + return &Map{ + emptyRows: make(map[int]bool), + emptyCols: make(map[int]bool), + List: aoc.NewList[rune](nil), + } +} +func (m *Map) String() string { + buf := &strings.Builder{} + fmt.Fprintf(buf, "Map size %d x %d\n", m.rows, m.cols) + fmt.Fprintln(buf, "empty rows:", all(m.emptyRows)) + fmt.Fprintln(buf, "empty cols:", all(m.emptyCols)) + + n := m.Head() + for n != nil { + fmt.Fprintln(buf, toXY(n.Position(), m.cols), n.String()) + n = n.Next() + } + + for row:=0; row