You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/2024/puzzles/day01.md
+68-2Lines changed: 68 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,79 @@ import Solver from "../../../../../website/src/components/Solver.js"
2
2
3
3
# Day 1: Historian Hysteria
4
4
5
+
by [@spamegg1](https://github.com/spamegg1)
6
+
5
7
## Puzzle description
6
8
7
9
https://adventofcode.com/2024/day/1
8
10
9
-
## Solutions from the community
11
+
## Solution Summary
12
+
13
+
1. Parse the input to split it into two lists (left/right), each sorted in increasing order.
14
+
2. Find the distance scores (for `part1`) and the similarity scores (for `part2`).
15
+
3. Sum the scores.
16
+
17
+
### Parsing
18
+
19
+
Our parser iterates over the lines, extracts the pair of numbers from each line,
20
+
then splits them into two lists (lefts and rights), and separately sorts the lists.
21
+
Therefore it looks like this:
22
+
23
+
```scala
24
+
defparse(input: String): (Seq[Long], Seq[Long]) =
25
+
// Extract pairs of numbers from each line
26
+
valpairs= input
27
+
.linesIterator
28
+
.map(line => line.split("").map(_.toLong))
29
+
.toSeq
30
+
31
+
// Group the left and right members from each pair, sort them
32
+
vallefts= pairs.map(_.head).toSeq.sorted
33
+
valrights= pairs.map(_.last).toSeq.sorted
34
+
(lefts, rights)
35
+
```
10
36
11
-
[Solution](https://github.com/Jannyboy11/AdventOfCode2024/blob/master/src/main/scala/day01/Day01.scala) of [Jan Boerman](https://x.com/JanBoerman95)
37
+
### Part 1
38
+
39
+
Now that the lefts and rights are sorted in increasing order, we can zip them,
40
+
so that the first smallest on the left is paired with the first smallest on the right,
41
+
the second smallest on the left is paired with the second smallest on the right, and so on.
42
+
Then we can find the distances between them, and sum the distances:
-[Solution](https://github.com/Jannyboy11/AdventOfCode2024/blob/master/src/main/scala/day01/Day01.scala) of [Jan Boerman](https://x.com/JanBoerman95)
72
+
-[Solution](https://github.com/bishabosha/advent-of-code-2024/blob/main/2024-day01.scala) by [Jamie Thompson](https://github.com/bishabosha)
73
+
-[Solution](https://github.com/nikiforo/aoc24/blob/main/src/main/scala/io/github/nikiforo/aoc24/D1T2.scala) by [Artem Nikiforov](https://github.com/nikiforo)
74
+
-[Solution](https://github.com/rmarbeck/advent2024/tree/main/day1) by [Raphaël Marbeck](https://github.com/rmarbeck)
75
+
-[Solution](https://github.com/Philippus/adventofcode/blob/main/src/main/scala/adventofcode2024/Day01.scala) by [Philippus Baalman](https://github.com/philippus)
76
+
-[Solution](https://scastie.scala-lang.org/Sporarum/jVlQBCvoQXCtlK4ryIn42Q/4) by [Quentin Bernet](https://github.com/Sporarum)
77
+
-[Solution](https://github.com/jnclt/adventofcode2024/blob/main/day01/historian-hysteria.sc) by [jnclt](https://github.com/jnclt)
12
78
13
79
Share your solution to the Scala community by editing this page.
14
80
You can even write the whole article! [See here for the expected format](https://github.com/scalacenter/scala-advent-of-code/discussions/424)
0 commit comments