diff --git a/2024/02.odin b/2024/02.odin index a72f56b..5307f29 100644 --- a/2024/02.odin +++ b/2024/02.odin @@ -1,14 +1,65 @@ package aoc import "core:fmt" -import "core:log" -import "core:os" +import "core:math" +import "core:strconv" +import "core:strings" day02 :: proc() { - input := slurp_lines("input/02") + input := slurp_lines("input/02") defer delete(input) - + + p1: int + p2: int for line in input { - + sp := strings.split(line, " ") + report := make([dynamic]int, 0, len(sp)) + for s in sp { + i, _ := strconv.parse_int(s) + append(&report, i) + } + if validate(report) { + p1 += 1 + } + + p2_valid := true + for i := 0; i < len(report); i += 1 { + cpy := make([dynamic]int, 0, len(report)) + copy(cpy[:], report[:]) + ordered_remove(&cpy, i) + if !validate(cpy) { + p2_valid = false + delete(cpy) + break + } + delete(cpy) + } + if p2_valid { + p2 += 1 + } + delete(report) } + fmt.println(p1) + fmt.println(p2) +} + +validate :: proc(report: [dynamic]int) -> bool { + increasing := false + if report[0] < report[1] { + increasing = true + } + + for i := 1; i < len(report); i += 1 { + if increasing && report[i - 1] > report[i] { + return false + } + if !increasing && report[i - 1] < report[i] { + return false + } + diff := math.abs(report[i - 1] - report[i]) + if diff < 1 || diff > 3 { + return false + } + } + return true } diff --git a/2024/2024 b/2024/2024 new file mode 100755 index 0000000..166b551 Binary files /dev/null and b/2024/2024 differ diff --git a/2025/02.rb b/2025/02.rb new file mode 100644 index 0000000..15d8bd1 --- /dev/null +++ b/2025/02.rb @@ -0,0 +1,30 @@ +def valid(str, p2) + (1..str.size / 2).each do |i| + sub = str[0, i] + return false if str[i..] == sub + + if p2 && str.size > 2 + chunks = str[i..].chars.each_slice(sub.size).map(&:join) + puts chunks + end + end + true +end + +input = File.read('input/test').split(',') +p1 = 0 +p2 = 0 + +input.each do |line| + sp = line.split('-') + a = sp[0].to_i + b = sp[1].to_i + + (a..b).each do |i| + p1 += i unless valid(i.to_s, false) + p2 += i unless valid(i.to_s, true) + end +end + +puts p1 +puts p2 diff --git a/2025/input/02 b/2025/input/02 new file mode 100644 index 0000000..76ef783 --- /dev/null +++ b/2025/input/02 @@ -0,0 +1 @@ +9191896883-9191940271,457499-518693,4952-6512,960-1219,882220-1039699,2694-3465,3818-4790,166124487-166225167,759713819-759869448,4821434-4881387,7271-9983,1182154-1266413,810784-881078,802-958,1288-1491,45169-59445,25035-29864,379542-433637,287-398,75872077-75913335,653953-689335,168872-217692,91-113,475-590,592-770,310876-346156,2214325-2229214,85977-112721,51466993-51620441,8838997-8982991,534003-610353,32397-42770,17-27,68666227-68701396,1826294188-1826476065,1649-2195,141065204-141208529,7437352-7611438,10216-13989,33-44,1-16,49-74,60646-73921,701379-808878 \ No newline at end of file