Skip to content

Commit

Permalink
feat: Implement 2024 day 2 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
obalunenko committed Dec 18, 2024
1 parent 6451ed4 commit acdda10
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions internal/puzzles/solutions/2024/day02/solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
package day02

import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"

"github.com/obalunenko/advent-of-code/internal/puzzles"
"github.com/obalunenko/advent-of-code/internal/puzzles/common/utils"
)

func init() {
Expand All @@ -22,9 +27,67 @@ func (s solution) Day() string {
}

func (s solution) Part1(input io.Reader) (string, error) {
return "", puzzles.ErrNotImplemented
scanner := bufio.NewScanner(input)

isSafe := func(line []int) bool {
var asc, desc bool

for i, val := range line {
if i == 0 {
continue
}

if line[i-1] < val {
if desc {
return false
}

asc = true
}

if line[i-1] > val {
if asc {
return false
}

desc = true
}

diff := val - line[i-1]
if diff < 0 {
diff = -diff
}

if diff < 1 || diff > 3 {
return false
}
}

return true
}

var safeCount int

for scanner.Scan() {
line := scanner.Bytes()

numbers, err := utils.ParseInts(bytes.NewReader(line), " ")
if err != nil {
return "", fmt.Errorf("failed to parse input line: %w", err)
}

if isSafe(numbers) {
safeCount++
}
}

if err := scanner.Err(); err != nil {
return "", fmt.Errorf("failed to read input: %w", err)
}

return strconv.Itoa(safeCount), nil
}

func (s solution) Part2(input io.Reader) (string, error) {
func (s solution) Part2(_ io.Reader) (string, error) {
return "", puzzles.ErrNotImplemented
}

0 comments on commit acdda10

Please sign in to comment.