Advent-of-Code-2025/day_1/test/day_1_test.gleam
2025-12-01 23:47:25 +01:00

178 lines
3.4 KiB
Gleam

import day_1.{Left, Right, rotate}
import gleam/list
import gleeunit
import simplifile
pub fn main() -> Nil {
gleeunit.main()
}
pub fn normal_right_rotate_test() {
let initial = 5
let rotation = Right(5)
let final = rotate(initial, rotation)
assert final == 10
}
pub fn overflow_right_rotate_test() {
let initial = 60
let rotation = Right(95)
let final = rotate(initial, rotation)
assert final == 55
}
pub fn normal_left_rotate_test() {
let initial = 40
let rotation = Left(10)
let final = rotate(initial, rotation)
assert final == 30
}
pub fn underflow_left_rotate_test() {
let initial = 40
let rotation = Left(50)
let final = rotate(initial, rotation)
assert final == 90
}
pub fn right_rotate_to_zero_test() {
assert rotate(52, Right(48)) == 0
}
pub fn left_rotate_to_zero_test() {
assert rotate(20, Left(20)) == 0
}
pub fn right_rotate_off_zero_test() {
assert rotate(0, Right(1)) == 1
}
pub fn left_rotate_off_zero_test() {
assert rotate(0, Left(1)) == 99
}
pub fn rotate_left_step_test() {
let initial = [50]
let rotation = Left(68)
let final = day_1.rotate_step(initial, rotation)
assert final == [82, 50]
}
pub fn rotate_1st_step_test() {
assert day_1.rotate_step([], Left(68)) == [82, 50]
}
pub fn rotate_2nd_step_test() {
assert day_1.rotate_step([82, 50], Left(30)) == [52, 82, 50]
}
pub fn count_list_of_zeros_test() {
assert { day_1.count_zeros([0, 0, 0]) == 3 }
}
pub fn count_empty_list_test() {
assert { day_1.count_zeros([]) == 0 }
}
pub fn count_dirty_list_test() {
assert { day_1.count_zeros([0, 1, 2, 0]) == 2 }
}
pub fn the_big_test() {
let rotations = [
Left(68),
Left(30),
Right(48),
Left(5),
Right(60),
Left(55),
Left(1),
Left(99),
Right(14),
Left(82),
]
assert { list.fold(rotations, [], day_1.rotate_step) |> day_1.count_zeros }
== 3
}
pub fn good_right_parse_test() {
assert day_1.line_to_rotation("R16") == Ok(Right(16))
}
pub fn good_left_parse_test() {
assert day_1.line_to_rotation("L32") == Ok(Left(32))
}
pub fn no_leading_letter_test() {
assert day_1.line_to_rotation("13") == Error(Nil)
}
pub fn no_numeral_test() {
assert day_1.line_to_rotation("L") == Error(Nil)
}
pub fn empty_string_test() {
assert day_1.line_to_rotation("") == Error(Nil)
}
pub fn garbage_string_test() {
assert day_1.line_to_rotation("a983u4") == Error(Nil)
}
pub fn split_good_file_test() {
assert day_1.read_line("test/good_file.txt") == Ok(["hello", "world", ""])
}
pub fn split_bad_file_test() {
assert day_1.read_line("test/bad_file.txt") == Error(simplifile.Enoent)
}
pub fn parse_rotats_test() {
assert day_1.file_to_rotations("test/parse_rotats.txt")
== Ok([
Left(68),
Left(30),
Right(48),
Left(5),
Right(60),
Left(55),
Left(1),
Left(99),
Right(14),
Left(82),
])
}
pub fn apply_empty_rots_test() {
assert day_1.apply_rotations([]) == []
}
pub fn apply_first_rot_test() {
assert day_1.apply_rotations([Left(68)]) == [82, 50]
}
pub fn apply_10_rots_test() {
assert day_1.apply_rotations([
Left(68),
Left(30),
Right(48),
Left(5),
Right(60),
Left(55),
Left(1),
Left(99),
Right(14),
Left(82),
])
== [32, 14, 0, 99, 0, 55, 95, 0, 52, 82, 50]
}
pub fn proving_test() {
assert day_1.rotations_file_to_zeros("test/parse_rotats.txt") == Ok(3)
}