2024 Day 6 Complete!

This commit is contained in:
Brian Buller 2024-12-07 08:09:00 -06:00
parent b852b3951a
commit cf6c7f34f4
6 changed files with 495 additions and 1 deletions

130
2024/day06/input Normal file
View File

@ -0,0 +1,130 @@
......#...........#..............................#..............................#...........................##...............#....
.............#..............#........#......##...................................................#............##...#..........#...
...#..................#..............#.........#...#.........................................................................#..#.
.....#..........#...................#.....................#..........#...........#......#..#...................#..................
...#...............................................##..................#............#.....#...................#...................
.#.#..#...................#.##.................#..........#................................#......#.........##..................#.
....................#...................#....#.........#....................#....................................................#
..........#......#...................................................................................................#....#.......
.#...............#.........#...........#...............#...................#......#...............................................
............#...........#........#..##.......#........#.....#................................##...................................
...................#.#.............###.................##.................#.......................#.....#..............##.........
.........................#...#............................#......#.......................#........................................
.....................#..............................................#.......#......##......................................#......
............................#.#............................................................................#.................#....
.#.....#.......................................................#..........#.......................................................
..............#..............................#.....#....#........#....................#....#.#...........#.#..................#...
........................#...#...##......#...........................#.............#............................#..................
...#........................................................#...#...........................#...#...............#........#........
.........#..#..........#.....................................#..................................................................#.
...........................................#.........#....#........................................#..#...........#...............
..#..........#...........................................................................................#..............#.........
...#.......#......................................#...............#...............................................................
.#........#......#.#....................................#...#...........#..........#........#.#.......#.......................#...
......................#...........#......##....................#..................#......#..............................#.......#.
..............#......................#..............#...#.#............................................................#.#........
..............#..................#...#..................#...#.....#...#...............#...........................................
.............#............................##..#...#...............................................................##..............
..#............................#.......#.#....................................#...................................................
.......#.....#..........#......#.....#...................................#...........................#......#....#..............#.
.#.................#.............................................#................#.............#.....#....................#......
..............#.........#..#.....#...........#..............................#.................................#............#......
....#.#............#............................................................#..........#......................................
.........................................#.......#......#....................................#.....................#...#......#...
....#.............................#....................................#..........#................................#..............
.........#..#......#..............................................................#....................#..........................
.......................................................##...#........#......................................................#.#..#
..........................#.......##..##................................................................................#.........
..................................##..................................................................#..........................#
.#..................................................#..............#.....................#.....#..................................
.......##........................#......................................................#..........#..............................
.........#....#....#.........#.#........................#...#.....................................................#.....#.........
....#.............................#....#....................#.....................#.....#......#.....................#..........#.
............................................................................................#..........#......#...................
..................................................................................#..........................#.#.....#.........#..
..................................................#...................................................#......#.....#............#.
..........................................^#................................................................................#.....
...............#..........#.............................................#.....#....#...................#................#.........
#..#....#........#........#.........#.......#.................#.......................#.#.............#...................#.......
..#................................................................#......................................#.......................
........#......................#.....................##.#..........#.............................#.............................#..
....#..........#........................#.........................................................................................
......#...#.#.#.........#.................#.....#.....#...#......#..................................................##........#...
...#....#....................#...................................................................#......#...............#.........
..#.......#.........................#..........#........#............................#......#....................#................
.................................#.......................................#....................................................#..#
......................#...#........................#.......................#.............#........................................
......#.......#...................................................................................................#...............
.............#............#......#............................#...........................................##......................
..........................#.....................................................................................#.................
....#..............................#......................#........................................#....#...........#.............
................#.......#.........................................................................................................
.............#............#........#.....#.........#.#.#............................#..........#..............#....#.......#.....#
..#.....#...............................................................................................#...............#.........
.......#...........#............#................#.......................................#..........................#......#......
..........#........................................#......................#............#....................................#..#..
.......#......#..............................#........................................................#.#.................#.......
.#.................#..#.....................................................................................#...........##........
#...................................#........................................#..............#.##........#..#.#.............#......
......#...............#........#....#.........#.........#...#.......................#...........................#............#....
...#...................................................................................#.......................................#..
.....#.......................................#................#...............................................................#...
.#................................................##................................#....................#...............#........
.................................................#..#..................#.......#........................#...............#.....#...
.....##.#.............................#.....................#................................................................#...#
...........#....................#...........#...............#.................................................#.......#.......#..#
........#....................................................#.....#....#.........................................#.....#.#.......
...............#..........................#............#.#.................................................#......................
...........#......#.....................#...................................................................#.#..............#....
......#...#.........................#..........................................................#..................................
.....................#................................................#..#........................................................
..............#.................#............#....................#.#.......................................#............#........
............#.....................#................#......#..........................#.#.........#............#...................
........................................................................#.....................#...................................
.............................#....................................................................................................
.........................#...#..........................................#............................................#............
..#...............................#.....#.....#.............#.......#..#.................##.#.......................#.........#.#.
......................................#...........................................................................................
...........#.....................................#................#...#.........................#........................#........
..#...#..#..........................#.......#....#......................................................#................#........
.....................................#...#.#........#...............#..#......#.....................................#...#.........
.#..............#..............................................................#.........................##......................#
..........#..................................#....#.........#...#.#..........#...#...................................##...........
...............#.....................................................................................................#............
.#.......#....................#...#..........................#.#...............#......................#........#.................#
.............#....................#........................#..................#......#....#....#..................................
........#..............................................................#.....................#....................................
.......................#..............#.............#......#.........................................#.........................#..
.......#.......#.#........#.......................#..#.......................................................#.#..................
...............#..#....#.......................#.....#..............................#..................#.....................#....
......#..............#...........................#............................##...........................#......................
.......................................#........#................................#..........................#.....................
.#.....................................#............#.............................................................#..#............
..................#..................#.........................#............#..........#.........................#...#............
.......................................................#..............#..........................................................#
...#....#..#...#..#...................##....................#.....................#...................#.....#..........#..........
.......................#........#....................##.................#.......................................................#.
........#.......#.......................#.#......##...##........#..##.........................................#...............##..
....#..........................#.........##..#......#...........................#........#........#...#...........................
...#..........................#.....#.....................................#.......................................................
....................................##.......#..............................#.............#...#......#.............#..............
.#..............................#...............................................................................................#.
.....#................................................................................##.....#..............#.#...................
...........................................................................................#....#................#................
...#..#...................................................#....#.......#.......#.................................#........#.......
.#....#....................#..............................................................................#.............#.......#.
.........#.................#.......................................#...........#......#.......#............#............#.........
........................................#...............................#.........................................................
..............#....#......................#..........#........#.....................................#...........#..........#......
......##....#.............#..........#......................#.........................................##..........................
....................#.............................#.....................##.#..........#..#......................###.........#.....
.#.......................................##........................#.....#.....#..#...............................................
..............................#............................#.......................................................##.............
.........#.............................#..............#......#.....#..............#.#............................#........#....#..
............#..............#.............................#..........................#.............................#...............
.................#...........#..............#.....#..............................#.............#................#.....#...........
........#.....................#........#.....................................#..........................#......#.#................
.....................................................#..#........................#.##..#.........#................................
......#..............................................#..........................#...............................#..........#......
.................................................#.................##.......................#...................................#.
....................#.....#.......#.............#................................#..........#...#.................................

148
2024/day06/main.go Normal file
View File

@ -0,0 +1,148 @@
package main
import (
"fmt"
"time"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToCoordMap()
cp := inp.Copy()
m := part1(inp)
part2(*cp, m.FindAll('X'))
}
var debug = false
func part1(m h.CoordByteMap) h.CoordByteMap {
init, err := m.FindFirst('^')
h.CheckErr(err)
for moveGuard(m) {
if debug {
fmt.Println(h.CLEAR_SCREEN)
fmt.Println(m)
guard := m.FindAll('^', '>', 'v', '<')[0]
fmt.Println(guard)
time.Sleep(time.Second / 10)
}
}
fmt.Println()
fmt.Println("# Part 1")
fmt.Printf("Guard moved into %d positions.\n", m.Count('X'))
fmt.Println()
m.Put(init, '^')
return m
}
func part2(clean h.CoordByteMap, p []h.Coordinate) {
// m should have all of the 'X' spots where the guard moved in part 1
init, err := clean.FindFirst('^')
h.CheckErr(err)
var obstacles []h.Coordinate
for i := range p {
fmt.Println(h.CLEAR_SCREEN)
fmt.Print("Progress: ")
h.PrintProgress(i, len(p))
fmt.Printf(" %d/%d\n", i, len(p))
m := *clean.Copy()
// Try putting an obstacle at p[i]
m.Put(p[i], 'O')
history := make(map[h.Coordinate][]string)
guard := m.FindAll('^', '>', 'v', '<')[0]
tst := m.Get(guard)
history[guard] = append(history[guard], string(tst))
var foundLoop bool
for moveGuard(m) && !foundLoop {
// fmt.Print(".")
if debug {
fmt.Println(h.CLEAR_SCREEN)
fmt.Println(m)
guard := m.FindAll('^', '>', 'v', '<')[0]
fmt.Printf("Guard: %s - Testing Obstacle: %s\n", guard, p[i])
fmt.Println(obstacles)
fmt.Println(history)
time.Sleep(time.Second / 10)
}
guard := m.FindAll('^', '>', 'v', '<')[0]
bt := m.Get(guard)
// Check if we already have this position/orientation in our history
for j := range history[guard] {
if history[guard][j] == string(bt) {
if !contains(obstacles, p[i]) {
obstacles = append(obstacles, p[i])
foundLoop = true
if debug {
fmt.Println("Found Loop:", string(history[guard][j]), "==", string(bt))
}
break
}
}
}
history[guard] = append(history[guard], string(bt))
}
m.Put(p[i], '.')
m.Put(init, '^')
}
fmt.Println()
fmt.Println("# Part 2")
fmt.Printf("Obstacle positions to create a loop: %d\n", len(obstacles))
// 583 is too low
}
func checkForLoop(m h.CoordByteMap) {
}
func contains(sl []h.Coordinate, c h.Coordinate) bool {
for i := range sl {
if sl[i].Equals(c) {
return true
}
}
return false
}
func turn(g byte) byte {
switch g {
case '^':
return '>'
case '>':
return 'v'
case 'v':
return '<'
default:
return '^'
}
}
func nextPos(guard h.Coordinate, m h.CoordByteMap) h.Coordinate {
switch m.Get(guard) {
case '>':
return guard.East()
case 'v':
return guard.South()
case '<':
return guard.West()
}
return guard.North()
}
func moveGuard(m h.CoordByteMap) bool {
guard := m.FindAll('^', '>', 'v', '<')[0]
currBt := m.Get(guard)
turn := turn(currBt)
next := nextPos(guard, m)
if !m.ContainsCoord(next) {
m.Put(guard, 'X')
return false
}
tst := m.Get(next)
if tst == '#' || tst == 'O' {
m.Put(guard, turn)
return moveGuard(m)
}
m.Put(guard, 'X')
m.Put(next, currBt)
return true
}

170
2024/day06/problem Normal file
View File

@ -0,0 +1,170 @@
[1]Advent of Code
• [2][About]
• [3][Events]
• [4][Shop]
• [5][Settings]
• [6][Log Out]
br0xen [7](AoC++) 10*
 {ʼyearʼ:[8]2024}
• [9][Calendar]
• [10][AoC++]
• [11][Sponsors]
• [12][Leaderboard]
• [13][Stats]
Our [14]sponsors help make Advent of Code possible:
[15]Jane Street - Weʼre a research-driven trading firm where curious
people work together to solve the puzzle of global markets. Will our next
great idea come from you? Our largest offices are in NYC, London, Hong
Kong, and Singapore.
--- Day 6: Guard Gallivant ---
The Historians use their fancy [16]device again, this time to whisk you
all away to the North Pole prototype suit manufacturing lab... in the year
[17]1518! It turns out that having direct access to history is very
convenient for a group of historians.
You still have to be careful of time paradoxes, and so it will be
important to avoid anyone from 1518 while The Historians search for the
Chief. Unfortunately, a single guard is patrolling this part of the lab.
Maybe you can work out where the guard will go ahead of time so that The
Historians can search safely?
You start by making a map (your puzzle input) of the situation. For
example:
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
The map shows the current position of the guard with ^ (to indicate the
guard is currently facing up from the perspective of the map). Any
obstructions - crates, desks, alchemical reactors, etc. - are shown as #.
Lab guards in 1518 follow a very strict patrol protocol which involves
repeatedly following these steps:
 If there is something directly in front of you, turn right 90 degrees.
 Otherwise, take a step forward.
Following the above protocol, the guard moves up several times until she
reaches an obstacle (in this case, a pile of failed suit prototypes):
....#.....
....^....#
..........
..#.......
.......#..
..........
.#........
........#.
#.........
......#...
Because there is now an obstacle in front of the guard, she turns right
before continuing straight in her new facing direction:
....#.....
........>#
..........
..#.......
.......#..
..........
.#........
........#.
#.........
......#...
Reaching another obstacle (a spool of several very long polymers), she
turns right again and continues downward:
....#.....
.........#
..........
..#.......
.......#..
..........
.#......v.
........#.
#.........
......#...
This process continues for a while, but the guard eventually leaves the
mapped area (after walking past a tank of universal solvent):
....#.....
.........#
..........
..#.......
.......#..
..........
.#........
........#.
#.........
......#v..
By predicting the guard's route, you can determine which specific
positions in the lab will be in the patrol path. Including the guard's
starting position, the positions visited by the guard before leaving the
area are marked with an X:
....#.....
....XXXXX#
....X...X.
..#.X...X.
..XXXXX#X.
..X.X.X.X.
.#XXXXXXX.
.XXXXXXX#.
#XXXXXXX..
......#X..
In this example, the guard will visit 41 distinct positions on your map.
Predict the path of the guard. How many distinct positions will the guard
visit before leaving the mapped area?
To begin, [18]get your puzzle input.
Answer: [19]_____________________ [20][ [Submit] ]
You can also [Shareon [21]Bluesky [22]Twitter [23]Mastodon] this puzzle.
References
Visible links
1. https://adventofcode.com/
2. https://adventofcode.com/2024/about
3. https://adventofcode.com/2024/events
4. https://cottonbureau.com/people/advent-of-code
5. https://adventofcode.com/2024/settings
6. https://adventofcode.com/2024/auth/logout
7. Advent of Code Supporter
https://adventofcode.com/2024/support
8. https://adventofcode.com/2024
9. https://adventofcode.com/2024
10. https://adventofcode.com/2024/support
11. https://adventofcode.com/2024/sponsors
12. https://adventofcode.com/2024/leaderboard
13. https://adventofcode.com/2024/stats
14. https://adventofcode.com/2024/sponsors
15. https://adventofcode.com/2024/sponsors/redirect?url=https%3A%2F%2Fwww%2Ejanestreet%2Ecom%2F
16. https://adventofcode.com/2024/day/4
17. https://adventofcode.com/2018/day/5
18. https://adventofcode.com/2024/day/6/input
21. https://bsky.app/intent/compose?text=%22Guard+Gallivant%22+%2D+Day+6+%2D+Advent+of+Code+2024+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2024%2Fday%2F6
22. https://twitter.com/intent/tweet?text=%22Guard+Gallivant%22+%2D+Day+6+%2D+Advent+of+Code+2024&url=https%3A%2F%2Fadventofcode%2Ecom%2F2024%2Fday%2F6&related=ericwastl&hashtags=AdventOfCode
23. javascript:void(0);

10
2024/day06/testinput Normal file
View File

@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...

View File

@ -34,6 +34,21 @@ func NewCoordByteMap() CoordByteMap {
}
}
func (m *CoordByteMap) Copy() *CoordByteMap {
c := CoordByteMap{
Field: make(map[Coordinate]byte),
TLX: m.TLX,
TLY: m.TLY,
BRX: m.BRX,
BRY: m.BRY,
StringEmptyByte: m.StringEmptyByte,
}
for i := range m.Field {
c.Field[i] = m.Field[i]
}
return &c
}
func StringSliceToCoordByteMap(input []string) CoordByteMap {
ret := CoordByteMap{
Field: make(map[Coordinate]byte),
@ -259,7 +274,7 @@ func (m *CoordByteMap) FindLast(b byte) (Coordinate, error) {
}
}
}
return Coordinate{}, errors.New("Not Found")
return Coordinate{}, errors.New("not found")
}
func (m *CoordByteMap) FindAll(b ...byte) []Coordinate {

View File

@ -31,6 +31,12 @@ const (
SHRUG = "¯\\_(ツ)_/¯"
)
func CheckErr(err error) {
if err != nil {
panic(err)
}
}
// Fact returns the factorial of the given int
func Fact(x int) int {
var ret int
@ -402,6 +408,7 @@ func SliceMin(sl []int) int {
return Min(sl[0], sl[1], sl[2:]...)
}
}
func Min(v1, v2 int, vrest ...int) int {
min := v2
if v1 < v2 {
@ -440,3 +447,17 @@ func Sum(l, h int) int {
func ManhattanDistance(x1, y1, x2, y2 int) int {
return AbsInt(x1-x2) + AbsInt(y1-y2)
}
func PromptUser(text string, required bool) string {
var resp string
fmt.Print(text + ": ")
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
resp = scanner.Text()
}
if resp == "" && required {
fmt.Println("Non-empty response is required")
return PromptUser(text, required)
}
return strings.TrimSpace(resp)
}