aoc2026

Advent of code 2026 solutions in Gleam
Log | Files | Refs | README

commit fa95d0fb0e12e301904ecd9a5d7c522db525817c
Author: Martin Ashby <martin@ashbysoft.com>
Date:   Tue,  1 Dec 2026 12:03:46 +0000

Day 1 pt1 & 2

Diffstat:
A.gitignore | 4++++
AREADME.md | 6++++++
Agleam.toml | 20++++++++++++++++++++
Ainput/day1.txt | 2018+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amanifest.toml | 19+++++++++++++++++++
Asrc/aoc2026.gleam | 9+++++++++
Asrc/day1.gleam | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Atest/aoc2026_test.gleam | 5+++++
Atest/day1_test.gleam | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9 files changed, 2189 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,4 @@ +*.beam +*.ez +/build +erl_crash.dump diff --git a/README.md b/README.md @@ -0,0 +1,5 @@ +# aoc2026 + +Advent of code solutions for 2026 in Gleam + +https://adventofcode.com/2026 +\ No newline at end of file diff --git a/gleam.toml b/gleam.toml @@ -0,0 +1,20 @@ +name = "aoc2026" +version = "1.0.0" + +# Fill out these fields if you intend to generate HTML documentation or publish +# your project to the Hex package manager. +# +# description = "" +# licences = ["Apache-2.0"] +# repository = { type = "github", user = "", repo = "" } +# links = [{ title = "Website", href = "" }] +# +# For a full reference of all the available options, you can have a look at +# https://gleam.run/writing-gleam/gleam-toml/. + +[dependencies] +gleam_stdlib = ">= 1.0.0 and < 2.0.0" +simplifile = ">= 2.7.0 and < 3.0.0" + +[dev_dependencies] +gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/input/day1.txt b/input/day1.txt @@ -0,0 +1,2017 @@ +639 +611 +933 +104 +701 +162 +917 +202 +608 +512 +753 +459 +409 +736 +412 +933 +691 +752 +359 +954 +295 +288 +684 +178 +908 +990 +996 +346 +473 +777 +736 +399 +736 +272 +928 +338 +546 +798 +882 +916 +191 +129 +668 +210 +409 +640 +888 +864 +855 +795 +171 +506 +481 +514 +726 +313 +832 +668 +887 +504 +171 +409 +344 +459 +670 +862 +741 +726 +403 +574 +499 +175 +839 +848 +537 +869 +968 +780 +278 +688 +798 +967 +678 +346 +304 +973 +906 +757 +899 +948 +151 +856 +595 +900 +278 +868 +669 +549 +213 +172 +372 +215 +684 +961 +841 +260 +257 +532 +378 +681 +473 +151 +979 +368 +507 +757 +939 +866 +113 +558 +868 +555 +213 +771 +757 +903 +960 +985 +978 +894 +993 +368 +846 +374 +844 +663 +198 +279 +420 +338 +264 +990 +623 +297 +920 +366 +343 +822 +503 +805 +811 +325 +167 +317 +771 +861 +374 +801 +186 +343 +310 +869 +678 +773 +652 +813 +388 +469 +204 +896 +401 +546 +408 +808 +926 +299 +939 +475 +507 +463 +370 +742 +349 +912 +936 +426 +611 +355 +474 +590 +684 +931 +835 +623 +823 +420 +483 +469 +575 +904 +389 +499 +700 +919 +270 +552 +362 +710 +734 +666 +494 +513 +382 +437 +644 +784 +811 +895 +946 +252 +688 +773 +228 +341 +478 +495 +689 +401 +135 +621 +300 +495 +436 +862 +135 +172 +599 +711 +222 +356 +270 +444 +895 +846 +621 +133 +797 +388 +370 +788 +560 +152 +684 +398 +499 +798 +760 +107 +769 +682 +344 +511 +199 +275 +187 +773 +389 +427 +578 +732 +105 +900 +222 +494 +212 +472 +355 +682 +904 +971 +812 +894 +935 +342 +793 +456 +666 +513 +773 +670 +891 +904 +968 +153 +416 +312 +559 +435 +131 +618 +257 +197 +437 +228 +249 +837 +646 +407 +755 +636 +741 +393 +385 +714 +399 +902 +317 +670 +460 +720 +882 +792 +240 +523 +574 +588 +357 +705 +640 +820 +277 +753 +907 +166 +811 +527 +710 +736 +255 +327 +599 +279 +523 +399 +186 +635 +946 +186 +481 +496 +953 +417 +594 +546 +349 +347 +982 +620 +444 +689 +661 +317 +460 +186 +278 +984 +611 +198 +275 +734 +778 +724 +211 +187 +765 +387 +792 +965 +106 +173 +469 +590 +595 +478 +593 +571 +979 +281 +370 +873 +735 +836 +964 +310 +506 +759 +267 +297 +743 +866 +708 +800 +519 +387 +600 +374 +655 +503 +841 +430 +416 +257 +984 +890 +767 +439 +639 +111 +463 +564 +846 +474 +272 +745 +715 +844 +926 +254 +613 +809 +955 +661 +553 +802 +520 +795 +213 +805 +802 +756 +661 +124 +532 +321 +175 +802 +220 +815 +580 +459 +436 +427 +682 +527 +240 +865 +951 +759 +801 +398 +753 +316 +309 +769 +985 +789 +122 +927 +469 +777 +791 +750 +347 +179 +996 +719 +689 +981 +862 +711 +310 +909 +676 +746 +109 +900 +809 +856 +822 +755 +372 +660 +926 +132 +382 +152 +926 +823 +180 +580 +771 +800 +740 +395 +976 +460 +600 +502 +962 +835 +369 +150 +229 +125 +800 +582 +608 +819 +503 +636 +318 +861 +664 +531 +976 +208 +250 +825 +319 +369 +842 +831 +778 +708 +166 +629 +254 +739 +852 +394 +572 +605 +981 +750 +765 +449 +206 +864 +416 +779 +979 +472 +187 +825 +645 +565 +470 +300 +394 +765 +389 +141 +973 +635 +793 +287 +927 +186 +734 +456 +489 +880 +776 +735 +549 +344 +267 +166 +208 +842 +605 +193 +522 +280 +388 +142 +179 +358 +382 +607 +583 +881 +916 +523 +310 +939 +430 +277 +246 +212 +580 +179 +164 +256 +361 +605 +607 +901 +511 +255 +255 +903 +476 +981 +813 +985 +369 +220 +333 +860 +164 +106 +670 +934 +112 +299 +924 +582 +962 +454 +312 +130 +648 +960 +366 +941 +239 +896 +197 +765 +757 +398 +210 +486 +407 +819 +682 +162 +911 +856 +808 +968 +789 +928 +784 +622 +309 +519 +454 +903 +884 +368 +321 +726 +756 +468 +944 +459 +719 +401 +111 +180 +739 +423 +594 +990 +574 +572 +312 +652 +637 +741 +228 +888 +344 +585 +385 +620 +412 +944 +366 +583 +674 +639 +135 +585 +555 +600 +153 +569 +422 +213 +661 +212 +860 +531 +918 +912 +644 +531 +163 +357 +607 +904 +974 +732 +432 +947 +899 +252 +106 +918 +393 +385 +371 +665 +618 +880 +906 +660 +365 +909 +961 +504 +712 +331 +278 +309 +891 +784 +618 +948 +201 +427 +813 +173 +472 +712 +360 +392 +343 +544 +572 +438 +960 +395 +909 +732 +131 +476 +908 +992 +850 +597 +622 +737 +601 +214 +407 +112 +129 +588 +626 +801 +520 +310 +979 +941 +639 +239 +935 +903 +992 +850 +718 +585 +338 +231 +288 +554 +855 +974 +168 +943 +572 +575 +463 +902 +122 +850 +885 +193 +201 +475 +502 +342 +437 +212 +426 +895 +780 +740 +597 +880 +951 +449 +514 +249 +222 +934 +827 +845 +113 +916 +127 +365 +347 +391 +776 +920 +669 +510 +666 +961 +873 +496 +720 +388 +709 +552 +280 +627 +899 +404 +954 +900 +582 +622 +300 +132 +349 +387 +492 +852 +836 +927 +645 +974 +919 +842 +319 +877 +840 +893 +626 +512 +724 +304 +831 +903 +112 +611 +993 +116 +113 +663 +356 +295 +535 +912 +239 +398 +906 +478 +993 +776 +410 +214 +618 +366 +668 +877 +623 +500 +636 +308 +965 +146 +571 +655 +151 +760 +316 +951 +881 +760 +496 +901 +465 +135 +700 +911 +129 +892 +673 +936 +996 +256 +346 +962 +378 +356 +532 +619 +935 +204 +677 +265 +375 +801 +620 +701 +665 +837 +884 +754 +468 +895 +583 +427 +124 +847 +125 +916 +580 +538 +127 +812 +313 +427 +393 +709 +681 +591 +110 +967 +528 +967 +752 +131 +724 +645 +150 +363 +267 +362 +120 +943 +930 +776 +600 +438 +270 +129 +320 +791 +839 +327 +595 +191 +201 +150 +187 +133 +125 +298 +974 +201 +211 +660 +676 +394 +619 +308 +363 +820 +202 +519 +387 +990 +666 +705 +510 +839 +805 +320 +408 +899 +907 +208 +420 +504 +887 +665 +234 +681 +748 +353 +164 +441 +661 +347 +489 +215 +264 +943 +746 +793 +960 +887 +712 +483 +792 +752 +277 +808 +554 +355 +395 +559 +745 +825 +510 +823 +178 +594 +811 +856 +691 +703 +623 +196 +483 +740 +953 +366 +280 +391 +308 +971 +800 +380 +299 +197 +837 +462 +279 +708 +836 +528 +700 +795 +715 +391 +463 +179 +722 +152 +371 +240 +601 +648 +669 +325 +627 +571 +765 +769 +780 +372 +917 +527 +798 +599 +629 +416 +648 +410 +888 +474 +756 +593 +124 +486 +132 +789 +689 +559 +866 +107 +739 +677 +537 +837 +532 +930 +756 +107 +948 +435 +708 +984 +393 +892 +677 +531 +489 +901 +319 +928 +125 +615 +513 +961 +260 +795 +378 +691 +990 +646 +564 +775 +363 +346 +615 +924 +204 +180 +992 +250 +591 +131 +409 +778 +795 +873 +499 +198 +637 +973 +254 +955 +544 +213 +231 +221 +616 +663 +250 +171 +911 +105 +646 +486 +346 +343 +917 +681 +506 +116 +369 +701 +417 +868 +565 +719 +750 +793 +392 +316 +777 +546 +885 +861 +392 +993 +771 +588 +676 +492 +722 +341 +495 +918 +298 +887 +303 +848 +151 +496 +890 +718 +432 +601 +558 +827 +422 +595 +356 +732 +359 +746 +637 +852 +363 +565 +275 +489 +840 +947 +430 +815 +808 +841 +412 +892 +882 +578 +739 +349 +942 +560 +116 +432 +855 +976 +395 +714 +928 +619 +891 +591 +644 +462 +724 +215 +425 +191 +538 +172 +667 +613 +321 +664 +538 +945 +673 +809 +111 +962 +616 +713 +164 +197 +845 +739 +334 +180 +270 +562 +715 +535 +528 +438 +831 +441 +506 +246 +788 +676 +167 +150 +964 +508 +691 +257 +812 +618 +496 +802 +375 +948 +492 +682 +841 +473 +718 +265 +513 +891 +788 +953 +473 +881 +893 +597 +867 +363 +622 +163 +360 +394 +206 +211 +231 +951 +835 +920 +198 +409 +425 +365 +597 +511 +410 +722 +500 +593 +331 +220 +153 +359 +388 +295 +124 +691 +327 +344 +715 +303 +265 +200 +110 +423 +864 +819 +673 +855 +588 +394 +312 +648 +890 +221 +318 +146 +867 +831 +880 +141 +297 +408 +935 +868 +789 +742 +439 +813 +884 +385 +111 +823 +629 +456 +454 +907 +360 +670 +575 +342 +773 +432 +270 +982 +200 +280 +127 +740 +582 +942 +926 +162 +191 +732 +900 +380 +277 +985 +553 +294 +981 +391 +688 +260 +113 +287 +131 +825 +967 +320 +599 +743 +212 +636 +299 +700 +514 +917 +882 +362 +370 +842 +323 +294 +527 +924 +162 +341 +382 +387 +982 +636 +832 +508 +404 +819 +449 +930 +520 +549 +420 +931 +476 +835 +468 +645 +964 +104 +556 +399 +436 +652 +578 +714 +640 +754 +554 +520 +964 +304 +141 +734 +438 +619 +426 +615 +740 +372 +931 +860 +202 +133 +231 +152 +359 +709 +947 +667 +944 +199 +664 +902 +919 +558 +734 +196 +179 +294 +288 +473 +214 +412 +973 +277 +753 +200 +665 +514 +425 +320 +317 +532 +933 +919 +737 +896 +955 +742 +234 +511 +478 +249 +881 +737 +535 +705 +531 +797 +590 +593 +754 +791 +341 +623 +320 +172 +125 +199 +686 +663 +590 +608 +714 +163 +714 +668 +982 +865 +599 +187 +674 +133 +745 +375 +815 +746 +511 +116 +120 +815 +780 +404 +719 +425 +234 +474 +439 +456 +333 +508 +809 +809 +211 +403 +523 +771 +885 +613 +348 +389 +135 +709 +748 +562 +652 +684 +852 +459 +252 +722 +906 +655 +162 +127 +776 +347 +613 +278 +210 +304 +569 +142 +780 +168 +839 +605 +820 +404 +620 +356 +178 +465 +798 +798 +358 +840 +560 +544 +522 +357 +868 +993 +460 +620 +756 +308 +909 +936 +323 +973 +689 +556 +607 +255 +865 +382 +256 +372 +281 +888 +676 +316 +441 +648 +967 +132 +146 +513 +331 +606 +908 +715 +896 +220 +468 +674 +827 +627 +107 +678 +430 +822 +777 +992 +637 +578 +661 +877 +639 +673 +146 +626 +700 +436 +353 +941 +122 +710 +847 +757 +846 +357 +594 +470 +437 +537 +844 +854 +287 +767 +250 +591 +961 +827 +865 +392 +681 +438 +562 +885 +666 +941 +432 +264 +640 +422 +564 +392 +359 +380 +449 +720 +953 +313 +142 +412 +309 +904 +325 +120 +168 +549 +257 +168 +686 +635 +954 +494 +735 +902 +930 +202 +597 +745 +987 +255 +968 +815 +779 +978 +779 +987 +279 +842 +319 +317 +552 +968 +867 +928 +919 +221 +894 +791 +797 +686 +660 +678 +349 +358 +280 +635 +323 +847 +436 +173 +168 +439 +361 +104 +935 +361 +528 +343 +861 +627 +298 +197 +202 +256 +582 +942 +575 +506 +460 +229 +711 +294 +358 +894 +281 +981 +637 +661 +613 +965 +200 +300 +936 +710 +703 +591 +869 +674 +142 +616 +249 +686 +358 +422 +222 +850 +954 +499 +495 +571 +606 +249 +808 +435 +430 +907 +465 +228 +109 +769 +854 +611 +607 +503 +619 +893 +522 +767 +964 +333 +811 +792 +965 +775 +848 +942 +371 +850 +130 +705 +334 +835 +112 +741 +312 +832 +171 +906 +544 +196 +348 +342 +777 +333 +556 +200 +978 +562 +678 +355 +553 +481 +569 +198 +562 +856 +830 +250 +246 +323 +215 +272 +112 +933 +753 +823 +854 +606 +559 +748 +113 +560 +191 +385 +193 +309 +495 +393 +941 +470 +800 +754 +199 +743 +987 +146 +444 +595 +403 +374 +669 +475 +948 +741 +845 +222 +300 +629 +700 +564 +575 +127 +934 +830 +954 +755 +615 +519 +553 +590 +110 +353 +909 +408 +478 +616 +316 +294 +423 +180 +724 +308 +348 +852 +746 +830 +104 +462 +206 +885 +465 +759 +395 +229 +555 +987 +942 +946 +527 +895 +152 +572 +742 +338 +417 +880 +449 +971 +845 +403 +500 +254 +559 +105 +703 +845 +175 +512 +640 +334 +931 +435 +129 +254 +580 +866 +507 +549 +621 +132 +389 +711 +299 +133 +361 +342 +884 +365 +742 +660 +775 +365 +502 +444 +167 +944 +371 +178 +318 +463 +901 +965 +234 +130 +866 +303 +939 +109 +404 +924 +705 +\ No newline at end of file diff --git a/manifest.toml b/manifest.toml @@ -0,0 +1,19 @@ +# Do not manually edit this file, it is managed by Gleam. +# +# This file locks the dependency versions used, to make your build +# deterministic and to prevent unexpected versions from being included +# in your application. +# +# You should check this file into your source control repository. + +packages = [ + { name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" }, + { name = "gleam_stdlib", version = "1.2.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "93D27B978967D61211E06BB1F1062BCF36B6E546F67A575EFA928605F467AF65" }, + { name = "gleeunit", version = "1.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "EC31ABA74256AEA531EDF8169931D775BBB384FED0A8A1BDC4DD9354E3E21826" }, + { name = "simplifile", version = "2.7.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "A2727627B063E87351934C7F7F008F2D1FDB16F6DE0B8C79F9E46459CFC9C164" }, +] + +[requirements] +gleam_stdlib = { version = ">= 1.0.0 and < 2.0.0" } +gleeunit = { version = ">= 1.0.0 and < 2.0.0" } +simplifile = { version = ">= 2.7.0 and < 3.0.0" } diff --git a/src/aoc2026.gleam b/src/aoc2026.gleam @@ -0,0 +1,9 @@ +import day1 +import simplifile +import gleam/io + +pub fn main() -> Nil { + let assert Ok(input) = simplifile.read("input/day1.txt") + io.println(day1.part2(input)) + Nil +} diff --git a/src/day1.gleam b/src/day1.gleam @@ -0,0 +1,51 @@ +import gleam/order +import gleam/dict +import gleam/int +import gleam/list +import gleam/string +import gleam/option.{Some,None} + +pub fn part1(in: String) -> String { + parse_in(in) + |> count_freq() + |> dict.to_list + |> list.sort(fn (tup1, tup2) { + order.negate(int.compare(tup1.1, tup2.1)) + }) + |> list.take(3) + |> list.map(fn (tup) {tup.0}) + |> list.fold(1, int.multiply) + |> int.to_string +} + +pub fn part2(in: String) -> String { + parse_in(in) + |> count_freq() + |> dict.to_list + |> list.sort(fn (tup1, tup2) { + int.compare(tup1.1, tup2.1) + }) + |> list.take(3) + |> list.map(fn (tup) {tup.0}) + |> list.fold(1, int.multiply) + |> int.to_string +} + +fn parse_in(in: String) -> List(Int) { + string.split(in, "\n") + |> list.map(fn (tok) { + let assert Ok(num) = int.parse(tok) + num + }) +} + +fn count_freq(in: List(Int)) -> dict.Dict(Int, Int) { + list.fold(in, dict.new(), fn (acc, num) { + dict.upsert(acc, num, fn (maybe_ct) { + case maybe_ct { + Some(ct) -> ct+1 + None -> 1 + } + }) + }) +} diff --git a/test/aoc2026_test.gleam b/test/aoc2026_test.gleam @@ -0,0 +1,5 @@ +import gleeunit + +pub fn main() -> Nil { + gleeunit.main() +} diff --git a/test/day1_test.gleam b/test/day1_test.gleam @@ -0,0 +1,57 @@ +import day1 + +// gleeunit test functions end in `_test` + +pub fn day1_pt1_test() { + let input = "57 +57 +55 +57 +76 +78 +57 +45 +43 +78 +43 +76 +43 +78 +57 +45 +57 +76 +43 +43 +76" + let result = day1.part1(input) + assert result == "186276" +} + + +pub fn day1_pt2_test() { + let input = "57 +57 +55 +57 +76 +78 +57 +45 +43 +78 +43 +76 +43 +78 +57 +45 +57 +76 +43 +43 +76" + let result = day1.part2(input) + assert result == "193050" +} +