diff options
author | HJ <thechairman@thechairman.info> | 2023-12-06 01:03:37 -0500 |
---|---|---|
committer | HJ <thechairman@thechairman.info> | 2023-12-06 01:03:37 -0500 |
commit | ba0980cb60c4373d28619a4b200b7375ac381791 (patch) | |
tree | 3645fa77c03b2d22bf4efc1007e0c44ffa37b52c | |
parent | ac7a889dea39a6479d2d6c47b2b8304bae60e49c (diff) | |
download | gleam_aoc-ba0980cb60c4373d28619a4b200b7375ac381791.tar.gz gleam_aoc-ba0980cb60c4373d28619a4b200b7375ac381791.zip |
day 6 gleam/racket complete
-rw-r--r-- | aoc2023-other/day-06/day-06.rkt | 31 | ||||
-rw-r--r-- | aoc2023/src/day6/.gitignore | 1 | ||||
-rw-r--r-- | aoc2023/src/day6/solve.gleam | 85 | ||||
-rw-r--r-- | aoc2023/test/day6/day6_test.gleam | 50 |
4 files changed, 167 insertions, 0 deletions
diff --git a/aoc2023-other/day-06/day-06.rkt b/aoc2023-other/day-06/day-06.rkt new file mode 100644 index 0000000..2897821 --- /dev/null +++ b/aoc2023-other/day-06/day-06.rkt @@ -0,0 +1,31 @@ +#lang racket + +(require advent-of-code + threading) + +(match-define (list times distances) + (~> (open-aoc-input (find-session) 2023 6 #:cache #true) port->lines)) + +;; part 1 +(define get-numbers (λ~> string-split (map string->number _) rest)) +(define (find-bound race-time dist button-time step) + (if (< dist (* button-time (- race-time button-time))) + button-time + (find-bound race-time dist (+ step button-time) step))) + +(define (lower-bound rtime dist) + (find-bound rtime dist 1 1)) +(define (upper-bound rtime dist) + (find-bound rtime dist rtime -1)) + +(for/fold ([acc 1]) + ([race-time (in-list (get-numbers times))] [distance (in-list (get-numbers distances))]) + (* acc (add1 (- (upper-bound race-time distance) (lower-bound race-time distance))))) + +;; part 2 + +(define unkern (λ~>> get-numbers (apply ~a) string->number)) +(define big-time (unkern times)) +(define big-distance (unkern distances)) + +(add1 (- (upper-bound big-time big-distance) (lower-bound big-time big-distance))) diff --git a/aoc2023/src/day6/.gitignore b/aoc2023/src/day6/.gitignore new file mode 100644 index 0000000..ae40cea --- /dev/null +++ b/aoc2023/src/day6/.gitignore @@ -0,0 +1 @@ +input.txt
\ No newline at end of file diff --git a/aoc2023/src/day6/solve.gleam b/aoc2023/src/day6/solve.gleam new file mode 100644 index 0000000..98f39a1 --- /dev/null +++ b/aoc2023/src/day6/solve.gleam @@ -0,0 +1,85 @@ +import adglent.{First, Second} +import gleam/io +import gleam/string +import gleam/int +import gleam/list +import gleam/result + +type Race { + Race(time: Int, distance: Int) +} + +fn parse_with_bad_kerning(input: String) { + input + |> string.split("\n") + |> list.map(fn(str) { + str + |> string.split(" ") + |> list.map(int.parse) + |> result.values + }) + |> list.transpose + |> list.map(fn(ns) { + let [t, d] = ns + Race(t, d) + }) +} + +fn find_bound(race: Race, button_time: Int, step: Int) { + let travel_time = race.time - button_time + case button_time * travel_time > race.distance { + True -> button_time + False -> find_bound(race, button_time + step, step) + } +} + +fn lower_bound(race: Race) { + find_bound(race, 1, 1) +} + +fn upper_bound(race: Race) { + find_bound(race, race.time, -1) +} + +pub fn part1(input: String) { + { + use acc, race <- list.fold(parse_with_bad_kerning(input), 1) + acc * { upper_bound(race) - lower_bound(race) + 1 } + } + |> string.inspect +} + +fn parse_properly(input: String) { + input + |> string.replace(" ", "") + |> string.split("\n") + |> list.flat_map(string.split(_, ":")) + |> list.map(int.parse) + |> result.values +} + +pub fn part2(input: String) { + let [time, distance] = + input + |> parse_properly + + let race = Race(time, distance) + + upper_bound(race) - lower_bound(race) + 1 + |> string.inspect +} + +pub fn main() { + let assert Ok(part) = adglent.get_part() + let assert Ok(input) = adglent.get_input("6") + case part { + First -> + part1(input) + |> adglent.inspect + |> io.println + Second -> + part2(input) + |> adglent.inspect + |> io.println + } +} diff --git a/aoc2023/test/day6/day6_test.gleam b/aoc2023/test/day6/day6_test.gleam new file mode 100644 index 0000000..c551993 --- /dev/null +++ b/aoc2023/test/day6/day6_test.gleam @@ -0,0 +1,50 @@ +import gleam/list +import showtime/tests/should +import adglent.{type Example, Example} +import day6/solve + +type Problem1AnswerType = + String + +type Problem2AnswerType = + String + +/// Add examples for part 1 here: +/// ```gleam +///const part1_examples: List(Example(Problem1AnswerType)) = [Example("some input", "")] +/// ``` +const part1_examples: List(Example(Problem1AnswerType)) = [ + Example( + "Time: 7 15 30 +Distance: 9 40 200", + "288", + ), +] + +/// Add examples for part 2 here: +/// ```gleam +///const part2_examples: List(Example(Problem2AnswerType)) = [Example("some input", "")] +/// ``` +const part2_examples: List(Example(Problem2AnswerType)) = [ + Example( + "Time: 7 15 30 +Distance: 9 40 200", + "71503", + ), +] + +pub fn part1_test() { + part1_examples + |> should.not_equal([]) + use example <- list.map(part1_examples) + solve.part1(example.input) + |> should.equal(example.answer) +} + +pub fn part2_test() { + part2_examples + |> should.not_equal([]) + use example <- list.map(part2_examples) + solve.part2(example.input) + |> should.equal(example.answer) +} |