↤ go back, or return home

jack's advent of code 2022 ramblings

day 4

managed to snag this one faster than yesterdays, although i found due to having pulled out set operations yesterday, my brain went right back to them again, even though clearly there were tricks here to avoid doing that

oh well, currently optimizing for speed of solution, therefore the first method that pops into my head is what i run with :)


click to view my solution

given the sample input:

2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8

assuming input is the above as one big string,

sets =
  input
  |> String.split("\n", trim: true)
  |> Enum.map(fn line ->
    line
    |> String.split(",")
    |> Enum.map(fn pair ->
      pair
      |> String.split("-")
      |> Enum.map(&String.to_integer/1)
      |> Enum.reduce(&Range.new(&1, &2))
      |> MapSet.new()
    end)
  end)

a busier input parse today!

i usually consider ‘input parsing’ to include converting the input into some data structure / set of data structures that are the preamble to the actually problems solving meat

here we take each line, split it by ,, and then iterate over each pair

then we split the inner pair into two values, map them to integers, and use reduce to go from [1,4] to the range 0..4

with this range, we can then use MapSet.new, which will decompose the range into a set of its values

[
  [MapSet.new([2, 3, 4]), MapSet.new([6, 7, 8])],
  [MapSet.new([2, 3]), MapSet.new([4, 5])],
  [MapSet.new([5, 6, 7]), MapSet.new([7, 8, 9])],
  [MapSet.new([2, 3, 4, 5, 6, 7, 8]), MapSet.new([3, 4, 5, 6, 7])],
  [MapSet.new([6]), MapSet.new([4, 5, 6])],
  [MapSet.new([2, 3, 4, 5, 6]), MapSet.new([4, 5, 6, 7, 8])]
]

here is what we end up with after input parsing

yet again, a list of lists appears infront of us

and, for the second day in a row, i have reached for MapSets

part 1

sets
|> Enum.filter(fn [a, b] -> MapSet.subset?(a, b) || MapSet.subset?(b, a) end)
|> Enum.count() # -> 2

then, we filter out every pair in which one MapSet is a subset of the other MapSet, and count it

part 2

sets
|> Enum.filter(fn [a, b] -> MapSet.intersection(a, b) |> Enum.count() != 0 end)
|> Enum.count() # -> 4

similarly, for the second part, we filter out every pair in the intersection of the MapSets results in nothing, i.e. no intersection, and count it

my code for today is clean, but as you can see by quite a few of the solutions bellow, this is quite inefficient :P

the full solution can be found [here]



others

less people here than in previous days, but that might also just be folk taking a break on the weekend

while i write this, its 2:25pm, so some might not gotten around to completing it yet either, i will try my best to go back and review peoples repos to add them, but i am simply human

[Mudkip/AdventOfCode] python src/data/aoc/2022/data
def parse_inputs():
    inputs = open('4.in', 'r').read().splitlines()
    return [(list(map(int, pair[0].split("-"))), list(map(int, pair[1].split("-")))) for pair in [input.split(",") for input in inputs]]

def solve(pairs, condition):
    return len([pair for pair in pairs if condition(pair)])

pairs = parse_inputs()
print(f"Part 1: {solve(pairs, lambda pair: (pair[0][0] >= pair[1][0] and pair[0][1] <= pair[1][1]) or (pair[0][0] <= pair[1][0] and pair[0][1] >= pair[1][1]))}")
print(f"Part 2: {solve(pairs, lambda pair: max(pair[0][0], pair[1][0]) <= min(pair[0][1], pair[1][1]))}")

day 2 of having mudkip’s code inline within this page due to it being so smol

[9:39 AM] flooding 27015 with A2S_INFO: Guess I’m going with writing as little lines of code as possible while not completely jeopardizing performance

and if this is the case, this streak will continue!

[briannamcdonald/advent-of-code-2022] python src/data/aoc/2022/data

compact, list comprehension for input data parsing is clean

actually the list comprehension usage here is nice, i don’t feel like i need a degree in python to understand each statement, yet its still quite compact

nice

[krbarter/Advent-Of-Code-2022] python src/data/aoc/2022/data

a bit less compact, input parsing could be more concise

also using ; to use one line for many statements, not epic

a1 = int(x[0]); a2 = int(x[1]); b1 = int(x[2]); b2 = int(x[3])

this could be done using a map over x to convert it to ints, and then a list unpack to pluck each value out into a variable

[nint8835/AdventOfCode2022] f# & python src/data/aoc/2022/data

nice and functional, but uses the trick that doesn’t require a range/mapset

let tryBothOrders (func: int [] -> int [] -> bool) (pair: int [] []) : bool =
  func pair[0] pair[1] || func pair[1] pair[0]

i like this usage of this function to try both orders of pairs

nice

[hamzahap/AdventOfCode2022] sheets src/data/aoc/2022/data

my laptop fans continue to cry when opening up this browser tab

=IFS(AND(D3>=F3,D3<=G3),1,AND(E3<=G3,E3>=F3),1,AND(F3<=D3,F3>=E3),1,AND(G3<=E3,G3>=D3),1)

its neat to look at this formula here, and while my google-sheets-fu isn’t this strong, i still get whats its doing having seen other solutions that have similar boolean logic in them

[TheCrypticCanadian/advent-of-code-2022] python src/data/aoc/2022/data

the jupyter notebook is coming out here tonight

and, kept for the sake of learnings i assume, ethan has left his ‘initial failure’ as a cell here

nice

this is a pretty minimal range / set solution

nice

[STollenaar/AdventOfCode2022] golang src/data/aoc/2022/data

clean little Range struct here, makes the rest of the code clean

type Range struct {
  min, max int
}

i pick on go for being too simple sometimes, but sometimes that simplicity is nice

[mathieuboudreau/advent] python notebook src/data/aoc/2022/data

nice and simple set-based solution

but i have one comment

def is_subset(subset,superset):
    # Check if the extremas of the subset are within the superset
    if subset[0]>=superset[0] and subset[1]<=superset[1]:
        return True
    else:
        return False

this utility can be made a one-statement function :)

[SteveParson/AOC2022] c, python, & java src/data/aoc/2022/data

input parsing could be a but simplier, but its quaint, so its nice

[CameronSquires/AdventOfCode2022] python src/data/aoc/2022/data

capitalized variable names, not very :snakelang:

could be good to rely on sets instead of hand rolling count functions

[RyanBrushett/adventofcode2022] ruby src/data/aoc/2022/data

clean util function usage here

as usual, i likea-da-ruby

[canetoads.ca] javascript src/data/aoc/2022/data

these lines be long, but they be clean

could have some util functions for input parsing, since there is already util functions for inrange / nrange

still a nice solution though

[apreynolds1989/AdventOfCode2022] typescript src/data/aoc/2022/data

Andrew Reynolds: The majority of my time was spent ensuring I implemented reduce in place of any forEach.

i have influced andrew to learn the ways of the reduce, presumably from previous mentions of how he was close to a functional solution in these ramblings

[davidtgillard/advent-of-code] f# src/data/aoc/2022/data

was curious what the [<AutoOpen] does, seems to be something related to scope

i like the fact that each problem here is passed the input stream as a StreamReader

nice and smol

[EthanDenny/AdventOfCode2022] c++ src/data/aoc/2022/data

a c++ solution, but a pretty compact c++ solution

nice

[lilmert/aoc] rust src/data/aoc/2022/data

“i feel like i smurfed today”

- marty

marty is getting more functional

he is learning

[ericthomasca/adventofcode2022] rust src/data/aoc/2022/data

result usage! nice :)

breaking out each part into util functions, pretty clean

would be nice if like other languages you could destructure the Vec into multiple variables, but after a quick google, it doesn’t look like its possible

[ecumene/advent_of_code] python notebook src/data/aoc/2022/data

todays mitch ai image:

prompt: elves cleaning up a campsite stylized

look at them go

also

i tripped over x1,x2 y1,y2 being strings not integers

it seems like quite a few people tripped over this one, mudkip and keenan included

[bjbemister19/AdventOfCode] rust src/data/aoc/2022/data

a clean rust implementation, feels quite like svens go solution rust-ified

input parsing is clean, which implies the problem solution is clean

clean

[M-ArafatZaman/AdventOfCode] python & c++ src/data/aoc/2022/data

nice, a clean simple solution

good usage of destucturing creating the pair1 & pair2 variables, but destucturing could also be used for pair1/2_start & part1/2_end to condense a little bit

my only comment on pythonic vibes would be to skip defining a main function to call it at the bottom

the more preferable way to do this would be:

def main():
    ...

if __name__ == "__main__":
    main()

as explained [here], this is better since if something imports this file, it will not invoke main, but will still permit importing main / other functions defined in this file to be used elsewhere

but, for the sake of problem solving brevity, its (in my opinion) fine to forget about this and just [write solutions without a def main]

[skanes17/Advent2022] javascript src/data/aoc/2022/data

very nice, very map-y filter-y, therefore good in my books :)

probably my favorute javascript solution


meta

building this summary of each day has been fun, but i started it on friday night, and it has consumed quite a bit of my weekend, but i really do want to keep it up!

there is a [secret page] that i’ve put together to help myself:

  1. get the links to everyones repos / places they are putting their solutions, so i can hunt down if people have completed days or not easier

  2. automagically generate, based on the day, links to each persons solutions some people have a file-per-solution

    so i link directly to those, and some have folders that usually contain part 1 & two, so i link to the folders

    some people are too tricky to link to, so i either have a custom handling solution for them, or fall back to just linking to the repository itself

  3. after making quick inventory of who has completed a day, generate the boilerplate markdown for the ‘others’ section

i still want to write out each block, so that bit isn’t being automated away, i just want during the work week to speed this up :)

all of this is being done [within this repository] if you are curious to peep the inner workings

in a future meta section i will cover more about the static site generator being used here, how i recreated the advent of code look and feel, and other meta goodies


if you want to have your repo added for me to make a note of / talk about here (or have you repo removed), reach out:

email -> me@jackharrhy.com

discord -> <i>jack arthur null</i>#7539