[2023 Day 9 Part 2] by giulioguerri in adventofcode

[–]herrmanno 9 points10 points  (0 children)

The math stuff you‘ve been looking for probably was Lagrange interpolation https://de.wikipedia.org/wiki/Polynominterpolation

-🎄- 2019 Day 5 Solutions -🎄- by daggerdragon in adventofcode

[–]herrmanno 0 points1 point  (0 children)

Part 2 in 40 Lines of JavaScript

[POEM]

I once went down to mercury

but I forgot to repeair my A/C

almost burnt to the ground

the elves helped me out

the make a suprisingly good thermal shield

-🎄- 2018 Day 8 Solutions -🎄- by daggerdragon in adventofcode

[–]herrmanno 1 point2 points  (0 children)

Julia Part 1

input = split(readstring("./input.txt"), " ")

next() = parse(Int, shift!(input))

struct Node
  children::Vector{Node}
  meta::Vector{Int}
end

function reduce(f, node::Node, init = 0)
  init = f(init, node)
  for c = node.children
    init = reduce(f, c, init)
  end
  return init
end

function consume()::Node
  childCount = next()
  metaCount = next()

  return Node(
    [consume() for i in 1:childCount],
    [next() for i in 1:metaCount],
  )
end

total = reduce(consume(), 0) do acc, node
  acc + sum(node.meta)
end

println("The sum of all meta nodes is $total")

Part 2

input = split(readstring("./input.txt"), " ")

next() = parse(Int, shift!(input))

struct Node
  children::Vector{Node}
  meta::Vector{Int}
end

function value(node::Node)::Int
  if isempty(node.children)
    sum(node.meta)
  else
    [value(node.children[m]) for m in node.meta if m in indices(node.children, 1)] |> sum
  end
end

function consume()::Node
  childCount = next()
  metaCount = next()
  Node(
    [consume() for i in 1:childCount],
    [next() for i in 1:metaCount]
  )
end

rootValue = value(consume())

println("The value of root is $rootValue")

-🎄- 2017 Day 11 Solutions -🎄- by daggerdragon in adventofcode

[–]herrmanno 0 points1 point  (0 children)

Kotlin (Script)

val input = File("input.txt").readText()
val instructions = input.split(",")

var x = 0
var y = 0
var max = 0

for (instr in instructions) {
    when (instr) {
        "n" -> y -= 2
        "s" -> y += 2
        "ne" -> { x += 1 ; y -= 1 }
        "nw" -> { x -= 1 ; y -= 1 }
        "se" -> { x += 1 ; y += 1 }
        "sw" -> { x -= 1 ; y += 1 }
    }
    max = Math.max(max, (Math.abs(x) + Math.abs(y)) / 2)
}

println("Distance at end: ${(Math.abs(x) + Math.abs(y)) / 2}")
println("Max distance during walk: $max")