Day 06. part 1

This commit is contained in:
Borna Rajković 2024-12-06 22:41:21 +01:00
parent 2b14d3a742
commit 4d8dd29a27
6 changed files with 277 additions and 0 deletions

View File

@ -18,3 +18,6 @@ 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 Normal file
View File

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

10
day_06/example_data.txt Normal file
View File

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

105
day_06/solution.go Normal file
View File

@ -0,0 +1,105 @@
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 = '#'
)
var directions = map[rune]Direction{
'^': UP,
'>': RIGHT,
'v': DOWN,
'<': LEFT,
}
// task:https://adventofcode.com/2024/day/5
// short summary - count visited steps
func SolveBasic(input string) int {
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)
}
sum := 0
for _, row := range grid {
for _, value := range row {
if value == VISITED {
sum++
}
}
}
return sum
}
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 := directions[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
}
// task:https://adventofcode.com/2024/day/5#part2
// short summary - fix and count invalid columns
func SolveComplex(input string) int {
return 0
}

24
day_06/solution_test.go Normal file
View File

@ -0,0 +1,24 @@
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 != 123 {
t.Fatalf("Expected 123 received %d", result)
}
}

View File

@ -6,6 +6,7 @@ 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"
@ -35,6 +36,10 @@ 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)) //
} }
} }