Skip to content

Commit 46e40f3

Browse files
committed
trailing whitespace was useful in the last problem. Refactored so it will only remove one trailing newline if present.
1 parent deffe41 commit 46e40f3

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

src/day06/solution.gleam

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,10 @@ pub fn solve_p1(lines: List(String)) -> Result(String, String) {
3737

3838
// Part 2
3939
pub fn solve_p2(lines: List(String)) -> Result(String, String) {
40-
let line_length =
41-
list.fold(lines, 0, fn(acc, v) { int.max(acc, string.length(v)) })
42-
4340
lines
4441
|> list.map(fn(line) {
45-
let new_line =
46-
string.to_graphemes(line)
47-
|> list.reverse
48-
49-
let n = line_length - list.length(new_line)
50-
list.append(list.repeat(" ", n), new_line)
42+
string.to_graphemes(line)
43+
|> list.reverse
5144
})
5245
|> list.transpose
5346
|> list.filter(fn(l) {

src/internal/aoc_utils.gleam

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ pub fn read_lines(
1313
) -> Result(List(String), simplifile.FileError) {
1414
use content <- result.map(simplifile.read(from: filepath))
1515

16-
content
17-
|> string.trim_end
16+
// Drop one trailing newline if present
17+
case string.ends_with(content, "\n") {
18+
False -> content
19+
True -> string.drop_end(content, 1)
20+
}
1821
|> string.split("\n")
1922
}
2023

0 commit comments

Comments
 (0)