Solution 1 day 5

This commit is contained in:
2024-12-06 19:36:11 +08:00
parent ec1f958f9d
commit bbf76b12d7
3 changed files with 196 additions and 0 deletions

130
2024/day-6/input Normal file
View File

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

56
2024/day-6/solution-1.sh Normal file
View File

@ -0,0 +1,56 @@
#!/usr/bin/env bash
ARRAY_SHAPE=10 # It's a square
FILENAME=test-input
read -r -a MAP_ARRAY <<< "$( paste -s -d '' "$FILENAME" | sed -E 's/(.)/\1\ /g' )"
# Get initial position
POSITION=$(( ( $( grep -n '\^' "$FILENAME" | cut -f1 -d: ) -1 ) * ARRAY_SHAPE -1 + $( grep -o '.*\^' "$FILENAME" | wc -c ) -1 ))
MAP_ARRAY[POSITION]=X # Remove position after save
# up = 0, right = 1, down = 2, left = 3, following 90 degree turn
TURNS=0
while true
do
EXIT=1
# Get direction and travel
DIRECTION=$(( TURNS % 4 ))
if [[ $DIRECTION -eq 0 ]] # up
then
TRAVEL=-$ARRAY_SHAPE
LIMIT=$(( POSITION % ARRAY_SHAPE ))
elif [[ $DIRECTION -eq 1 ]] # right
then
TRAVEL=1
LIMIT=$(( (POSITION / ARRAY_SHAPE + 1) * ARRAY_SHAPE )) # Never verified
elif [[ $DIRECTION -eq 2 ]] # down
then
TRAVEL=$ARRAY_SHAPE
LIMIT=$(( (POSITION % ARRAY_SHAPE) + (ARRAY_SHAPE * ARRAY_SHAPE) ))
elif [[ $DIRECTION -eq 3 ]] # left
then
TRAVEL=-1
LIMIT=$(( (POSITION / ARRAY_SHAPE) * ARRAY_SHAPE )) # Never verified
fi
while [[ ${MAP_ARRAY[$POSITION+$TRAVEL]} != '#' ]]
do
if [[ $POSITION -eq $LIMIT ]] # Walked out
then
EXIT=0
break
fi
MAP_ARRAY[POSITION]=X # Mark walking path
(( POSITION+=TRAVEL ))
done
if [[ $EXIT -eq 0 ]] ; then break ; fi # Walked out
(( TURNS++ ))
done
# Count all coordinates
printf "%s" "${MAP_ARRAY[@]}" | tee travel-path | sed 's/[^X]//g' | wc -c
< travel-path fold -w $ARRAY_SHAPE

10
2024/day-6/test-input Normal file
View File

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