Day 2 Part 2

This commit is contained in:
April Eaton 2025-12-02 18:28:42 +01:00
parent ccee86dc80
commit 4bd66215be
3 changed files with 91 additions and 3 deletions

1
day_2/input02.txt Normal file
View file

@ -0,0 +1 @@
24-46,124420-259708,584447-720297,51051-105889,6868562486-6868811237,55-116,895924-1049139,307156-347325,372342678-372437056,1791-5048,3172595555-3172666604,866800081-866923262,5446793-5524858,6077-10442,419-818,57540345-57638189,2143479-2274980,683602048-683810921,966-1697,56537997-56591017,1084127-1135835,1-14,2318887654-2318959425,1919154462-1919225485,351261-558210,769193-807148,4355566991-4355749498,809094-894510,11116-39985,9898980197-9898998927,99828221-99856128,9706624-9874989,119-335

View file

@ -1,6 +1,7 @@
import gleam/int import gleam/int
import gleam/io import gleam/io
import gleam/list import gleam/list
import gleam/pair
import gleam/result import gleam/result
import gleam/string import gleam/string
import simplifile import simplifile
@ -32,6 +33,34 @@ pub fn int_is_doubled_string(i: Int) -> Result(Bool, Nil) {
}) })
} }
pub fn digits_to_substrings(i: List(Int)) -> List(List(Int)) {
list.map(i, fn(d) { [d] })
|> list.map_fold([], fn(acc, d) {
#(
list.append(acc, [list.append(list.last(acc) |> result.unwrap([]), d)]),
list.append(list.last(acc) |> result.unwrap([]), d),
)
})
|> pair.first
|> list.filter(fn(l) { l != i })
}
pub fn can_repeat_into(l: List(a), r: List(a)) -> Bool {
{ list.length(r) % list.length(l) } == 0
}
pub fn repeats_into(s: List(Int), f: List(Int)) -> Bool {
list.repeat(s, { list.length(f) / list.length(s) }) |> list.flatten == f
}
pub fn has_repeated_string(i: Int) -> Bool {
let digits = digits(i, 10) |> result.unwrap([])
digits_to_substrings(digits)
|> list.filter(can_repeat_into(_, digits))
|> list.filter(repeats_into(_, digits))
|> fn(l) { l != [] }
}
pub fn filter_for_bad_ids(ids: List(Int)) -> List(Int) { pub fn filter_for_bad_ids(ids: List(Int)) -> List(Int) {
list.filter(ids, fn(id) { int_is_doubled_string(id) |> result.unwrap(False) }) list.filter(ids, fn(id) { int_is_doubled_string(id) |> result.unwrap(False) })
} }
@ -72,6 +101,27 @@ pub fn file_to_sum_of_bad_ids(f: String) -> Int {
|> sum_of_bad_ids |> sum_of_bad_ids
} }
pub fn main() -> Nil { pub fn sum_of_bad_ids_part2(ids: List(#(Int, Int))) -> Int {
file_to_sum_of_bad_ids("input.txt") |> int.to_string |> io.println ranges_to_bad_ids_part2(ids) |> list.fold(0, int.add)
}
pub fn file_to_sum_of_bad_ids_part2(f: String) -> Int {
simplifile.read(f)
|> result.unwrap("")
|> string.trim
|> list_of_ranges_to_int_pairs
|> sum_of_bad_ids_part2
}
pub fn ranges_to_bad_ids_part2(ids: List(#(Int, Int))) -> List(Int) {
ranges_to_list_of_ids(ids) |> filter_for_bad_ids_part2
}
pub fn filter_for_bad_ids_part2(ids: List(Int)) -> List(Int) {
list.filter(ids, fn(id) { has_repeated_string(id) })
}
pub fn main() -> Nil {
// file_to_sum_of_bad_ids("input.txt") |> int.to_string |> io.println
file_to_sum_of_bad_ids_part2("input02.txt") |> int.to_string |> io.println
} }

View file

@ -110,6 +110,43 @@ pub fn ranges_parse_test() {
] ]
} }
pub fn calibrate() { pub fn calibrate_test() {
assert day_2.file_to_sum_of_bad_ids("test/calibration.txt") == 1_227_775_554 assert day_2.file_to_sum_of_bad_ids("test/calibration.txt") == 1_227_775_554
} }
pub fn int_to_substrings_test() {
assert day_2.digits_to_substrings([1, 2, 3, 4]) == [[1], [1, 2], [1, 2, 3]]
}
pub fn can_repeat_into_test() {
assert day_2.can_repeat_into([1, 2], [1, 2, 3, 4])
}
pub fn cannot_repeat_into_test() {
assert !{ day_2.can_repeat_into([1, 2], [1, 2, 3]) }
}
pub fn repeats_into_test() {
assert day_2.repeats_into([1, 2], [1, 2, 1, 2])
}
pub fn doesnot_repeat_into_test() {
assert !day_2.repeats_into([1, 2], [1, 2, 3])
}
pub fn has_doubled_string_test() {
assert day_2.has_repeated_string(1212)
}
pub fn has_trippled_string_test() {
assert day_2.has_repeated_string(121_212)
}
pub fn no_repeated_strings_test() {
assert !day_2.has_repeated_string(123)
}
pub fn calibrate_part2_test() {
assert day_2.file_to_sum_of_bad_ids_part2("test/calibration.txt")
== 4_174_379_265
}