feat: add day 2 2024
This commit is contained in:
parent
b1e4c4d634
commit
2274eca981
6
aoc2024/day02/example.txt
Normal file
6
aoc2024/day02/example.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
1000
aoc2024/day02/input.txt
Normal file
1000
aoc2024/day02/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
88
aoc2024/day02/main.go
Normal file
88
aoc2024/day02/main.go
Normal file
|
@ -0,0 +1,88 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
aoc "go.sour.is/advent-of-code"
|
||||
)
|
||||
|
||||
// var log = aoc.Log
|
||||
|
||||
func main() { aoc.MustResult(aoc.Runner(run)) }
|
||||
|
||||
type result struct {
|
||||
valuePT1 int
|
||||
valuePT2 int
|
||||
}
|
||||
|
||||
func (r result) String() string { return fmt.Sprintf("%#v", r) }
|
||||
|
||||
func run(scan *bufio.Scanner) (*result, error) {
|
||||
|
||||
sum := 0
|
||||
sum2 := 0
|
||||
|
||||
for scan.Scan() {
|
||||
txt := scan.Text()
|
||||
row := aoc.ReadStringToInts(strings.Fields(txt))
|
||||
|
||||
good, bad := testSafety(row)
|
||||
if good {
|
||||
sum++
|
||||
sum2++
|
||||
continue
|
||||
}
|
||||
|
||||
for i := max(0, bad-1); i < min(bad+2, len(row)); i++ {
|
||||
arr := cut(i, row)
|
||||
|
||||
good, _ := testSafety(arr)
|
||||
if good {
|
||||
sum2++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &result{valuePT1: sum, valuePT2: sum2}, nil
|
||||
}
|
||||
|
||||
func testSafety(row []int) (bool, int) {
|
||||
good := true
|
||||
bad := -1
|
||||
increasing := false
|
||||
decreasing := false
|
||||
|
||||
for i, v := range row[1:] {
|
||||
if v > row[i] {
|
||||
increasing = true
|
||||
}
|
||||
if v < row[i] {
|
||||
decreasing = true
|
||||
}
|
||||
|
||||
if difference := aoc.ABS(v - row[i]); difference < 1 || difference > 3 {
|
||||
good = false
|
||||
bad = i
|
||||
break
|
||||
}
|
||||
|
||||
if increasing && decreasing {
|
||||
good = false
|
||||
bad = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return good, bad
|
||||
}
|
||||
|
||||
func cut(i int, values []int) []int {
|
||||
arr := make([]int, 0, len(values))
|
||||
arr = append(arr, values[:i]...)
|
||||
arr = append(arr, values[i+1:]...)
|
||||
return arr
|
||||
}
|
43
aoc2024/day02/main_test.go
Normal file
43
aoc2024/day02/main_test.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
_ "embed"
|
||||
|
||||
"github.com/matryer/is"
|
||||
)
|
||||
|
||||
//go:embed example.txt
|
||||
var example []byte
|
||||
|
||||
//go:embed input.txt
|
||||
var input []byte
|
||||
|
||||
func TestExample(t *testing.T) {
|
||||
is := is.New(t)
|
||||
scan := bufio.NewScanner(bytes.NewReader(example))
|
||||
|
||||
result, err := run(scan)
|
||||
is.NoErr(err)
|
||||
|
||||
t.Log(result)
|
||||
is.Equal(result.valuePT1, 2)
|
||||
is.Equal(result.valuePT2, 4)
|
||||
}
|
||||
|
||||
func TestSolution(t *testing.T) {
|
||||
is := is.New(t)
|
||||
scan := bufio.NewScanner(bytes.NewReader(input))
|
||||
|
||||
result, err := run(scan)
|
||||
is.NoErr(err)
|
||||
|
||||
t.Log(result)
|
||||
is.Equal(result.valuePT1, 486)
|
||||
is.True(result.valuePT2 > 517)
|
||||
is.True(result.valuePT2 > 523)
|
||||
is.Equal(result.valuePT2, 540)
|
||||
}
|
Loading…
Reference in New Issue
Block a user