Day02
This commit is contained in:
76
2024/gareth/day02/day02.go
Normal file
76
2024/gareth/day02/day02.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package day02
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Part1(input string) int {
|
||||
total := 0
|
||||
lines := strings.Split(input, "\n")
|
||||
for _, line := range lines {
|
||||
var report []int
|
||||
nums := strings.Fields(line)
|
||||
for _, num := range nums {
|
||||
intnum, _ := strconv.Atoi(num)
|
||||
report = append(report, int(intnum))
|
||||
}
|
||||
if isSafe(report) {
|
||||
total++
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
total := 0
|
||||
lines := strings.Split(input, "\n")
|
||||
for _, line := range lines {
|
||||
var report []int
|
||||
nums := strings.Fields(line)
|
||||
for _, num := range nums {
|
||||
intnum, _ := strconv.Atoi(num)
|
||||
report = append(report, int(intnum))
|
||||
}
|
||||
if isSafeWithDampener(report) {
|
||||
total++
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func isSafe(report []int) bool {
|
||||
increasing := report[1] > report[0]
|
||||
decreasing := report[1] < report[0]
|
||||
|
||||
for i := 1; i < len(report); i++ {
|
||||
diff := report[i] - report[i-1]
|
||||
absDiff := int(math.Abs(float64(diff)))
|
||||
|
||||
if absDiff < 1 || absDiff > 3 {
|
||||
return false
|
||||
}
|
||||
|
||||
if (diff > 0 && !increasing) || (diff < 0 && !decreasing) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func isSafeWithDampener(report []int) bool {
|
||||
if isSafe(report) {
|
||||
return true
|
||||
}
|
||||
|
||||
for i := 0; i < len(report); i++ {
|
||||
modifiedReport := append(report[:i], report[i+1:]...)
|
||||
if isSafe(modifiedReport) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
27
2024/gareth/day02/day02_test.go
Normal file
27
2024/gareth/day02/day02_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package day02
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
r := Part1(`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`)
|
||||
assert.Equal(t, 2, r)
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
r := Part2(`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`)
|
||||
assert.Equal(t, 4, r)
|
||||
}
|
||||
1000
2024/gareth/day02/input.txt
Normal file
1000
2024/gareth/day02/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
45
2024/gareth/day02/poc.py
Normal file
45
2024/gareth/day02/poc.py
Normal file
@@ -0,0 +1,45 @@
|
||||
def is_safe(report):
|
||||
increasing = report[1] > report[0]
|
||||
decreasing = report[1] < report[0]
|
||||
|
||||
for i in range(1, len(report)):
|
||||
diff = report[i] - report[i - 1]
|
||||
abs_diff = abs(diff)
|
||||
if abs_diff < 1 or abs_diff > 3:
|
||||
return False
|
||||
if (diff > 0 and not increasing) or (diff < 0 and not decreasing):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def is_safe_with_dampener(report):
|
||||
if is_safe(report):
|
||||
return True
|
||||
for i in range(len(report)):
|
||||
modified_report = report[:i] + report[i+1:]
|
||||
if is_safe(modified_report):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def read_reports_from_file(filename):
|
||||
reports = []
|
||||
with open(filename, 'r') as file:
|
||||
for line in file:
|
||||
report = list(map(int, line.split()))
|
||||
reports.append(report)
|
||||
return reports
|
||||
|
||||
def main():
|
||||
filename = 'input.txt'
|
||||
reports = read_reports_from_file(filename)
|
||||
|
||||
safe_count = 0
|
||||
for report in reports:
|
||||
if is_safe_with_dampener(report):
|
||||
safe_count += 1
|
||||
|
||||
print("OUTPUT:", safe_count)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,13 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"aoc2024/day01"
|
||||
"aoc2024/day02"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
data, _ := os.ReadFile("day01/input.txt")
|
||||
fmt.Printf("part 1: %d\n", day01.Part1(string(data)))
|
||||
fmt.Printf("part 2: %d\n", day01.Part2(string(data)))
|
||||
data, _ := os.ReadFile("day02/input.txt")
|
||||
fmt.Printf("part 1: %d\n", day02.Part1(string(data)))
|
||||
fmt.Printf("part 2: %d\n", day02.Part2(string(data)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user