Skip to content

Instantly share code, notes, and snippets.

@jexp
Last active December 4, 2022 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jexp/daff45f4eb0fa618686417b7b9b6f2d9 to your computer and use it in GitHub Desktop.
Save jexp/daff45f4eb0fa618686417b7b9b6f2d9 to your computer and use it in GitHub Desktop.
Advent of Code 2022 in some languages
with split($input, "\n\n") as elves
unwind elves as elf
return reduce(s=0, c in split(elf,"\n") | s + toInteger(c)) as total
order by total desc limit 1;
with split($input, "\n\n") as elves
unwind elves as elf
with reduce(s=0, c in split(elf,"\n") | s + toInteger(c)) as total
order by total desc limit 3
return sum(total);
# Generated by chat.open.ai chatgpt
# only had to fix: currentTotal += int(calories)
# initialize variables to track the Elf carrying the most Calories and their total Calories
maxCalories = 0
maxElf = 0
# initialize the current Elf and their total Calories to 0
currentElf = 0
currentTotal = 0
# iterate through the list of food item Calories
for calories in foodCalories:
# if the current item is a blank line, switch to the next Elf and reset their total Calories
if calories == '':
currentElf += 1
currentTotal = 0
continue
# add the current food item's Calories to the current Elf's total Calories
currentTotal += int(calories)
# if the current Elf's total Calories is higher than the maximum seen so far, update the maximum Calories and Elf
if currentTotal > maxCalories:
maxCalories = currentTotal
maxElf = currentElf
# print the Elf carrying the most Calories and their total Calories
print(f'Elf {maxElf} is carrying the most Calories: {maxCalories}')
with 'vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw' as data
unwind split(data,"\n") as row
with split(row,"") as row
with size(row) as len, row[0..size(row)/2] as part1, row[size(row)/2..] as part2
with apoc.coll.intersection(part1,part2)[0] as delta
with delta, case
when delta >= 'a' and delta <= 'z' then apoc.text.charAt(delta,0) - apoc.text.charAt('a',0) + 1
when delta >= 'A' and delta <= 'Z' then apoc.text.charAt(delta,0) - apoc.text.charAt('A',0) + 27
else 0
end as rate
return sum(rate);
// part 2
with 'vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw' as data
with split(data,"\n") as rows
unwind range(0,size(rows)-3,3) as index
call { with index, rows
unwind rows[index..index+3] as row
unwind apoc.coll.toSet(split(row,"")) as delta
with delta, count(*) as total where total = 3
return delta, total
}
with delta, case
when delta >= 'a' and delta <= 'z' then apoc.text.charAt(delta,0) - apoc.text.charAt('a',0) + 1
when delta >= 'A' and delta <= 'Z' then apoc.text.charAt(delta,0) - apoc.text.charAt('A',0) + 27
else 0
end as rate
return sum(rate);
// part 1
with '2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8' as input
unwind split(input,"\n") as assignment
with split(assignment,",") as pairs
with split(pairs[0],"-") as range1, split(pairs[1],"-") as range2
with range(toInteger(range1[0]),toInteger(range1[1])) as range1,
range(toInteger(range2[0]),toInteger(range2[1])) as range2
where apoc.coll.containsAll(range1, range2) OR
apoc.coll.containsAll(range2, range1)
return count(*);
// part 2
with '2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8' as input
unwind split(input,"\n") as assignment
with split(assignment,",") as pairs
with split(pairs[0],"-") as range1, split(pairs[1],"-") as range2
with range(toInteger(range1[0]),toInteger(range1[1])) as range1,
range(toInteger(range2[0]),toInteger(range2[1])) as range2
where size(apoc.coll.intersection(range1, range2)) > 0
return count(*);
@jexp
Copy link
Author

jexp commented Dec 3, 2022

Day 2 is google sheets with xlookup =XLOOKUP(A2,Ratings!$A$11:$A$19,Ratings!$B$11:$B$19)

Bildschirmfoto 2022-12-03 um 15 39 15

Bildschirmfoto 2022-12-03 um 15 39 23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment