diff --git a/data.go b/data.go index 2b60485..a53619b 100644 --- a/data.go +++ b/data.go @@ -18,3 +18,6 @@ var Day04Data string //go:embed day_05/data.txt var Day05Data string + +//go:embed day_06/data.txt +var Day06Data string diff --git a/day_06/data.txt b/day_06/data.txt new file mode 100644 index 0000000..39dc241 --- /dev/null +++ b/day_06/data.txt @@ -0,0 +1,130 @@ +............#.............#......................#....#....................................................#..............#....... +..................................#...#......#.......#........#..................#....#...........................##....#......... +...........#...........#...........#...#...#...............................#.......................##....................#........ +..................#.............#........#.....................#...........................................................#...... +....#............................................................#..#...#......#.#......#...#..........#..#.....#....#...#........ +.................................#......##.............................#..........................#...#.........#................. +.#.............#.........#..#..............................................................................##...........#..#...... +....#..............#..........................##......#........#...#........#..........#........#.............#................... +.....#.......#..#......#..#..................................................................#.........................#.......... +......................#...............................#......#................................#......#.....#...................... +.......#.........#.....................................................................#....#.#.....................#.........#... +........................#......##..........#....#..........................#....................#...................#.........#... +................#..#............#...#.................................................................#...........#............... +.#.....#.....................#..........................................#................................#........................ +.............#.##........#......#......#......................#..................................................#...#..........#. +.......#.........#.#..........#.............#.......#............#.....................#.......................................... +.............................#.........................................................#.........#.........#.............#........ +....#..#.#..................................................#...........#..................#....#................................. +...............#..........#.....#..............................................................#.................................. +.......##.............................................................................#...............#.........#................. +....#.....#..........................#..........#................................#....................#...#....................... +.#.................................................#..#.....#..................................................................... +...........................#..............#.#...#..........................................#......#..........................#.... +.#....#........#....................................................#...........................................#................. +...#..................#................##...#.............#..#....................#...............#............................... +..................................................#......#..........................................................#...#......... +#.#....................................#........#.............................................................#................... +...............................................#.......##...............................................#......................... +...#.............#................................................................................................................ +.......................................#....#..#........#............#..........#.........#...#..................#................ +........#.................................................#.........................................#...........#................. +...................#...........#...............................#..........................#................#..#..........#.....#.. +...............#......#...............................................................#............................##..#.....##... +.........................................##.#..#..................#.......#.....................#................................. +...........#...................................................................#...#..................................#.....#..... +......#...............................#.......###..........................#....#.....................................#........... +.....##...............................#...................................................#................................#...#.. +..................#....#.......#.......................................................................##......................... +.....#..#.................................................................................................#....................... +.#.................................#......................##....................................#..........................##..... +#.#................#......................................#...........................#.......#................................... +.................................#.......#.....#..............................................##..#....................#.......... +............#...............................................................................#....#...........#..........#......... +................................................##...............................................#..#..........................#.. +.................#.......#........................................................................#............................... +..#.........................#......#.............#.......................................#........................................ +...............#.............#...........................................................#.....#.................................. +.........#...............................................................................#...............................#........ +.........................................#...................#.................#.........##........#.........#.................... +............#..........#.....#................................................................#...##............................#. +........#...#....#..............................................#..............#........................#......................... +..#.....................................................................................................................#......... +.#....#.............................^................#..#...................#.........................#........................... +..........................................................................#.........#....#......#................................. +.........#................................#.#........................#.................#...................................#...... +.............................................#.##............................................................#......#..#.........# +..........#............#...........#..#......#..........#......................................................................... +......................#....................................#....#.............##....#............................................. +......................#...#............#.......................................................................................#.. +.##.................#.......................#...............#.............#......#..................................#.....#.#..... +....................#........#.......................#....................................#.......#...#.......#.......#......#.... +.............................................................#.......................#..........................#.....#........... +...#.................#.......................................................#.......##...........#.......#....................... +...............##.........................#.........................................................#.....#.......#............... +.......................#.......................#.........#..............#....................#..........#........................# +...................................##..........#..........................................................#.........#............. +..................#...............................#...................................#.......#.......................#........... +.......................#..#...................................#........................#........#............#.................... +...................................#....#............#..................#........................................#..........#..... +.............#...#...#......#.............##...............................................#..........................#....#...... +....................#..........................................#....#......................................................#...... +............#.........#...............#..#.....................................................................#.#................ +.......................#..#..............................................................#.....#........#..#..................#... +.............#..#..............................#.....#.................#.............#.......................#................#... +...............#......#...........#............#.........#......................................#................................. +............................#.....#.........#.....#..#...........#................................................................ +....#.......#.................#..............................................#.......#.#.............................#...#..#..... +..................................................#.#......#............................................................#......... +..................................#............#........................#.......#.........#...#...............#................... +............##....#.#......#..............................................................................#..............#........ +.....#..........................................#.........................................................#........##............. +............#.....................#..#.#..............#........#......#.............................#....#...#.................... +.......................#...........................................................................#.............................. +......#.........................................#........#............#..#..............................#.#....................... +#.#......#..#...............#...........#....#.................................................#.................................. +.........#...........#............................#..............#......................#.......................................#. +...#............#..................................#..............#............................#.........................#........ +...##..#.....................................................................................#...........................#........ +...............#......#.........#................................#...#............#....................#.......................... +..........................#...........................#..............................................#................#....#...... +.........#........#.#...........................................................................#..#.............................. +.................#..............#........................................................................#.................#...... +...#.........#.#.........#.#...........................................#........#........#.........#.............................. +..........#............................................................#..........#.....#......................................... +..................................#.....#...........##........#......#..........#..........................#...................... +...........................#....#.............#............................................................#...................... +.............#.......#...................................#.....#............................................................##.... +............#..............##.............#.....#...............#..........#........................#.........................#... +..##.....................................................................................................#........................ +.............#.#...........#...................#....#...#......#................##................................................ +.................#.#............#........................#.......##..#.........##................................................. +......................#.#...#................................................................#...............#.....#...........#.. +#.................#..........................#.............................##.....#...........................................#... +..............#.........#.................#................................................................................#...... +....#.#............#.......#.......................#.....#.......................#....................#.....#.............#....... +......................#..#....#.........................................#.#............##............#............................ +.........#.#...#...#........................................#...............................................#....................# +.......................#...................#.............................................................#.......................# +................#............................#........#..........#.......................##....#.................................. +.#...................#......................#.......................................#............................#................ +..#..##.......#......#..........#..#..................................................#........................................... +..............#..........#...........................................#.........##................................................. +....#..............................#.........#...#.................#......#.#.......#............................................. +#.................................#........#.....................................#...##...................#..#...........#........ +.......#.....#.............................................#..................#...................#...........#..#........#....... +.......................#................#.............#.#.........#..............................................................# +......................................................#....................#..........#................................#.......... +#...............#...#.............#.................#.................................................#........................... +..................................................................#.........................#......#.........#............##...... +#.................#........#.......#...#....................................#.#................................................... +.....................................#...#....#.........#................#.........................................#...#.......... +............#............................#.............#.#........................................................................ +......................................#.................................................#...............#.......#................. +.#.#..........#........#.......................#..................................................#....................#.......... +....#...............................................................#...........#............#.......................#............ +....#.......#.#..#...................#.........#..................................#....................................#.......... +..............................................#...................................##.......#...................................... +#...#.#............#...............................#.........#...........#..........#............................................# +..#...........#..#..........#.......................#..........##................................................................. +...#.....#..................................#................................................#.................................... \ No newline at end of file diff --git a/day_06/example_data.txt b/day_06/example_data.txt new file mode 100644 index 0000000..b60e466 --- /dev/null +++ b/day_06/example_data.txt @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... \ No newline at end of file diff --git a/day_06/solution.go b/day_06/solution.go new file mode 100644 index 0000000..571a00e --- /dev/null +++ b/day_06/solution.go @@ -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 +} diff --git a/day_06/solution_test.go b/day_06/solution_test.go new file mode 100644 index 0000000..943dfc1 --- /dev/null +++ b/day_06/solution_test.go @@ -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) + } +} diff --git a/main.go b/main.go index 3f6b313..3009826 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "advent_of_code_2024/day_03" "advent_of_code_2024/day_04" "advent_of_code_2024/day_05" + "advent_of_code_2024/day_06" _ "embed" "fmt" "os" @@ -35,6 +36,10 @@ func main() { fmt.Printf("Day 05. Basic: %d\n", day_05.SolveBasic(Day05Data)) // 2560 case "05-complex": 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)) // } }