From cf8f487526d484b446a33bf7f39d138e95380c6a Mon Sep 17 00:00:00 2001 From: spamegg Date: Sun, 1 Dec 2024 09:06:02 +0300 Subject: [PATCH 1/3] Solution article for 2024 Day 01 - Also added link to my community solution --- docs/2024/puzzles/day01.md | 62 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/docs/2024/puzzles/day01.md b/docs/2024/puzzles/day01.md index a207fe083..1500d11d7 100644 --- a/docs/2024/puzzles/day01.md +++ b/docs/2024/puzzles/day01.md @@ -6,7 +6,69 @@ import Solver from "../../../../../website/src/components/Solver.js" https://adventofcode.com/2024/day/1 +## Solution Summary + +1. Parse the input to split it into two lists (left/right), each sorted in increasing order. +2. Find the distance scores (for `part1`) and the similarity scores (for `part2`). +3. Sum the scores. + +### Parsing + +Our parser iterates over the lines, extracts the pair of numbers from each line, +then splits them into two lists (lefts and rights), and separately sorts the lists. +Therefore it looks like this: + +```scala +def parse(input: String): (Seq[Long], Seq[Long]) = + // Extract pairs of numbers from each line + val pairs = input + .linesIterator + .map(line => line.split(" ").map(_.toLong)) + .toSeq + + // Group the left and right members from each pair, sort them + val lefts = pairs.map(_.head).toSeq.sorted + val rights = pairs.map(_.last).toSeq.sorted + (lefts, rights) +``` + +### Part 1 + +Now that the lefts and rights are sorted in increasing order, we can zip them, +so that the first smallest on the left is paired with the first smallest on the right, +the second smallest on the left is paired with the second smallest on the right, and so on. +Then we can find the distances between them, and sum the distances: + +```scala +def part1(input: String): String = + val (lefts, rights) = parse(input) + lefts + .zip(rights) + .map((left, right) => math.abs(left - right)) // distances + .sum +end part1 +``` + +### Part 2 + +Very similar, but instead of distances, we find a left number's similarity on the right list. +We do this by counting how many times the left number occurs on the right list, +then multiply that count by the number itself. +Finally we sum the similarity scores of all the left numbers: + +```scala +def part2(input: String): String = + val (lefts, rights) = parse(input) + lefts + .map(left => rights.count(_ == left) * left) // similarity scores + .sum +end part2 +``` + + ## Solutions from the community +- [Solution](https://github.com/spamegg1/aoc/blob/master/2024/01/01.worksheet.sc#L122) by [Spamegg](https://github.com/spamegg1/) + Share your solution to the Scala community by editing this page. You can even write the whole article! [See here for the expected format](https://github.com/scalacenter/scala-advent-of-code/discussions/424) From df1f996d0ad1086d58e4643b9c6797c86c3a62fd Mon Sep 17 00:00:00 2001 From: spamegg Date: Sun, 1 Dec 2024 18:25:03 +0300 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Jamie Thompson --- docs/2024/puzzles/day01.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/2024/puzzles/day01.md b/docs/2024/puzzles/day01.md index 1500d11d7..dfb2e4e19 100644 --- a/docs/2024/puzzles/day01.md +++ b/docs/2024/puzzles/day01.md @@ -40,7 +40,7 @@ the second smallest on the left is paired with the second smallest on the right, Then we can find the distances between them, and sum the distances: ```scala -def part1(input: String): String = +def part1(input: String): Long = val (lefts, rights) = parse(input) lefts .zip(rights) @@ -57,7 +57,7 @@ then multiply that count by the number itself. Finally we sum the similarity scores of all the left numbers: ```scala -def part2(input: String): String = +def part2(input: String): Long = val (lefts, rights) = parse(input) lefts .map(left => rights.count(_ == left) * left) // similarity scores From 731def27901dc66b6945cbd60d088e21e4ca4e79 Mon Sep 17 00:00:00 2001 From: spamegg Date: Mon, 2 Dec 2024 18:27:44 +0300 Subject: [PATCH 3/3] added author name --- docs/2024/puzzles/day01.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/2024/puzzles/day01.md b/docs/2024/puzzles/day01.md index aacf5f2d6..5d0df78c5 100644 --- a/docs/2024/puzzles/day01.md +++ b/docs/2024/puzzles/day01.md @@ -2,6 +2,8 @@ import Solver from "../../../../../website/src/components/Solver.js" # Day 1: Historian Hysteria +by [@spamegg1](https://github.com/spamegg1) + ## Puzzle description https://adventofcode.com/2024/day/1