From e6623bd6c19db62ca17556fc16e5c4c874ae7509 Mon Sep 17 00:00:00 2001 From: "J.J" Date: Fri, 1 Dec 2023 12:59:02 -0500 Subject: beginning day 1 in racket --- aoc2023-other/day-01/day-01.rkt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 aoc2023-other/day-01/day-01.rkt diff --git a/aoc2023-other/day-01/day-01.rkt b/aoc2023-other/day-01/day-01.rkt new file mode 100644 index 0000000..1d13ba6 --- /dev/null +++ b/aoc2023-other/day-01/day-01.rkt @@ -0,0 +1,5 @@ +#lang racket + +(require advent-of-code + threading) + -- cgit v1.2.3 From a8589ba63ceb079ac6a24a7835098ec633296f02 Mon Sep 17 00:00:00 2001 From: "J.J" Date: Fri, 1 Dec 2023 13:33:45 -0500 Subject: racket day 1 complete --- aoc2023-other/day-01/day-01.rkt | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/aoc2023-other/day-01/day-01.rkt b/aoc2023-other/day-01/day-01.rkt index 1d13ba6..b4fd688 100644 --- a/aoc2023-other/day-01/day-01.rkt +++ b/aoc2023-other/day-01/day-01.rkt @@ -3,3 +3,36 @@ (require advent-of-code threading) +(define calibration-values (fetch-aoc-input (find-session) 2023 1)) + +(define (to-number str) + (match (string->number str) + [#false (hash-ref word-to-digit str)] + [n n])) + +(define (parse-calibration-value v valid) + (for/fold ([acc '()] [value v] #:result (+ (to-number (first acc)) (* 10 (to-number (last acc))))) + ([_ (in-naturals)]) + #:break (equal? value "") + (let ([possible-prefix (findf (curry string-prefix? value) valid)]) + (if possible-prefix + (values (cons possible-prefix acc) (substring value 1)) + (values acc (substring value 1)))))) + +(define (total-calibration input valid) + (~> input + (string-trim) + (string-split "\n") + (map (λ~> (parse-calibration-value valid)) _) + (apply + _))) + +;; part 1 + +(define valid-for-part-1 (~> (range 0 10) (map ~a _))) +(total-calibration calibration-values valid-for-part-1) + +;; part 2 +(define word-to-digit + (hash "one" 1 "two" 2 "three" 3 "four" 4 "five" 5 "six" 6 "seven" 7 "eight" 8 "nine" 9 "zero" 0)) +(define valid-for-part-2 (append valid-for-part-1 (hash-keys word-to-digit))) +(total-calibration calibration-values valid-for-part-2) -- cgit v1.2.3 From b24b10d0dc6f5c58a85ab1b62334a14301e5e3bf Mon Sep 17 00:00:00 2001 From: "J.J" Date: Fri, 1 Dec 2023 14:30:17 -0500 Subject: fixing a smal --- aoc2023/src/day1/solve.gleam | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/aoc2023/src/day1/solve.gleam b/aoc2023/src/day1/solve.gleam index fbc160f..b9a6588 100644 --- a/aoc2023/src/day1/solve.gleam +++ b/aoc2023/src/day1/solve.gleam @@ -7,7 +7,7 @@ import gleam/result import gleam/int fn parse_digits(input: String) { - let assert Ok(re) = regex.from_string("[0-9]") + let assert Ok(re) = regex.from_string("[1-9]") input |> string.split("\n") @@ -39,7 +39,6 @@ const substitutions = [ #("seven", "7n"), #("eight", "e8t"), #("nine", "n9e"), - #("zero", "0o"), ] pub fn part2(input: String) { -- cgit v1.2.3 From a75aa83cefb27b7684afb8f3e74f4dc199f0dcff Mon Sep 17 00:00:00 2001 From: "J.J" Date: Fri, 1 Dec 2023 14:31:01 -0500 Subject: fixing a small specification mistake --- aoc2023-other/day-01/day-01.rkt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aoc2023-other/day-01/day-01.rkt b/aoc2023-other/day-01/day-01.rkt index b4fd688..b720f79 100644 --- a/aoc2023-other/day-01/day-01.rkt +++ b/aoc2023-other/day-01/day-01.rkt @@ -28,11 +28,11 @@ ;; part 1 -(define valid-for-part-1 (~> (range 0 10) (map ~a _))) +(define valid-for-part-1 (~> (range 1 10) (map ~a _))) (total-calibration calibration-values valid-for-part-1) ;; part 2 (define word-to-digit - (hash "one" 1 "two" 2 "three" 3 "four" 4 "five" 5 "six" 6 "seven" 7 "eight" 8 "nine" 9 "zero" 0)) + (hash "one" 1 "two" 2 "three" 3 "four" 4 "five" 5 "six" 6 "seven" 7 "eight" 8 "nine" 9)) (define valid-for-part-2 (append valid-for-part-1 (hash-keys word-to-digit))) (total-calibration calibration-values valid-for-part-2) -- cgit v1.2.3 From f648264a1a91f93f1643163339d52968f1c7bec9 Mon Sep 17 00:00:00 2001 From: "J.J" Date: Fri, 1 Dec 2023 17:58:13 -0500 Subject: day 1 improvements --- aoc2023/adglent_cheatsheet.md | 3 +++ aoc2023/src/day1/solve.gleam | 9 +++------ 2 files changed, 6 insertions(+), 6 deletions(-) create mode 100644 aoc2023/adglent_cheatsheet.md diff --git a/aoc2023/adglent_cheatsheet.md b/aoc2023/adglent_cheatsheet.md new file mode 100644 index 0000000..4061e1b --- /dev/null +++ b/aoc2023/adglent_cheatsheet.md @@ -0,0 +1,3 @@ +Set up a solution: `gleam run -m adglent/day ` +Check against examples: `gleam test -- --modules=day/day_test` +Get final answer: `gleam run -m day/solve

` \ No newline at end of file diff --git a/aoc2023/src/day1/solve.gleam b/aoc2023/src/day1/solve.gleam index b9a6588..29322ce 100644 --- a/aoc2023/src/day1/solve.gleam +++ b/aoc2023/src/day1/solve.gleam @@ -3,7 +3,6 @@ import gleam/io import gleam/list import gleam/string import gleam/regex.{type Match, Match} -import gleam/result import gleam/int fn parse_digits(input: String) { @@ -11,21 +10,19 @@ fn parse_digits(input: String) { input |> string.split("\n") - |> list.map(fn(s) { + |> list.fold(0, fn(acc, s) { let matches = regex.scan(s, with: re) let assert Ok(Match(content: first, ..)) = list.first(matches) let assert Ok(Match(content: last, ..)) = list.last(matches) - - int.parse(first <> last) + let assert Ok(i) = int.parse(first <> last) + acc + i }) } pub fn part1(input: String) { input |> parse_digits - |> result.values - |> int.sum |> string.inspect } -- cgit v1.2.3 From 826d7698c84b42dadf6b1684c93828e3e2f5ee3a Mon Sep 17 00:00:00 2001 From: "J.J" Date: Fri, 1 Dec 2023 18:00:48 -0500 Subject: day 1 improvements --- aoc2023/src/day1/solve.gleam | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/aoc2023/src/day1/solve.gleam b/aoc2023/src/day1/solve.gleam index 29322ce..ed14bde 100644 --- a/aoc2023/src/day1/solve.gleam +++ b/aoc2023/src/day1/solve.gleam @@ -5,24 +5,22 @@ import gleam/string import gleam/regex.{type Match, Match} import gleam/int -fn parse_digits(input: String) { +pub fn part1(input: String) { let assert Ok(re) = regex.from_string("[1-9]") input |> string.split("\n") - |> list.fold(0, fn(acc, s) { - let matches = regex.scan(s, with: re) - - let assert Ok(Match(content: first, ..)) = list.first(matches) - let assert Ok(Match(content: last, ..)) = list.last(matches) - let assert Ok(i) = int.parse(first <> last) - acc + i - }) -} + |> list.fold( + 0, + fn(acc, s) { + let matches = regex.scan(s, with: re) -pub fn part1(input: String) { - input - |> parse_digits + let assert Ok(Match(content: first, ..)) = list.first(matches) + let assert Ok(Match(content: last, ..)) = list.last(matches) + let assert Ok(i) = int.parse(first <> last) + acc + i + }, + ) |> string.inspect } -- cgit v1.2.3