Skip to content

Commit

Permalink
day 02: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sreedevk committed Dec 2, 2024
1 parent c4ffe15 commit ea74fe8
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/advent_of_code.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,35 @@ import gleam/int
import gleam/io
import gleam/result
import historian_hysteria as day01
import red_nosed_reports as day02
import simplifile.{read}

pub fn main() {
// Day 01
result.map(read("data/day1.txt"), fn(data) {
io.println(
"[1] Historian Hysteria (Part 1): " <> int.to_string(day01.solve_a(data)),
)
io.println(
"[1] Historian Hysteria (Part 2): " <> int.to_string(day01.solve_b(data)),
)
})
result.unwrap(
result.map(read("data/day1.txt"), fn(data) {
io.println(
"[1] Historian Hysteria (Part 1): "
<> int.to_string(day01.solve_a(data)),
)
io.println(
"[1] Historian Hysteria (Part 2): "
<> int.to_string(day01.solve_b(data)),
)
}),
Nil,
)

// Day 02
result.unwrap(
result.map(read("data/day2.txt"), fn(data) {
io.println(
"[2] Red Nosed Reports (Part 1): " <> int.to_string(day02.solve_a(data)),
)
io.println(
"[2] Red Nosed Reports (Part 2): " <> int.to_string(day02.solve_b(data)),
)
}),
Nil,
)
}
66 changes: 66 additions & 0 deletions src/red_nosed_reports.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import gleam/int
import gleam/io
import gleam/list
import gleam/pair
import gleam/result
import gleam/string

fn parse_line(line: String) -> List(Int) {
use a <- list.map(string.split(line, " "))
result.unwrap(int.parse(a), 0)
}

fn all_incr(report: List(Int)) -> Bool {
use nums <- list.all(list.window_by_2(report))
case pair.second(nums) - pair.first(nums) {
x if x > 0 && x < 4 -> True
_ -> False
}
}

fn all_decr(report: List(Int)) -> Bool {
use nums <- list.all(list.window_by_2(report))
case pair.first(nums) - pair.second(nums) {
x if x > 0 && x < 4 -> True
_ -> False
}
}

fn valid(report: List(Int)) -> Bool {
all_incr(report) || all_decr(report)
}

fn valid_after_dampener(report) -> Bool {
case valid(report) {
True -> True
False -> {
let indexed_list = list.index_map(report, fn(x, i) { #(i, x) })
let range = list.range(0, list.length(indexed_list) - 1)
let final_list =
list.map(range, fn(rmi) {
use #(_, flist) <- result.map(list.key_pop(indexed_list, rmi))
list.map(flist, pair.second)
})

let ys = list.map(final_list, fn(y) { result.unwrap(y, []) })
list.any(ys, fn(y) { valid(y) })
}
}
}

pub fn solve_a(input: String) -> Int {
input
|> string.trim()
|> string.split("\n")
|> list.map(parse_line)
|> list.count(valid(_))
}

pub fn solve_b(input: String) -> Int {
input
|> string.trim()
|> string.split("\n")
|> list.map(parse_line)
|> list.filter(valid_after_dampener(_))
|> list.length()
}
21 changes: 21 additions & 0 deletions test/red_nosed_reports_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import gleam/string
import gleeunit/should
import red_nosed_reports as day01

fn test_data() -> String {
string.join(
[
"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",
],
"\n",
)
}

pub fn solve_a_test() {
should.equal(day01.solve_a(test_data()), 2)
}

pub fn solve_b_test() {
should.equal(day01.solve_b(test_data()), 4)
}

0 comments on commit ea74fe8

Please sign in to comment.