# [Advent Of Code 2022] Day 01 Discussion [SPOILERS]

Today’s the day, and those mischievous elves are back in force.

2 Likes

I did this one in Julia for the first time (outside Exercism), and it went fairly well considering that. One major point of friction was that I hadn’t thought ahead to prepare helper functions like I’ve done in previous years. I’ll work on that some on my lunch break tomorrow.

Edit - Forgot my solution

Summary
``````    function part01()
maximum = 0
current = 0
for l in eachline("../data/day01.txt")
if length(l) != 0
current += parse(Int, l)
else
maximum = max(current, maximum)
current = 0
end
end
maximum
end

function part02()
seen = Int[]
current = 0

#for l in eachline("../data/day01.txt")
if length(l) != 0
current += parse(Int, l)
else
push!(seen, current)
current = 0
end
end

sort(seen, rev = true) |> [1:3] |> sum
end
end
``````
1 Like

I had to do a lot of Haskell coding in the last month. I did not anticipate how much that would interfere with my Elixir code.

I forgot almost all of the Elixir techniques and it did not quite behave like Haskell. So I was frustrated with how to pipe functions and declare anonymous functions. I remember that there were some tricks to make it less ugly, but I cannot find them in the Elixir track.

I mean, look at my code. It works, but it sucks

``````defmodule AdventOfCode.Day01 do
@spec part1(String.t()) :: integer
def part1(args) do
convertTextToSummed(args)
|> Enum.max()
end

@spec part2(String.t()) :: integer
def part2(args) do
convertTextToSummed(args)
|> Enum.sort()
|> Enum.reverse()
|> Enum.take(3)
|> Enum.sum()
end

defp convertTextToSummed(str),
do:
str
|> String.trim()
|> String.split("\n\n")
|> Enum.map(&String.split(&1, "\n"))
|> Enum.map(&Enum.map(&1, fn i -> String.to_integer(i) end))
|> Enum.map(&Enum.sum(&1))
end
``````

On workflow: When I used Clojure, it was a breeze to see the current output in each line while typing. Calva is such an impressive tool. Currently, I have iex running to code, but it has no autocomplete. Non yet optimal.

Using Typescript for this year. Really enjoying using Deno as an alternative to Node. Typescript + ES Modules + top-level async without configuration files is such a game changer for this kind of stuff.

My TypeScript solution
``````interface State {
lines: string[];
elves: number[][];
totalByElf?: number[];
}

export function parse(lines: string[]): State {

const elves: number[][] = [];
let currentElf: number[] = [];

for (const line of lines) {
if(line==="") {
elves.push(currentElf);
currentElf = [];
continue;
}
currentElf.push(parseInt(line, 10));
}

return {lines, elves};
}

function sumNumbers(ns: number[]): number {
return ns.reduce((a, b) => a + b, 0);
}

export function part1(parsed: State): string {
const totalByElf = parsed.elves.map(sumNumbers);

// save totalByElf for part 2
parsed.totalByElf = totalByElf;

return Math.max(...totalByElf).toString();
}

export function part2(parsed: State): string {
const top3 = parsed.totalByElf!.sort((a, b) => b - a).slice(0, 3);
return sumNumbers(top3).toString();
}
``````
1 Like

Apparently we do:

3 Likes

Was anyone else surprised that today is the first day of December and, therefore, the first day of AOC? I had grand plans of building some helper functions, so I could efficiently solve the actual problems, but I was going to do that in Novemeber :(

1 Like

I can relate! For November, I had big plans to fill my knowledge gaps on Javascript, learn Typescript properly then do proper preparation for Advent of Code.

Reality? I’m only in the middle of reviewing my Javascript, didn’t touch the Typescript book yet. Only installed Deno yesterday 7 hours before the puzzle unlocking. Put together some functions to download and read the input, barely knowing what I was doing and went to bed

It’s ok though. It’s a good thing the first puzzles are easier, allow us to get more comfortable with our setup and do some catchup, especially important when learning something new.

Yes, I feel the same. The first puzzle my main struggle is to have a good setup. Especially since I am trying a new language every year.

My favorite solution this year is this one in Python:

I had code to auto-build my file but that failed me. I spent 3 minutes fixing the from-template file and 3 minutes solving it

My AoC Repo (Python)

``````    def part1(self, parsed_input: InputType) -> int:
"""Return the sum calories held by the elf with the most calories."""
return max(sum(i) for i in parsed_input)

def part2(self, parsed_input: InputType, top_n: int = 3) -> int:
"""Return the sum calories held by the top three elves with the most calories."""
return sum(sorted(sum(i) for i in parsed_input)[-top_n:])

def parse_input(self, puzzle_input: str) -> InputType:
"""Parse the input data."""
return [
[int(line) for line in block.splitlines()]
for block in  puzzle_input.split("\n\n")
]
``````

Not related, but definitely thought my eyes were getting worse when I saw this message. Apparently that is a graphic behind a spoiler block?

Sorry, still can’t private message, but also might be useful for someone else that doesn’t recognize the blurred graphic right away.

1 Like

Hey @vaeng

You may want to take a look at the Elixir Forum, where people post their solutions and discuss things: Advent of Code 2022 - Day 1 - Coding Challenges - Elixir Programming Language Forum

Some things I spotted which might improve your code a bit:

Enum.sort/2 supports a `sorter` argument, so you can use `:desc` to avoid the `Enum.reverse/1`

Can be written as `Enum.map(&String.split/1)` as `String.split/1` already splits on linebreaks.
I guess you might mean this with tricks to make it less ugly regarding anonymous functions?
If you use an anonymous functions which have an arity of 1 (accepting a single argument) you can simply do `&String.to_integer/1` instead of `&String.to_integer(&1)`

And maybe a thing of personal preference but which might make the code a bit more straight forward:

After the first split you have a list of elves. To now get rid of three maps in a row, which looks noisy, you could work on each elf within a single map:

``````|> String.split("\n\n")
|> Enum.map(fn elf ->
elf
|> String.split()
...do the other things
end)
``````

I hope this helps and have fun solving the future puzzles. Cheers!

1 Like

Yes, that is totally what I meant. Thank you very much.

Once you get around to creating those helper functions they’ll be there every year from now on. Years ago I created a jar that converts an input text file to common collections such as List of Strings and List of Integers. I still need to bundle up some of the commonly occurring types - Points, Ranges, Grids. That seems to be the extent of the re-use I’ve seen over the years.

My biggest issue this year is I broke my right hand the week before Thanksgiving and have a hard cast on. Makes typing near impossible, and IntelliJ is pretty frustrating with
So far I’m solving the problems as they drop - I’m in California and get them 9pm each day.

Ruby Solution
``````calories = File.read('inputs/01.txt')
.split("\n\n")
.map { |foods| foods.lines.map(&:to_i).sum }
.sort
.reverse

a = calories[0]
b = calories[0..2].sum
``````

You could use the time and get into programming by voice. Voice driven development It’s pretty impressive.

Is there some traditional day when this event starts every year? If so, what is that day??

The first puzzle for the current year’s challenge unlocks at 12am EST on December 1st. A new puzzle unlocks at the same time each subsequent day but the access doesn’t expire so you can do it whenever. You’ll also get access to puzzles from previous years as well.