Skip to content

Commit

Permalink
2024 day 6 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Redband authored and timredband committed Dec 12, 2024
1 parent 951f419 commit 060fbbc
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 4 deletions.
3 changes: 0 additions & 3 deletions .gitconfig

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions 2024/go/inputs/day6/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
130 changes: 130 additions & 0 deletions 2024/go/inputs/day6/input.txt

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions 2024/go/pkg/day6/day6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package day6

import (
"errors"
"fmt"
"log"
"os"
)

func Execute(part string, file *os.File) (int, error) {
switch part {
case "1":
return Part1(file), nil
case "2":
return Part2(file), nil
default:
log.Fatalf("unknown part: %s", part)
return 0, errors.New(fmt.Sprintf("unknown part: %s", part))
}
}
109 changes: 109 additions & 0 deletions 2024/go/pkg/day6/part1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package day6

import (
"os"

"strings"

"github.com/timredband/advent-of-code/pkg/utils"
)

type Position struct {
i int
j int
direction string
}

func Part1(file *os.File) int {
inputs := utils.ReadFile(file)

board := make([][]string, len(inputs))

position := Position{direction: "north"}
maxPosition := len(inputs) - 1

for i, v := range inputs {
line := strings.Split(v, "")
board[i] = line

for j, c := range line {
if c == "^" {
position.i = i
position.j = j
}
}
}

for position.i > 0 && position.j < len(inputs) {
board[position.i][position.j] = "X"

if position.direction == "north" {
if position.i-1 < 0 {
break
}

if board[position.i-1][position.j] == "#" {
position.direction = "east"
} else {
position.i -= 1
}

continue
}

if position.direction == "south" {
if position.i+1 > maxPosition {
break
}

if board[position.i+1][position.j] == "#" {
position.direction = "west"
} else {
position.i += 1
}

continue
}

if position.direction == "east" {
if position.j+1 > maxPosition {
break
}

if board[position.i][position.j+1] == "#" {
position.direction = "south"
} else {
position.j += 1
}

continue

}

if position.direction == "west" {
if position.j-1 < 0 {
break
}

if board[position.i][position.j-1] == "#" {
position.direction = "north"
} else {
position.j -= 1
}

continue
}
}

result := 0

for _, row := range board {
for _, v := range row {
if v == "X" {
result += 1
}
}
}

return result
}
7 changes: 7 additions & 0 deletions 2024/go/pkg/day6/part2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package day6

import (
"os"
)

func Part2(file *os.File) int { return 0 }
3 changes: 3 additions & 0 deletions 2024/go/pkg/days/days.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/timredband/advent-of-code/pkg/day3"
"github.com/timredband/advent-of-code/pkg/day4"
"github.com/timredband/advent-of-code/pkg/day5"
"github.com/timredband/advent-of-code/pkg/day6"
)

func Execute(day string, part string, file *os.File) (int, error) {
Expand All @@ -24,6 +25,8 @@ func Execute(day string, part string, file *os.File) (int, error) {
return day4.Execute(part, file)
case "5":
return day5.Execute(part, file)
case "6":
return day6.Execute(part, file)
default:
return 0, errors.New(fmt.Sprintf("unknown day: %s", day))
}
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# advent-of-code
asadasd# advent-of-code

0 comments on commit 060fbbc

Please sign in to comment.