aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--aoc2021/day-15/day-15.rkt5
-rw-r--r--aoc2023/src/day15/solve.gleam14
-rw-r--r--aoc2023/src/day17/solve.gleam24
-rw-r--r--aoc2023/src/day2/solve.gleam7
-rw-r--r--aoc2023/src/day5/solve.gleam23
-rw-r--r--aoc2023/src/utilities/array2d.gleam6
-rw-r--r--aoc2023/test/day15/day15_test.gleam2
7 files changed, 59 insertions, 22 deletions
diff --git a/aoc2021/day-15/day-15.rkt b/aoc2021/day-15/day-15.rkt
index 5e61c55..6ab67b1 100644
--- a/aoc2021/day-15/day-15.rkt
+++ b/aoc2021/day-15/day-15.rkt
@@ -1,12 +1,13 @@
#lang racket
-(require "../../jj-aoc.rkt"
+(require advent-of-code
threading
graph)
(struct Point (x y) #:transparent)
(define data
- (for/fold ([cells (hash)]) ([row (in-lines (open-day 15 2021))] [x (in-naturals)])
+ (for/fold ([cells (hash)])
+ ([row (in-lines (open-aoc-input (find-session) 2021 15 #:cache #true))] [x (in-naturals)])
(for/fold ([cells cells]) ([n (in-string row)] [y (in-naturals)])
(hash-set cells (Point x y) (~> n string string->number)))))
diff --git a/aoc2023/src/day15/solve.gleam b/aoc2023/src/day15/solve.gleam
index 2d863b5..a7d250c 100644
--- a/aoc2023/src/day15/solve.gleam
+++ b/aoc2023/src/day15/solve.gleam
@@ -12,13 +12,13 @@ fn split(input: String) -> List(String) {
}
fn hash_algorithm(str: String) -> Int {
- str
- |> string.to_utf_codepoints()
- |> list.map(string.utf_codepoint_to_int)
- |> list.fold(0, fn(acc, c) {
- let assert Ok(acc) = int.modulo({ acc + c } * 17, 256)
- acc
- })
+ let codepoints =
+ str
+ |> string.to_utf_codepoints()
+ |> list.map(string.utf_codepoint_to_int)
+ use acc, c <- list.fold(codepoints, 0)
+ let assert Ok(acc) = int.modulo({ acc + c } * 17, 256)
+ acc
}
pub fn part1(input: String) -> String {
diff --git a/aoc2023/src/day17/solve.gleam b/aoc2023/src/day17/solve.gleam
index 5289ce9..3ed632c 100644
--- a/aoc2023/src/day17/solve.gleam
+++ b/aoc2023/src/day17/solve.gleam
@@ -1,5 +1,29 @@
import adglent.{First, Second}
import gleam/io
+import utilities/array2d.{type Posn, Posn}
+
+type Direction {
+ North
+ South
+ East
+ West
+}
+
+fn turn_dirs(dir: Direction) {
+ case dir {
+ North | South -> [East, West]
+ East | West -> [North, South]
+ }
+}
+
+fn delta(dir: Direction) {
+ case dir {
+ North -> Posn(-1, 0)
+ South -> Posn(1, 0)
+ East -> Posn(0, 1)
+ West -> Posn(0, -1)
+ }
+}
pub fn part1(input: String) {
todo as "Implement solution to part 1"
diff --git a/aoc2023/src/day2/solve.gleam b/aoc2023/src/day2/solve.gleam
index 608955f..38e62d7 100644
--- a/aoc2023/src/day2/solve.gleam
+++ b/aoc2023/src/day2/solve.gleam
@@ -45,9 +45,10 @@ pub fn part2(input: String) {
green: int.max(green, acc.green),
)
}
- |> list.fold(from: 0, with: fn(acc, g: Game) {
- acc + g.red * g.blue * g.green
- })
+ |> list.fold(
+ from: 0,
+ with: fn(acc, g: Game) { acc + g.red * g.blue * g.green },
+ )
}
pub fn main() {
diff --git a/aoc2023/src/day5/solve.gleam b/aoc2023/src/day5/solve.gleam
index 7c05310..58e2ae0 100644
--- a/aoc2023/src/day5/solve.gleam
+++ b/aoc2023/src/day5/solve.gleam
@@ -132,17 +132,22 @@ fn do_remap_range(r: SeedRange, mapper: Mapper, acc: List(SeedRange)) {
]
// range overlaps end but not start -> left side transformed, right side moves to next mapping
[m, ..ms] if r.start >= m.start && r.end > m.end ->
- do_remap_range(SRange(m.end + 1, r.end), ms, [
- transform_range(SRange(r.start, m.end), m),
- ..acc
- ])
+ do_remap_range(
+ SRange(m.end + 1, r.end),
+ ms,
+ [transform_range(SRange(r.start, m.end), m), ..acc],
+ )
// mapping is fully inside range -> left not transformed, middle transformed, right to next
[m, ..ms] ->
- do_remap_range(SRange(m.end + 1, r.end), ms, [
- SRange(r.start, m.start - 1),
- transform_range(SRange(m.start, m.end), m),
- ..acc
- ])
+ do_remap_range(
+ SRange(m.end + 1, r.end),
+ ms,
+ [
+ SRange(r.start, m.start - 1),
+ transform_range(SRange(m.start, m.end), m),
+ ..acc
+ ],
+ )
}
}
diff --git a/aoc2023/src/utilities/array2d.gleam b/aoc2023/src/utilities/array2d.gleam
index d7d05f8..83ec8ae 100644
--- a/aoc2023/src/utilities/array2d.gleam
+++ b/aoc2023/src/utilities/array2d.gleam
@@ -9,6 +9,12 @@ pub type Posn {
pub type Array2D(a) =
Dict(Posn, a)
+pub fn add_posns(p1: Posn, p2: Posn) -> Posn {
+ case p1, p2 {
+ Posn(r1, c1), Posn(r2, c2) -> Posn(r1 + r2, c1 + c2)
+ }
+}
+
pub fn to_2d_array(xss: List(List(a))) -> Array2D(a) {
{
use r, row <- list.index_map(xss)
diff --git a/aoc2023/test/day15/day15_test.gleam b/aoc2023/test/day15/day15_test.gleam
index 83fadef..0ecaecc 100644
--- a/aoc2023/test/day15/day15_test.gleam
+++ b/aoc2023/test/day15/day15_test.gleam
@@ -22,7 +22,7 @@ const part1_examples: List(Example(Problem1AnswerType)) = [
///const part2_examples: List(Example(Problem2AnswerType)) = [Example("some input", "")]
/// ```
const part2_examples: List(Example(Problem2AnswerType)) = [
- Example("rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7", "145"),
+ Example("rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7", "145"),
]
pub fn part1_test() {