Compare commits
No commits in common. "a0d56e0f1435de26270bf11d33cca62c21e228ce" and "2b14d3a742146ff8778d5557e8a9ddab8d80b321" have entirely different histories.
a0d56e0f14
...
2b14d3a742
3
data.go
3
data.go
@ -18,6 +18,3 @@ var Day04Data string
|
|||||||
|
|
||||||
//go:embed day_05/data.txt
|
//go:embed day_05/data.txt
|
||||||
var Day05Data string
|
var Day05Data string
|
||||||
|
|
||||||
//go:embed day_06/data.txt
|
|
||||||
var Day06Data string
|
|
||||||
|
130
day_06/data.txt
130
day_06/data.txt
@ -1,130 +0,0 @@
|
|||||||
............#.............#......................#....#....................................................#..............#.......
|
|
||||||
..................................#...#......#.......#........#..................#....#...........................##....#.........
|
|
||||||
...........#...........#...........#...#...#...............................#.......................##....................#........
|
|
||||||
..................#.............#........#.....................#...........................................................#......
|
|
||||||
....#............................................................#..#...#......#.#......#...#..........#..#.....#....#...#........
|
|
||||||
.................................#......##.............................#..........................#...#.........#.................
|
|
||||||
.#.............#.........#..#..............................................................................##...........#..#......
|
|
||||||
....#..............#..........................##......#........#...#........#..........#........#.............#...................
|
|
||||||
.....#.......#..#......#..#..................................................................#.........................#..........
|
|
||||||
......................#...............................#......#................................#......#.....#......................
|
|
||||||
.......#.........#.....................................................................#....#.#.....................#.........#...
|
|
||||||
........................#......##..........#....#..........................#....................#...................#.........#...
|
|
||||||
................#..#............#...#.................................................................#...........#...............
|
|
||||||
.#.....#.....................#..........................................#................................#........................
|
|
||||||
.............#.##........#......#......#......................#..................................................#...#..........#.
|
|
||||||
.......#.........#.#..........#.............#.......#............#.....................#..........................................
|
|
||||||
.............................#.........................................................#.........#.........#.............#........
|
|
||||||
....#..#.#..................................................#...........#..................#....#.................................
|
|
||||||
...............#..........#.....#..............................................................#..................................
|
|
||||||
.......##.............................................................................#...............#.........#.................
|
|
||||||
....#.....#..........................#..........#................................#....................#...#.......................
|
|
||||||
.#.................................................#..#.....#.....................................................................
|
|
||||||
...........................#..............#.#...#..........................................#......#..........................#....
|
|
||||||
.#....#........#....................................................#...........................................#.................
|
|
||||||
...#..................#................##...#.............#..#....................#...............#...............................
|
|
||||||
..................................................#......#..........................................................#...#.........
|
|
||||||
#.#....................................#........#.............................................................#...................
|
|
||||||
...............................................#.......##...............................................#.........................
|
|
||||||
...#.............#................................................................................................................
|
|
||||||
.......................................#....#..#........#............#..........#.........#...#..................#................
|
|
||||||
........#.................................................#.........................................#...........#.................
|
|
||||||
...................#...........#...............................#..........................#................#..#..........#.....#..
|
|
||||||
...............#......#...............................................................#............................##..#.....##...
|
|
||||||
.........................................##.#..#..................#.......#.....................#.................................
|
|
||||||
...........#...................................................................#...#..................................#.....#.....
|
|
||||||
......#...............................#.......###..........................#....#.....................................#...........
|
|
||||||
.....##...............................#...................................................#................................#...#..
|
|
||||||
..................#....#.......#.......................................................................##.........................
|
|
||||||
.....#..#.................................................................................................#.......................
|
|
||||||
.#.................................#......................##....................................#..........................##.....
|
|
||||||
#.#................#......................................#...........................#.......#...................................
|
|
||||||
.................................#.......#.....#..............................................##..#....................#..........
|
|
||||||
............#...............................................................................#....#...........#..........#.........
|
|
||||||
................................................##...............................................#..#..........................#..
|
|
||||||
.................#.......#........................................................................#...............................
|
|
||||||
..#.........................#......#.............#.......................................#........................................
|
|
||||||
...............#.............#...........................................................#.....#..................................
|
|
||||||
.........#...............................................................................#...............................#........
|
|
||||||
.........................................#...................#.................#.........##........#.........#....................
|
|
||||||
............#..........#.....#................................................................#...##............................#.
|
|
||||||
........#...#....#..............................................#..............#........................#.........................
|
|
||||||
..#.....................................................................................................................#.........
|
|
||||||
.#....#.............................^................#..#...................#.........................#...........................
|
|
||||||
..........................................................................#.........#....#......#.................................
|
|
||||||
.........#................................#.#........................#.................#...................................#......
|
|
||||||
.............................................#.##............................................................#......#..#.........#
|
|
||||||
..........#............#...........#..#......#..........#.........................................................................
|
|
||||||
......................#....................................#....#.............##....#.............................................
|
|
||||||
......................#...#............#.......................................................................................#..
|
|
||||||
.##.................#.......................#...............#.............#......#..................................#.....#.#.....
|
|
||||||
....................#........#.......................#....................................#.......#...#.......#.......#......#....
|
|
||||||
.............................................................#.......................#..........................#.....#...........
|
|
||||||
...#.................#.......................................................#.......##...........#.......#.......................
|
|
||||||
...............##.........................#.........................................................#.....#.......#...............
|
|
||||||
.......................#.......................#.........#..............#....................#..........#........................#
|
|
||||||
...................................##..........#..........................................................#.........#.............
|
|
||||||
..................#...............................#...................................#.......#.......................#...........
|
|
||||||
.......................#..#...................................#........................#........#............#....................
|
|
||||||
...................................#....#............#..................#........................................#..........#.....
|
|
||||||
.............#...#...#......#.............##...............................................#..........................#....#......
|
|
||||||
....................#..........................................#....#......................................................#......
|
|
||||||
............#.........#...............#..#.....................................................................#.#................
|
|
||||||
.......................#..#..............................................................#.....#........#..#..................#...
|
|
||||||
.............#..#..............................#.....#.................#.............#.......................#................#...
|
|
||||||
...............#......#...........#............#.........#......................................#.................................
|
|
||||||
............................#.....#.........#.....#..#...........#................................................................
|
|
||||||
....#.......#.................#..............................................#.......#.#.............................#...#..#.....
|
|
||||||
..................................................#.#......#............................................................#.........
|
|
||||||
..................................#............#........................#.......#.........#...#...............#...................
|
|
||||||
............##....#.#......#..............................................................................#..............#........
|
|
||||||
.....#..........................................#.........................................................#........##.............
|
|
||||||
............#.....................#..#.#..............#........#......#.............................#....#...#....................
|
|
||||||
.......................#...........................................................................#..............................
|
|
||||||
......#.........................................#........#............#..#..............................#.#.......................
|
|
||||||
#.#......#..#...............#...........#....#.................................................#..................................
|
|
||||||
.........#...........#............................#..............#......................#.......................................#.
|
|
||||||
...#............#..................................#..............#............................#.........................#........
|
|
||||||
...##..#.....................................................................................#...........................#........
|
|
||||||
...............#......#.........#................................#...#............#....................#..........................
|
|
||||||
..........................#...........................#..............................................#................#....#......
|
|
||||||
.........#........#.#...........................................................................#..#..............................
|
|
||||||
.................#..............#........................................................................#.................#......
|
|
||||||
...#.........#.#.........#.#...........................................#........#........#.........#..............................
|
|
||||||
..........#............................................................#..........#.....#.........................................
|
|
||||||
..................................#.....#...........##........#......#..........#..........................#......................
|
|
||||||
...........................#....#.............#............................................................#......................
|
|
||||||
.............#.......#...................................#.....#............................................................##....
|
|
||||||
............#..............##.............#.....#...............#..........#........................#.........................#...
|
|
||||||
..##.....................................................................................................#........................
|
|
||||||
.............#.#...........#...................#....#...#......#................##................................................
|
|
||||||
.................#.#............#........................#.......##..#.........##.................................................
|
|
||||||
......................#.#...#................................................................#...............#.....#...........#..
|
|
||||||
#.................#..........................#.............................##.....#...........................................#...
|
|
||||||
..............#.........#.................#................................................................................#......
|
|
||||||
....#.#............#.......#.......................#.....#.......................#....................#.....#.............#.......
|
|
||||||
......................#..#....#.........................................#.#............##............#............................
|
|
||||||
.........#.#...#...#........................................#...............................................#....................#
|
|
||||||
.......................#...................#.............................................................#.......................#
|
|
||||||
................#............................#........#..........#.......................##....#..................................
|
|
||||||
.#...................#......................#.......................................#............................#................
|
|
||||||
..#..##.......#......#..........#..#..................................................#...........................................
|
|
||||||
..............#..........#...........................................#.........##.................................................
|
|
||||||
....#..............................#.........#...#.................#......#.#.......#.............................................
|
|
||||||
#.................................#........#.....................................#...##...................#..#...........#........
|
|
||||||
.......#.....#.............................................#..................#...................#...........#..#........#.......
|
|
||||||
.......................#................#.............#.#.........#..............................................................#
|
|
||||||
......................................................#....................#..........#................................#..........
|
|
||||||
#...............#...#.............#.................#.................................................#...........................
|
|
||||||
..................................................................#.........................#......#.........#............##......
|
|
||||||
#.................#........#.......#...#....................................#.#...................................................
|
|
||||||
.....................................#...#....#.........#................#.........................................#...#..........
|
|
||||||
............#............................#.............#.#........................................................................
|
|
||||||
......................................#.................................................#...............#.......#.................
|
|
||||||
.#.#..........#........#.......................#..................................................#....................#..........
|
|
||||||
....#...............................................................#...........#............#.......................#............
|
|
||||||
....#.......#.#..#...................#.........#..................................#....................................#..........
|
|
||||||
..............................................#...................................##.......#......................................
|
|
||||||
#...#.#............#...............................#.........#...........#..........#............................................#
|
|
||||||
..#...........#..#..........#.......................#..........##.................................................................
|
|
||||||
...#.....#..................................#................................................#....................................
|
|
@ -1,10 +0,0 @@
|
|||||||
....#.....
|
|
||||||
.........#
|
|
||||||
..........
|
|
||||||
..#.......
|
|
||||||
.......#..
|
|
||||||
..........
|
|
||||||
.#..^.....
|
|
||||||
........#.
|
|
||||||
#.........
|
|
||||||
......#...
|
|
@ -1,161 +0,0 @@
|
|||||||
package day_06
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Direction int
|
|
||||||
|
|
||||||
const (
|
|
||||||
UP Direction = iota
|
|
||||||
RIGHT Direction = iota
|
|
||||||
DOWN Direction = iota
|
|
||||||
LEFT Direction = iota
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
VISITED = 'X'
|
|
||||||
OCCUPIED = '#'
|
|
||||||
FREE = '.'
|
|
||||||
)
|
|
||||||
|
|
||||||
var toDirections = map[rune]Direction{
|
|
||||||
'^': UP,
|
|
||||||
'>': RIGHT,
|
|
||||||
'v': DOWN,
|
|
||||||
'<': LEFT,
|
|
||||||
}
|
|
||||||
|
|
||||||
var fromDirections = map[Direction]rune{
|
|
||||||
UP: '^',
|
|
||||||
RIGHT: '>',
|
|
||||||
DOWN: 'v',
|
|
||||||
LEFT: '<',
|
|
||||||
}
|
|
||||||
|
|
||||||
// task:https://adventofcode.com/2024/day/5
|
|
||||||
// short summary - count visited steps
|
|
||||||
func SolveBasic(input string) int {
|
|
||||||
grid := visitationGrid(input)
|
|
||||||
|
|
||||||
sum := 0
|
|
||||||
for _, row := range grid {
|
|
||||||
for _, value := range row {
|
|
||||||
if value == VISITED {
|
|
||||||
sum++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sum
|
|
||||||
}
|
|
||||||
|
|
||||||
// task:https://adventofcode.com/2024/day/5#part2
|
|
||||||
// short summary - find all position that will create an infinite loop
|
|
||||||
func SolveComplex(input string) int {
|
|
||||||
grid := visitationGrid(input)
|
|
||||||
|
|
||||||
dirGrid := loadGrid(input)
|
|
||||||
startX, startY, startDir := findPlayer(dirGrid)
|
|
||||||
dirGrid[startY][startX] = FREE
|
|
||||||
|
|
||||||
positions := 0
|
|
||||||
|
|
||||||
for y, row := range grid {
|
|
||||||
for x, value := range row {
|
|
||||||
if value == VISITED && dirGrid[y][x] == FREE {
|
|
||||||
dirGrid[y][x] = OCCUPIED
|
|
||||||
|
|
||||||
curX := startX
|
|
||||||
curY := startY
|
|
||||||
curDir := startDir
|
|
||||||
|
|
||||||
for inGrid(dirGrid, curX, curY) {
|
|
||||||
if dirGrid[curY][curX] == fromDirections[curDir] {
|
|
||||||
// we visited a same point second time in the same direction
|
|
||||||
// which means we are in an infinite list
|
|
||||||
positions++
|
|
||||||
break
|
|
||||||
}
|
|
||||||
dirGrid[curY][curX] = fromDirections[curDir]
|
|
||||||
curX, curY, curDir = nextSpot(dirGrid, curX, curY, curDir)
|
|
||||||
|
|
||||||
}
|
|
||||||
dirGrid[y][x] = FREE
|
|
||||||
clearDirGrid(dirGrid)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return positions
|
|
||||||
}
|
|
||||||
|
|
||||||
func clearDirGrid(grid [][]rune) {
|
|
||||||
for y, row := range grid {
|
|
||||||
for x, value := range row {
|
|
||||||
if value != OCCUPIED {
|
|
||||||
grid[y][x] = FREE
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func nextSpot(grid [][]rune, x int, y int, dir Direction) (int, int, Direction) {
|
|
||||||
for {
|
|
||||||
nx, ny := calculateNext(x, y, dir)
|
|
||||||
if !inGrid(grid, nx, ny) {
|
|
||||||
return nx, ny, dir
|
|
||||||
} else if grid[ny][nx] != OCCUPIED {
|
|
||||||
return nx, ny, dir
|
|
||||||
} else {
|
|
||||||
dir = (dir + 1) % 4
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func calculateNext(x int, y int, dir Direction) (int, int) {
|
|
||||||
switch dir {
|
|
||||||
case UP:
|
|
||||||
return x, y - 1
|
|
||||||
case RIGHT:
|
|
||||||
return x + 1, y
|
|
||||||
case DOWN:
|
|
||||||
return x, y + 1
|
|
||||||
case LEFT:
|
|
||||||
return x - 1, y
|
|
||||||
}
|
|
||||||
panic("unknown direction")
|
|
||||||
}
|
|
||||||
|
|
||||||
func inGrid(grid [][]rune, x int, y int) bool {
|
|
||||||
return x >= 0 && y >= 0 && x < len(grid[0]) && y < len(grid)
|
|
||||||
}
|
|
||||||
|
|
||||||
func findPlayer(grid [][]rune) (int, int, Direction) {
|
|
||||||
for y, row := range grid {
|
|
||||||
for x, value := range row {
|
|
||||||
if dir, present := toDirections[value]; present {
|
|
||||||
return x, y, dir
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
panic("Couldn't find player")
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadGrid(input string) [][]rune {
|
|
||||||
substrings := strings.Split(input, "\n")
|
|
||||||
var grid [][]rune
|
|
||||||
for _, line := range substrings {
|
|
||||||
grid = append(grid, []rune(line))
|
|
||||||
}
|
|
||||||
return grid
|
|
||||||
}
|
|
||||||
|
|
||||||
func visitationGrid(input string) [][]rune {
|
|
||||||
grid := loadGrid(input)
|
|
||||||
x, y, dir := findPlayer(grid)
|
|
||||||
|
|
||||||
for inGrid(grid, x, y) {
|
|
||||||
grid[y][x] = VISITED
|
|
||||||
x, y, dir = nextSpot(grid, x, y, dir)
|
|
||||||
}
|
|
||||||
return grid
|
|
||||||
}
|
|
@ -1,24 +0,0 @@
|
|||||||
package day_06_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"advent_of_code_2024/day_06"
|
|
||||||
_ "embed"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
//go:embed example_data.txt
|
|
||||||
var ExampleData string
|
|
||||||
|
|
||||||
func TestBasicSolutionExample(t *testing.T) {
|
|
||||||
result := day_06.SolveBasic(ExampleData)
|
|
||||||
if result != 41 {
|
|
||||||
t.Fatalf("Expected 41 received %d", result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestComplexSolutionExample(t *testing.T) {
|
|
||||||
result := day_06.SolveComplex(ExampleData)
|
|
||||||
if result != 6 {
|
|
||||||
t.Fatalf("Expected 6 received %d", result)
|
|
||||||
}
|
|
||||||
}
|
|
5
main.go
5
main.go
@ -6,7 +6,6 @@ import (
|
|||||||
"advent_of_code_2024/day_03"
|
"advent_of_code_2024/day_03"
|
||||||
"advent_of_code_2024/day_04"
|
"advent_of_code_2024/day_04"
|
||||||
"advent_of_code_2024/day_05"
|
"advent_of_code_2024/day_05"
|
||||||
"advent_of_code_2024/day_06"
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -36,10 +35,6 @@ func main() {
|
|||||||
fmt.Printf("Day 05. Basic: %d\n", day_05.SolveBasic(Day05Data)) // 2560
|
fmt.Printf("Day 05. Basic: %d\n", day_05.SolveBasic(Day05Data)) // 2560
|
||||||
case "05-complex":
|
case "05-complex":
|
||||||
fmt.Printf("Day 05. Complex: %d\n", day_05.SolveComplex(Day05Data)) // 63013756
|
fmt.Printf("Day 05. Complex: %d\n", day_05.SolveComplex(Day05Data)) // 63013756
|
||||||
case "06-basic":
|
|
||||||
fmt.Printf("Day 06. Basic: %d\n", day_06.SolveBasic(Day06Data)) //
|
|
||||||
case "06-complex":
|
|
||||||
fmt.Printf("Day 06. Complex: %d\n", day_06.SolveComplex(Day06Data)) //
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user