chore: add day 14 pt1
This commit is contained in:
parent
142a8dc00f
commit
186cfb3157
10
day14/example.txt
Normal file
10
day14/example.txt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
O....#....
|
||||||
|
O.OO#....#
|
||||||
|
.....##...
|
||||||
|
OO.#O....O
|
||||||
|
.O.....O#.
|
||||||
|
O.#..O.#.#
|
||||||
|
..O..#O..O
|
||||||
|
.......O..
|
||||||
|
#....###..
|
||||||
|
#OO..#....
|
100
day14/input.txt
Normal file
100
day14/input.txt
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
...#.O.#.#.O.O....#...O....#..#........OO.O#...O..O#O#.......O.O....O.....#...OO#.O....OOO..OO#..#..
|
||||||
|
.##.....#.OOO.OO#...O.......O#..O.##OO.O.......O##....O....#O..O..#.#....#OO..#...O.O#.#...#....#O.O
|
||||||
|
O...##.O.O.O.O..#.#.....#O....#..#O..O...O..OO..O...##.......OO#..OOO..#O..#.O#.O......#..#O..O..O.O
|
||||||
|
..OO....OOO....O.O..OOO.....O........#...O.O.O.O..O#.O...O..#.#O........O.....#O...##....#...#.#....
|
||||||
|
##.O.O#..O..##O.....O.#.....O.O#O.O..#...#...OO.##OOO...OO#...O...O........O.#.O.........#O.#...#.#.
|
||||||
|
.#.O.O.....#..O###..O#.#..O....O.O....#...##.O..O.OOO..O.O..O#.O..O....#.#.O..#..##O...O....#O#.....
|
||||||
|
.......O...##..O..O##..##...O....###.O.#.#.O......O...O.O......#O.....#..O..O#..##O.O#O##O....O#....
|
||||||
|
.#O..#....##.##..O#..#..#..O........OO....#..##.O.#.O.O..OO.O.O#..#O##O......OO...O...OO...#....OO..
|
||||||
|
#.....O#......O#.....#..O...#O..OO..OO.#O...O...O.........O...#O...O.....#.....#...#.....O##.O.....O
|
||||||
|
OO.O..O...O......#.O......#O....#..#.#...O.O....O.#.O#...O........O......#.#O#.O...O.........#O....O
|
||||||
|
O.O..O..#......O.O.OO...OO..##.....OO.....#.O...O.#.#...OO..OO.O.........#O..#.O...O#.#..O..OO.##O..
|
||||||
|
..#.......O....#...#....##..O.O......O....O#.O..O..#.#.O#O.O.#...#.##.....O...O.O...O.#...O...O.#...
|
||||||
|
.O...#..O.....O.O.....OO.O.O.......O..O..........#...O..O..O......O....#OOO...O#O...#.O#........O...
|
||||||
|
.OO#....#.....O...#.###..#..O..O.....#O..#..OO...OOO#.O#..O#O........#..#.O..OOO#O#..O..OO........#.
|
||||||
|
..O.....#..#..O.............#..O.O..O#.......#.O.........O.#..OO.O..#..##..O.O#O....OO.O.O.O.....O#.
|
||||||
|
........#O..O#OO...#.##O.O....O...O..........##.#O..O......O..O.#...#O.O.OO...#.#OOO.#...O#....#.O.#
|
||||||
|
.....O.O...#OO#.OO#......#..O.O..#.OO.......#...#.O.O#.O.#O.O#..#O##..OO...#......#.O....#.....O.#O.
|
||||||
|
.O#OOOO...#.#..........O#.#.#.#.O....O..O...O...OOO.O.O.O..OO..O..O#.#...#....#...#O#.OO.O..#.O#....
|
||||||
|
OO#OO..#.O.#....#..#.#..#O#.O#O#.#..O.O....#..O...OO..O..#........#.O...O#.#.....#....O.....#.O.....
|
||||||
|
.O.....#...OO...#..O..#..O..O##..OO...O....#.O.#............###.O.#..O..O.........O..#.O..O.#OO.#...
|
||||||
|
.##O.#.O.#....O..O..#..O.#.O..##O..O......OO...#...O..OO#....O..O#O.##.OO#...O#...#.......O.O#.#OO..
|
||||||
|
.OO#O#...O..##.O......O..O.O....O...OOO.O..#...O#.#.O.OOO..#...#.##..OO..................OOOOO...###
|
||||||
|
.#..##...####.O#...O........O...#O..O..O....O##O..O..OO..#.....#...O.....OO......OOO.#..O.##OO##...#
|
||||||
|
....O#........O##.O...O#..#.O#O...O...O.O........#..#.#.O......O..#.....#.O..#.#..#..#O....O##...#..
|
||||||
|
.O..OO.O##..O...O.OO....O.OOO..#O#.O##......O.O#....O#O.O......#.O..#O..#O.........#..OO....#.OO..O.
|
||||||
|
.#OO..#OO.###........#...#..#.O..#O.O#O...O.#.....O...#......O.......#.O.#.##.O..#O.O###OOOO...#..O.
|
||||||
|
##...OOO.#..........O#...O#.....OO...O#..OO.OO.#.OOO..OO....O..OOO...##O...O.........##O...#.#O.....
|
||||||
|
.#....O.....#OO.O.OO..O......#.OO..#O...O..##O.#O....#O.OO..#O.OO...O..O.#..##...O......#O##OO.#.O..
|
||||||
|
.#..O....O....#......#.O.##...OO.O.O...O.OO.O....#...#..O....O#O..#...#.#..#...OO......OOO...O..#...
|
||||||
|
#.#OO.#....O.#.OO....#........O.O.##...O.#.................OOOO....O..#......O..#.#...O....#.O#OO.#O
|
||||||
|
....O.O.......##..O.........OO#O..O.O#.#.#.O....#..OO.O..O#O....O..O.O......O.O.#..O.......O.O..O.O#
|
||||||
|
...........#OO...O....#.O.#..#O.O#.....O..O.....O#.....OO.#O...#O.#O.#.....OO.O.O....OO......O#O.O..
|
||||||
|
OOOO.OO..O....O.O...O..#.........O#.##OOO#.O..O.O.O......O..#..O..#..#.O..O....O#O.#OO..O.O...O#.##.
|
||||||
|
##.O.#O.#OO#..........OO#.OO.#..#.#......#.OO...#.O.O.......O##....O..O..#.#.#O....O..##.#OO.O..O..O
|
||||||
|
#O...OO..#.#....##.O#.#...##OO#........O#.O..O#.........O.O.#....O.#.O.......O..OOO......O#O.###.O..
|
||||||
|
....O#....#..O...#.#.....O.O#O..#O....O...O.OO#...O..O#.##.O.....O...#..#.O.....O...O#..O.#...#.#O..
|
||||||
|
O.OO......#..O...O.O....#...OO..O#.O..OO..#.##.....#....O.O#O#.O....O..#..OOO..O....O.O..#..O.O..O..
|
||||||
|
.#..#..##O..O.....O#.#.O.O..O..OO..O.O..OO.##.O...O..O#........O.............#.OO..#......O..#O#OOO.
|
||||||
|
.#..##...O.#..OO...O..#..#O#..#..##.O#.#.....#....O.O..O.........#.###...#.O##..O...#.O.OO..........
|
||||||
|
........#O.......O...O#.#....#.O#....#..O.....OO........O...#.........##.....O...O#.O...O.O..#OO.O.#
|
||||||
|
.#..#.O##..##.O#.#.......OO...OO#...#O#............#.#...#...O...#.O..#O.#.O.........O..OO.O...O.#..
|
||||||
|
#.O#OO...OO.O.#...##........#....O.#.O.....##.#.O.O.O.#..O#...O..#......#..O...###O..OO..##..OO.#OOO
|
||||||
|
.OO...#O....O##.O...O.#....O...#O.......O.....OO.O...O..#...OO.OO.O#...O##.....O..#.#...#OO......O..
|
||||||
|
..O..#...O...##..O...O..OO.O.O.O.....#.....O.....O..#OOO.....OOOO#.#.O..O..#........O.#O#.#O..O.##..
|
||||||
|
.....##.....O.....O#...##.OOO....O.O..O.#.#O........OO.....#...##...#.#......#....OO.O.....#O....#.#
|
||||||
|
..O.OO#....O..O.#O.#OO#..#.O.##.#..#.OO##....O#...#..O.#..#...#.#....OOOO.#...OOOO..O.........##O.O.
|
||||||
|
O.O.#......O#...O..#.......#O.O..OOOO..#....#.#.OO.O..#O......#.#....#...#..O....#O.OO..O.......#.#.
|
||||||
|
...O.O....O#......#.O.O....O#.#..#O.#.....#...#...#.O#.........O.OO..O###...O....O.#.O.#.....##..O..
|
||||||
|
.#.OO...O#O.....O#....O......OO...OO.OOOOOO##....#..#..#O.#.OO....O#..OO.#O........O#..O.O#.#.......
|
||||||
|
#OO#.#.....O#OOO#...O.........#......O#.......O.....O#.#O...O........#....O...O.....#.......##.OOO#.
|
||||||
|
..#.O##..#OO..#....#O....#....#..........#O...#...O.#.O.O###O..O..#...#.OO....##..O.O.O.....O..O....
|
||||||
|
....OOO#.#......#..O.O......##O.#.O.#.O.OO....#..O.......#.....#O.O...##O###.##.##..#.O.#.O#..#..O#.
|
||||||
|
......O#OO......#.#..#.O#OO#O..O#O.O##.......O...O........O......O#.....#..#....#...#.#..O...O##.#O.
|
||||||
|
#..OO..O.O.....#O.O.#.#......O......O.#....O.#OO.........#.O.O#..#..#.O..O.#..#...#...O.#..........O
|
||||||
|
..O#OOO#.#.....O.....O..##.#..OOO.O..#..O#...##O..OO...#....#..#...#...OO.....##OO#.OO...#.O....#.#.
|
||||||
|
#.#O.............#....O#.....O.....O.##O.....#.#.#.....O....O...O...##..O...#........#O..##.#.....##
|
||||||
|
.O.....O...#O.#.O#..O#........#.##...O##O..........O##.O......O..#....##....###......O##..##O..#.O#.
|
||||||
|
.#...#..#..O.O.............#....O.OO.....OO#O.....OO..OOO#O.O.O.O...#...#.........OO..O#.O...#.##..#
|
||||||
|
..O...#O#.......#OO..........OO#..#.....OO#...#OO.O#....OO....O..O..#....#OO......O#O...#......O..O.
|
||||||
|
O.......O..O..#O.....O#O.........O##.O#.#.O.O....##....O.#....O....OOO..#O#...#...O.O#.O.....O.O#.O.
|
||||||
|
O.#O....O..##....O.........##.OO###....O...O...O..O#O#...#O.O.O....#..O..#..#.O.......###..#....#..O
|
||||||
|
.........O..#O...O..OO.....#...O..#O#.....O....#O#....##...#.OOOO.O..O#...O..O.O#.#..O.O..#..#.OO...
|
||||||
|
...#.....#..OO.O.#.#.O.....O#...#..O...O..O..#...O..O.O..#...#.###..#..##O.#.#O.......O..##O.O.O#...
|
||||||
|
.##O......O....#....O....#O..#.....#.O..O.O.O..#.#O.....O#.O...#.#O.#...#.........O....O....OO..O...
|
||||||
|
.OO.O.O.O#O..O##.O...OO.....O..O....O....#..#...O.O...O....O.#.O......#O.#.O#O..O...O.O.#.O..O.#....
|
||||||
|
O#..#.#..#..#...#.O...O.###.##.OO.O.#.OOO..O..#O.#.O...OO...O.OOO....#.....#OO.O##.O.#.#O...#.#.....
|
||||||
|
.#.....#.........O.#.........#...OO..O.#.O...O##O...#..O.O.OO#....###.OO.....#..........#.#OO...O.O.
|
||||||
|
........OO..O.O.#..O.#.......#.#O.OOO...OO.O............O#O#...O...#.O.....#...........O...#..OO.O#O
|
||||||
|
..#.#.#...O....#O.......#...#...O###.O.....O.O.OO....#O...OO.O.O........O.O.#O.O....OO.....OO.....#.
|
||||||
|
#..OOO#......O.#.O#...#..#..O..O.O.O.#...O#OOO.#...O.#.OO.#O..#....#..#O....O.#.O...O...O#OO...#....
|
||||||
|
.....#.....##...O....#..O..O.....O#OO.#O#.....O...O.##.#........O..O........#.#......##......#..#..O
|
||||||
|
O..O....O.##..#.#O##.#.....OO....O.##O......#...#..OO.O.....#...#........O..O#O##.O.......#..##O..##
|
||||||
|
#.....#O....O...#OOO#..O#.O.....O..OO.#..#.#.O#..O..O#.O###....O......O.#.O..#O.....#.......##O.O..O
|
||||||
|
O.O.#..#....O....O.......#.O..O#O#.O..........#O.....#OO.O...#.OO.....#...O....OO....#...O....O.#..O
|
||||||
|
..#...OO....OO......O....#..#..........##..##..#.....##...#..#O.....#OO......#........O.....O..O...#
|
||||||
|
.OO#...O#.O....#.........OO#...O#..O.#..O.O.O.#O......##.O.......O...O...###.O......O....O...#.OO#..
|
||||||
|
..O..O#O.#..O.#O...OOO#O.#.O....O.#......O....#....###....OO......O...#...OO.#....O........O#..#..OO
|
||||||
|
#..OO..O......#...##..O#.O........O.O...##.#O...O...#O#.O.O....#.............#.O#.#..O..#..OO#.....#
|
||||||
|
#..........O...O##.O#..O..OO..O.....#O.#.O......#......O.O....##.O#OO..O.O..O.O.O..#..O....O..#....O
|
||||||
|
.O.O...O.#..O.O....#.#.O.O.##.##..O..#OOOO.###...#O...#...O.....O...#.##.OO..O.#..#.O.#.......O....#
|
||||||
|
#....#....OOOOOOO.#...OO....##O.#..#......O..#..........O......##..O..###..#.O....O.......OO......#.
|
||||||
|
.O.O..##.O.#O.#...#....O..O.....#..##......O.......O..O#.O##.#........O..#.O.#.O.O..##..###..##.#...
|
||||||
|
..OOO..O##..OOOO.O.....OO##..O..O.#....#.....OO......O.OOO#...#O.OO...O.O......O#..O.##....###...#.O
|
||||||
|
....O...O.O....#..O#.O.....O.O.#.......O##...O#..OO.O.O##...#.....O#.###....O....#O.........OO..O.O.
|
||||||
|
O#.O......##..#.#..............#......#.#.OO#......##O#OO..OOO..##.O##....#..O...#......##O.O##...OO
|
||||||
|
...#OO....OO...O#.#..##....OO.#..#O......O#....#.O.....O.#O..OO.#..OO...O#.O#....O.#.OO.O.O#..O.....
|
||||||
|
#..O.OO#O...O..O..........#..#.#..#..#O.....#O#..O....#..O...O.#.#O#.#OO.....OOO.#..#.........O...O#
|
||||||
|
....O......#..........O...#.....OOOO........#.#OO.O............O...#....O........#....#....#........
|
||||||
|
...O.......###...O.O...#O.##O.OOO.........O.###.O.......O....O.O...O.#.....#..#O..#..OO..O.O...O....
|
||||||
|
O.O#....##.O.O.....O....#............OO.#O...O..#...##.#.#O#O.#.....O......O....#.OO#.O#....#.....O.
|
||||||
|
..O####.O.........O...........OO..OO.#.....O.......#..O#.O....O#...##.......#.#....O..O..O#..##.....
|
||||||
|
.#..#......O.##.O.OOO.O.OO....O.......#......#.O...OOOO...O..#..#.O.#......O...O#.#.O.#.O....O....O.
|
||||||
|
##....O.O..O..O.#...O##.......O....#...#OOO.O..##.....#....O.......#O..#.#O..O.#.......O..#.O.O#.O##
|
||||||
|
...O....O#O.#...#......#..O.O.O.O...O.....O..O.#..#.....O#...OO...#.O.O....#..OOO...#.....OO.OO..O..
|
||||||
|
O##.O.##.O#OO...OO...OO..O....OO#O.#O..O.O....OO..........O..#.O.#O#.......O.O..#O#....#..#...#O..#.
|
||||||
|
.#.#..........#.......#..OO#....O.#.#.O.....OO.#.O..#.#.O....#..O....#..#..O.#O#O.....#.O.O.#O.O.#O.
|
||||||
|
.O.#....##O...#..OOO.#......#...#.O.......OO....#.......#..#.OOO........OO..O..###O##..O.....O##....
|
||||||
|
OO##..O#....O.##.......O...#........O.O...#O#.#.OOO....#......#.#O.O.O..O..O...#.#.O.O.OOO.O#O......
|
||||||
|
#O...#....OOO.O...#O....O...O..........O..#..O#.O..#O.....#......#.#..#.OO..#....#O#....O...O....##.
|
||||||
|
.OOOO......##.#O.#O...#......O..#..O..#.O...O.OO...............#O...O..##..#.#....O...O..O.##..O.O..
|
151
day14/main.go
Normal file
151
day14/main.go
Normal file
|
@ -0,0 +1,151 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
_ "embed"
|
||||||
|
"fmt"
|
||||||
|
"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) {
|
||||||
|
var maps []Map
|
||||||
|
var m Map
|
||||||
|
|
||||||
|
for scan.Scan() {
|
||||||
|
text := scan.Text()
|
||||||
|
if len(text) == 0 {
|
||||||
|
maps = append(maps, m)
|
||||||
|
m = Map{}
|
||||||
|
}
|
||||||
|
|
||||||
|
m = append(m, []rune(text))
|
||||||
|
}
|
||||||
|
maps = append(maps, m)
|
||||||
|
|
||||||
|
score1 := 0
|
||||||
|
for _, m := range maps {
|
||||||
|
m = aoc.Transpose(reverse(m))
|
||||||
|
m.Sort()
|
||||||
|
score1 += m.Score()
|
||||||
|
}
|
||||||
|
|
||||||
|
score2 := 0
|
||||||
|
// type record [5]int
|
||||||
|
// var last, current record
|
||||||
|
// memo := make(map[record]int)
|
||||||
|
|
||||||
|
// for _, m := range maps {
|
||||||
|
// fmt.Println(m)
|
||||||
|
|
||||||
|
// m = aoc.Transpose(reverse(m))
|
||||||
|
|
||||||
|
// for i := 0; i < 1_000_000_000; i++ {
|
||||||
|
// m.Sort()
|
||||||
|
// current[0] = m.Score()
|
||||||
|
|
||||||
|
// m = aoc.Transpose(reverse(m))
|
||||||
|
// m.Sort()
|
||||||
|
// current[1] = m.Score()
|
||||||
|
|
||||||
|
// m = aoc.Transpose(reverse(m))
|
||||||
|
// m.Sort()
|
||||||
|
// current[2] = m.Score()
|
||||||
|
|
||||||
|
// m = aoc.Transpose(reverse(m))
|
||||||
|
// m.Sort()
|
||||||
|
// current[3] = m.Score()
|
||||||
|
|
||||||
|
// m = aoc.Transpose(reverse(m))
|
||||||
|
// current[4] = m.Score()
|
||||||
|
|
||||||
|
// if slices.Equal(last[:], current[:]) {
|
||||||
|
// break
|
||||||
|
// }
|
||||||
|
|
||||||
|
// v, ok := memo[current]
|
||||||
|
// if (i - 104) % 13 == 12 {
|
||||||
|
// fmt.Println(i, current, ok, v)
|
||||||
|
// }
|
||||||
|
// // if ok && v > 2 {
|
||||||
|
// // break
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// memo[current] += 1
|
||||||
|
|
||||||
|
// last = current
|
||||||
|
// }
|
||||||
|
|
||||||
|
// score2 += m.Score()
|
||||||
|
// }
|
||||||
|
|
||||||
|
return &result{valuePT1: score1, valuePT2: score2}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Map [][]rune
|
||||||
|
|
||||||
|
func (m Map) String() string {
|
||||||
|
var buf strings.Builder
|
||||||
|
for _, row := range m {
|
||||||
|
buf.WriteString(string(row))
|
||||||
|
buf.WriteRune('\n')
|
||||||
|
}
|
||||||
|
buf.WriteRune('\n')
|
||||||
|
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
func (m *Map) Sort() {
|
||||||
|
if m == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, row := range *m {
|
||||||
|
base := 0
|
||||||
|
for i, r := range row {
|
||||||
|
if r == '#' {
|
||||||
|
base = i + 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if r == 'O' {
|
||||||
|
if base < i {
|
||||||
|
row[base], row[i] = row[i], row[base]
|
||||||
|
}
|
||||||
|
base++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (m *Map) Score() int {
|
||||||
|
if m == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
sum := 0
|
||||||
|
max := len(*m)
|
||||||
|
for _, row := range *m {
|
||||||
|
for i, r := range row {
|
||||||
|
if r == 'O' {
|
||||||
|
sum += max - i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
func reverse(m Map) Map {
|
||||||
|
for _, row := range m {
|
||||||
|
aoc.Reverse(row)
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
46
day14/main_test.go
Normal file
46
day14/main_test.go
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
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, 136)
|
||||||
|
is.Equal(result.valuePT2, 64)
|
||||||
|
}
|
||||||
|
|
||||||
|
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.True(result.valuePT2 < 87286) // first submission
|
||||||
|
is.True(result.valuePT2 < 87292) // second submission
|
||||||
|
is.True(result.valuePT2 < 87287) // third submission
|
||||||
|
|
||||||
|
is.Equal(result.valuePT1, 110407)
|
||||||
|
is.Equal(result.valuePT2, 87273)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user