main
Adam Millerchip 11 months ago
parent 675985e57d
commit bd4acc8f17
  1. 995
      2018/day1/input
  2. 5
      2018/day1/part1.exs
  3. 27
      2018/day1/part2.exs
  4. 91
      2018/day10/day10.exs
  5. 374
      2018/day10/input
  6. 93
      2018/day11/day11.exs
  7. 101
      2018/day12/day12.exs
  8. 34
      2018/day12/input
  9. 179
      2018/day13/day13.exs
  10. 150
      2018/day13/input
  11. 6
      2018/day13/test
  12. 7
      2018/day13/test2
  13. 155
      2018/day14/day14.exs
  14. 250
      2018/day2/input
  15. 33
      2018/day2/part1.exs
  16. 30
      2018/day2/part2.exs
  17. 57
      2018/day3/day3.exs
  18. 1317
      2018/day3/input
  19. 97
      2018/day4/day4.exs
  20. 1106
      2018/day4/input
  21. 41
      2018/day5/day5.exs
  22. 1
      2018/day5/input
  23. 118
      2018/day6/day6.exs
  24. 50
      2018/day6/input
  25. 125
      2018/day7/day7.exs
  26. 101
      2018/day7/input
  27. 52
      2018/day8/day8.exs
  28. 1
      2018/day8/input
  29. 102
      2018/day9/day9.exs
  30. 1
      2018/day9/input

@ -0,0 +1,995 @@
-4
+9
+6
+8
+3
+14
+11
+7
-12
+4
-15
+2
-12
-4
+15
-8
-16
-4
+14
-11
-19
-2
+15
-4
-5
-1
+15
-8
+1
+10
+7
+18
+1
+10
+11
+17
-16
+4
-12
+14
+9
+6
+7
+10
+14
+7
+6
-3
+19
+18
+4
+16
+17
+11
+9
+7
-8
-1
-5
-12
-7
+4
-5
+2
+4
+3
+20
-19
+18
+12
+14
+5
-14
+7
-19
-20
+6
-3
+7
+15
+12
+11
+15
+2
+3
-17
-18
+1
+13
-3
+14
-1
-8
-9
-12
+15
-8
+10
+11
+15
+4
+6
+16
+1
+3
+13
-6
+14
-3
-12
-12
+14
+6
-10
+20
+5
+18
+11
+3
+17
+2
-9
-17
-14
+2
+9
+11
-2
+14
+5
-7
+18
-6
-9
-9
-10
-12
-16
-1
+14
-15
-9
-2
-6
-11
-14
-11
-19
+11
-12
-18
-5
+11
+18
+17
-5
-19
+14
+11
-2
-14
+17
+8
+3
+12
-9
+1
+23
+7
-12
+24
+10
-2
+6
+20
-16
-17
+19
-7
+11
+14
+21
-18
+1
+4
+8
-2
-5
-12
-13
+7
+19
+2
+8
+9
+8
+2
+17
+15
+18
+18
+18
-2
-13
-18
-12
-1
+17
-14
-15
+8
+20
+9
-7
+11
-15
-16
-11
-9
+11
+19
+15
+4
+15
+9
-3
+5
-18
+6
+17
+15
-8
-13
+16
+8
+17
+2
+18
+2
+2
+4
-10
+11
-3
+8
+2
+3
-12
+6
+12
-1
+21
-13
-17
-10
+13
-6
-18
-4
-6
-5
+18
-20
-6
-7
-17
-6
-18
+9
+16
+14
+10
+16
+1
-4
+1
+15
-9
-5
-4
-7
-7
-1
+13
-15
-1
-22
-19
+10
-8
-5
+16
-12
+17
+7
+4
-7
+4
+6
+5
-2
-1
+20
-8
-3
+7
-47
-10
+19
-27
-3
+13
-26
+1
+8
-30
-14
+3
+17
-16
-2
-11
+14
-8
-7
-10
-8
+20
+23
+26
+12
-25
-88
+4
+1
-29
-5
+16
-19
+15
+19
-27
-8
-18
-9
-12
-10
+14
-13
-14
-10
-21
-14
+6
-1
+21
+14
+6
+21
-2
+17
+20
-5
+17
-4
-14
-9
-11
-14
+17
+20
+10
+20
-80
+15
-27
-42
+7
-18
+14
-4
-16
-3
+10
-8
-25
-14
-3
+24
+3
+18
-35
+54
+29
-35
+63
+4
-119
-24
-57508
+1
-10
-10
-17
-8
-9
+4
-7
+19
+2
+19
+8
-16
-19
-2
+5
+2
+10
+11
-6
-18
+11
-17
-17
+12
+4
-2
-17
-5
+7
-18
-14
-3
-18
-16
+18
-1
-10
-8
-13
+12
-4
-9
+6
+11
-5
+9
+13
+16
+15
-5
+10
+13
-19
-16
-13
+5
-12
+8
+1
+14
+23
-1
+17
-15
-19
-9
+8
-2
+19
-13
-15
+2
+12
-15
-16
-20
-2
+14
+6
-1
-1
-3
-9
-3
-15
+6
+4
-8
+9
-14
-10
+9
-15
-16
-3
-18
-9
+18
-8
-16
-18
+14
+17
+9
+12
-7
+2
+15
+12
-7
-11
+13
+8
+5
+2
+10
-19
-12
-20
+10
+7
+19
-8
-7
+19
-18
-15
-9
-12
-18
+2
+12
+10
-5
+7
-15
+1
-11
-18
-8
-16
-16
+13
-19
-10
-5
-5
-18
+13
+3
+16
+17
+15
+19
-11
+15
-12
+2
+17
+17
-16
-15
+5
+20
-12
+5
+2
+7
+15
-6
-17
+18
-20
-8
-15
-18
-1
-20
-5
+18
-11
-15
+2
-15
-15
+8
-18
+1
+1
+5
+5
+11
+5
+15
+16
+19
-4
-7
-5
+8
-2
-19
+7
-6
+19
+17
+9
+2
+1
-15
-2
-6
-12
+19
-16
-9
+4
-3
+21
+6
+27
+14
-22
+18
+12
+18
+13
-10
+8
-13
-6
+10
-12
-17
-38
+21
-19
+18
-23
-7
+17
+8
+59
+3
-19
+22
+17
+15
+5
+24
+13
+15
+8
+18
-17
+16
+16
+20
+1
+7
-16
-15
+2
-18
-9
+1
+13
+5
-17
+22
-13
-21
-12
-4
+20
-31
-12
-34
+10
+2
+20
+22
-28
-19
-19
-2
+133
+17
-10
+18
-10
+6
+20
-10
+3
+11
+14
-5
+3
+20
+11
-9
-12
-1
-3
-19
+12
+14
+8
+12
+2
+18
+7
-19
+11
-22
+13
-9
+15
-13
+1
+20
+14
-9
+11
-18
+13
-21
-20
-16
+7
-14
+12
-1
+8
-16
-16
-13
+7
-3
+19
-22
-10
+6
+1
+16
+4
-1
-12
-16
-10
+29
+8
+18
-9
+11
+18
-6
-2
+4
-3
-8
-9
+21
+13
-23
-26
-22
-9
-24
-11
+2
-18
-42
+13
+46
-96
-7
-9
-131
-6
-21
+11
-18
+14
+28
-18
-11
-18
-48
+7
+69
+9
-24
-15
+46
-10
+137
-57522
+5
+4
+3
+16
-3
-18
+3
-9
-10
+11
+12
-8
-18
-18
-4
+18
+19
-4
-19
-19
+4
-15
+3
-16
-2
+12
+16
+9
+10
+13
+1
+21
+17
+18
+5
-4
-2
+15
+13
+2
+7
-19
+1
-16
-7
-10
+18
+12
-21
-7
+3
+16
+14
-13
+17
+2
+7
-2
-10
-17
+9
-5
+14
+13
+7
+15
-13
+1
-13
+16
-2
-5
-7
+15
+13
-8
-1
+18
+16
-15
-7
+11
+10
+16
-12
+15
-16
-14
+8
-16
-4
-4
-19
-17
-4
+18
+1
+14
-9
-15
+19
+8
+19
+14
+20
-14
+1
-18
+4
-3
+2
-20
+10
-2
+1
-3
-2
-3
+12
+6
+6
+11
+17
-7
+20
-16
+5
+6
-39
-26
-31
-29
+2
-19
+18
-6
-16
+14
+4
-19
+18
-14
-1
-10
-4
-17
+6
+17
+1
+1
-12
+21
-12
+16
-12
+16
-27
+22
-2
+30
+1
-3
+7
+23
-9
+20
+34
-142
-7
+5
+16
-23
-9
+7
-2
+12
-15
-4
-19
-16
+15
+13
+4
+4
-16
-7
+10
+17
-6
-15
+11
-5
-8
-18
+115524

@ -0,0 +1,5 @@
File.stream!("input")
|> Stream.map(&String.trim/1)
|> Stream.map(&String.to_integer/1)
|> Enum.sum()
|> IO.puts()

@ -0,0 +1,27 @@
defmodule Part2 do
def mark_seen(change, {prev_freq, seen}) do
freq = prev_freq + change
marked_freq = {freq, MapSet.member?(seen, freq)}
acc = {freq, MapSet.put(seen, freq)}
{[marked_freq], acc}
end
def run do
File.stream!("input")
|> Stream.map(&String.trim/1)
# Trying to work out why can't cycle over the stream
# Can't reproduce with this
# Stream.cycle([1]) |> Stream.transform(0, fn one, prev -> {[prev + one], prev + one} end) |> Enum.find(fn x -> x == 25 end)
# So maybe it's because a combination of the accumulator in the stream and Enum.find
|> Stream.map(&String.to_integer/1)
|> Stream.cycle()
|> Stream.transform({0, MapSet.new([0])}, &mark_seen/2)
|> Enum.find(fn {_freq, seen} -> seen end)
|> elem(0)
|> IO.puts()
end
end
Part2.run()

@ -0,0 +1,91 @@
defmodule Star do
defstruct [:pos_x, :pos_y, :vel_x, :vel_y]
def move_all(stars, secs) do
Enum.map(stars, &move(&1, secs))
end
def move(star, secs) do
%Star{star | pos_x: star.pos_x + star.vel_x * secs, pos_y: star.pos_y + star.vel_y * secs}
end
end
defmodule Day10 do
@input_pattern ~r/=< ?(-?\d+), ?(-?\d+)>.*=< ?(-?\d+), ?(-?\d+)>/
def parse_line(line) do
[pos_x, pos_y, v_x, v_y] =
Regex.run(@input_pattern, line, capture: :all_but_first)
|> Enum.map(&String.to_integer/1)
%Star{pos_x: pos_x, pos_y: pos_y, vel_x: v_x, vel_y: v_y}
end
def init do
File.stream!("input")
|> Enum.map(&parse_line/1)
end
# Start with the first star, then compare all the others to find the extremities
def bounding_box([%Star{pos_x: x, pos_y: y} | _] = stars) do
Enum.reduce(stars, {x, y, x, y}, fn star, {min_x, min_y, max_x, max_y} ->
{
min(star.pos_x, min_x),
min(star.pos_y, min_y),
max(star.pos_x, max_x),
max(star.pos_y, max_y)
}
end)
end
def draw(stars) do
starfield = MapSet.new(stars, fn star -> {star.pos_x, star.pos_y} end)
{min_x, min_y, max_x, max_y} = bounding_box(stars)
grid =
for y <- min_y..max_y, x <- min_x..max_x do
eol = if x === max_x, do: "\n", else: ""
char = if MapSet.member?(starfield, {x, y}), do: "#", else: " "
char <> eol
end
Enum.join(grid)
end
def aligned?(stars) do
starfield = MapSet.new(stars, fn star -> {star.pos_x, star.pos_y} end)
Enum.all?(stars, &aligned?(&1, starfield))
end
def aligned?(star, starfield) do
neighbours = [
{star.pos_x - 1, star.pos_y - 1},
{star.pos_x - 1, star.pos_y},
{star.pos_x - 1, star.pos_y + 1},
{star.pos_x, star.pos_y - 1},
{star.pos_x, star.pos_y + 1},
{star.pos_x + 1, star.pos_y - 1},
{star.pos_x + 1, star.pos_y},
{star.pos_x + 1, star.pos_y + 1}
]
Enum.any?(neighbours, &MapSet.member?(starfield, &1))
end
def wait_for_alignment(stars, count) do
if aligned?(stars) do
{stars, count}
else
stars |> Star.move_all(1) |> wait_for_alignment(count + 1)
end
end
def go do
{stars, count} = init() |> wait_for_alignment(0)
IO.puts(draw(stars))
IO.puts("#{count} seconds")
end
end
Day10.go()

@ -0,0 +1,374 @@
position=<-20620, -41485> velocity=< 2, 4>
position=<-51844, 41770> velocity=< 5, -4>
position=<-51817, -20670> velocity=< 5, 2>
position=< 21000, 52179> velocity=<-2, -5>
position=< 21051, 31360> velocity=<-2, -3>
position=< 41859, -41487> velocity=<-4, 4>
position=< 41830, 31361> velocity=<-4, -3>
position=<-51817, -51895> velocity=< 5, 5>
position=<-51836, -41484> velocity=< 5, 4>
position=< 21032, -51887> velocity=<-2, 5>
position=<-31003, -41482> velocity=< 3, 4>
position=< 52261, -51890> velocity=<-5, 5>
position=<-51836, 10547> velocity=< 5, -1>
position=< 10614, -10259> velocity=<-1, 1>
position=<-10194, 41776> velocity=< 1, -4>
position=<-10186, 10550> velocity=< 1, -1>
position=< 31431, -51892> velocity=<-3, 5>
position=<-51804, 52175> velocity=< 5, -5>
position=< 10601, 52183> velocity=<-1, -5>
position=<-51812, 10548> velocity=< 5, -1>
position=< 10633, 20959> velocity=<-1, -2>
position=<-41417, -41480> velocity=< 4, 4>
position=< 10628, 31360> velocity=<-1, -3>
position=<-10160, -20666> velocity=< 1, 2>
position=<-51793, -20673> velocity=< 5, 2>
position=<-41430, -41485> velocity=< 4, 4>
position=< 21032, -20670> velocity=<-2, 2>
position=<-30987, -51891> velocity=< 3, 5>
position=< 52280, -31080> velocity=<-5, 3>
position=<-30995, -10266> velocity=< 3, 1>
position=<-10177, -51888> velocity=< 1, 5>
position=<-30995, 10551> velocity=< 3, -1>
position=< 21028, 20961> velocity=<-2, -2>
position=< 31412, -51895> velocity=<-3, 5>
position=< 31467, -10260> velocity=<-3, 1>
position=< 31407, -51895> velocity=<-3, 5>
position=< 10636, 41773> velocity=<-1, -4>
position=< 31418, -51892> velocity=<-3, 5>
position=<-20620, 41773> velocity=< 2, -4>
position=<-10163, -10265> velocity=< 1, 1>
position=<-20594, -10264> velocity=< 2, 1>
position=< 52266, -10262> velocity=<-5, 1>
position=< 52223, 52178> velocity=<-5, -5>
position=< 21019, -20666> velocity=<-2, 2>
position=<-20620, 20958> velocity=< 2, -2>
position=< 21016, 20960> velocity=<-2, -2>
position=< 10593, -10259> velocity=<-1, 1>
position=< 21045, 31365> velocity=<-2, -3>
position=< 10609, 10548> velocity=<-1, -1>
position=< 21016, -20666> velocity=<-2, 2>
position=< 10598, -10265> velocity=<-1, 1>
position=<-41394, 20955> velocity=< 4, -2>
position=<-20612, -10262> velocity=< 2, 1>
position=<-30987, -41489> velocity=< 3, 4>
position=< 52261, 31361> velocity=<-5, -3>
position=< 31408, 31364> velocity=<-3, -3>
position=<-10181, -31076> velocity=< 1, 3>
position=<-20625, -10268> velocity=< 2, 1>
position=< 41846, 10551> velocity=<-4, -1>
position=<-20588, -31074> velocity=< 2, 3>
position=<-41407, 31364> velocity=< 4, -3>
position=<-20577, 41767> velocity=< 2, -4>
position=< 21040, 31361> velocity=<-2, -3>
position=<-20586, -51892> velocity=< 2, 5>
position=<-41386, 31364> velocity=< 4, -3>
position=< 21001, 20953> velocity=<-2, -2>
position=<-51797, -20671> velocity=< 5, 2>
position=<-20620, -41484> velocity=< 2, 4>
position=< 31433, -51887> velocity=<-3, 5>
position=< 10617, 31363> velocity=<-1, -3>
position=< 10598, 52175> velocity=<-1, -5>
position=<-41413, -51891> velocity=< 4, 5>
position=<-41386, -10260> velocity=< 4, 1>
position=<-51846, -20671> velocity=< 5, 2>
position=< 52273, -51892> velocity=<-5, 5>
position=<-51836, -51896> velocity=< 5, 5>
position=<-20596, 20959> velocity=< 2, -2>
position=< 41814, 31360> velocity=<-4, -3>
position=< 10620, -31073> velocity=<-1, 3>
position=<-41438, 31360> velocity=< 4, -3>
position=< 21060, 31361> velocity=<-2, -3>
position=<-20628, -41487> velocity=< 2, 4>
position=<-10185, -51896> velocity=< 1, 5>
position=<-31022, -20671> velocity=< 3, 2>
position=< 31407, 10551> velocity=<-3, -1>
position=< 41830, -51887> velocity=<-4, 5>
position=< 52263, 52177> velocity=<-5, -5>
position=<-41413, -10267> velocity=< 4, 1>
position=< 10653, -10260> velocity=<-1, 1>
position=<-20628, -31074> velocity=< 2, 3>
position=< 41830, -51892> velocity=<-4, 5>
position=<-51822, 20953> velocity=< 5, -2>
position=< 52221, 52181> velocity=<-5, -5>
position=<-20583, -41488> velocity=< 2, 4>
position=<-51813, -31082> velocity=< 5, 3>
position=< 41822, 52175> velocity=<-4, -5>
position=<-41442, -41482> velocity=< 4, 4>
position=<-10172, -31078> velocity=< 1, 3>
position=<-10178, -10263> velocity=< 1, 1>
position=<-51797, 31360> velocity=< 5, -3>
position=< 10650, 31364> velocity=<-1, -3>
position=<-51798, 10550> velocity=< 5, -1>
position=< 31444, -51893> velocity=<-3, 5>
position=< 21008, -51888> velocity=<-2, 5>
position=<-51792, -31078> velocity=< 5, 3>
position=<-30991, 20960> velocity=< 3, -2>
position=< 10606, -10263> velocity=<-1, 1>
position=<-41429, 31363> velocity=< 4, -3>
position=< 41843, 20960> velocity=<-4, -2>
position=< 31423, -51896> velocity=<-3, 5>
position=<-20586, 20957> velocity=< 2, -2>
position=< 21002, -20675> velocity=<-2, 2>
position=< 21056, 20953> velocity=<-2, -2>
position=< 31420, 10549> velocity=<-3, -1>
position=< 21029, 31365> velocity=<-2, -3>
position=< 21048, 20955> velocity=<-2, -2>
position=< 31463, -10264> velocity=<-3, 1>
position=<-31007, -41489> velocity=< 3, 4>
position=<-20595, -20675> velocity=< 2, 2>
position=<-51849, -20673> velocity=< 5, 2>
position=< 41856, -10264> velocity=<-4, 1>
position=<-31011, -20671> velocity=< 3, 2>
position=<-31033, 41767> velocity=< 3, -4>
position=<-10165, 20956> velocity=< 1, -2>
position=<-41418, -20667> velocity=< 4, 2>
position=< 21024, 41775> velocity=<-2, -4>
position=<-20572, 10547> velocity=< 2, -1>
position=<-41383, 52181> velocity=< 4, -5>
position=<-31022, 20956> velocity=< 3, -2>
position=< 31415, -51889> velocity=<-3, 5>
position=<-10184, -51887> velocity=< 1, 5>
position=<-41418, 10548> velocity=< 4, -1>
position=< 52238, 10555> velocity=<-5, -1>
position=<-41439, 10546> velocity=< 4, -1>
position=< 21013, -51887> velocity=<-2, 5>
position=< 52247, 41767> velocity=<-5, -4>
position=<-20599, -51895> velocity=< 2, 5>
position=< 41823, 31364> velocity=<-4, -3>
position=<-41394, 41773> velocity=< 4, -4>
position=<-41394, 10547> velocity=< 4, -1>
position=<-31027, -51893> velocity=< 3, 5>
position=< 31420, 10554> velocity=<-3, -1>
position=< 52258, -10267> velocity=<-5, 1>
position=< 52277, -20672> velocity=<-5, 2>
position=< 10601, -41488> velocity=<-1, 4>
position=< 52258, 31362> velocity=<-5, -3>
position=<-31031, -31078> velocity=< 3, 3>
position=<-30987, -31073> velocity=< 3, 3>
position=<-30990, 41775> velocity=< 3, -4>
position=< 31432, -41489> velocity=<-3, 4>
position=<-31001, 41767> velocity=< 3, -4>
position=<-20588, -20675> velocity=< 2, 2>
position=<-20591, 41769> velocity=< 2, -4>
position=<-31003, 20953> velocity=< 3, -2>
position=<-51793, 52178> velocity=< 5, -5>
position=<-41410, 31361> velocity=< 4, -3>
position=<-20580, 31369> velocity=< 2, -3>
position=<-20594, 31360> velocity=< 2, -3>
position=< 41855, -41487> velocity=<-4, 4>
position=<-10173, 20954> velocity=< 1, -2>
position=< 21016, 20954> velocity=<-2, -2>
position=< 41843, -51888> velocity=<-4, 5>
position=< 52241, -51887> velocity=<-5, 5>
position=<-51816, -31082> velocity=< 5, 3>
position=<-31035, -20666> velocity=< 3, 2>
position=< 41857, 31366> velocity=<-4, -3>
position=< 41814, -51896> velocity=<-4, 5>
position=< 31439, 20957> velocity=<-3, -2>
position=< 21036, 31366> velocity=<-2, -3>
position=<-20615, -41488> velocity=< 2, 4>
position=< 10634, -20674> velocity=<-1, 2>
position=< 52269, 20958> velocity=<-5, -2>
position=<-41441, -20671> velocity=< 4, 2>
position=<-30995, -20670> velocity=< 3, 2>
position=< 41872, -20672> velocity=<-4, 2>
position=< 52222, 31364> velocity=<-5, -3>
position=<-41434, 20956> velocity=< 4, -2>
position=< 41814, 10554> velocity=<-4, -1>
position=< 41833, -41480> velocity=<-4, 4>
position=<-31007, 31368> velocity=< 3, -3>
position=< 10609, -41481> velocity=<-1, 4>
position=<-51844, 41769> velocity=< 5, -4>
position=< 41814, 31361> velocity=<-4, -3>
position=< 41818, 20957> velocity=<-4, -2>
position=< 41859, -51888> velocity=<-4, 5>
position=< 31455, -20667> velocity=<-3, 2>
position=< 21045, 10549> velocity=<-2, -1>
position=< 10609, 10550> velocity=<-1, -1>
position=<-20628, 10552> velocity=< 2, -1>
position=<-41431, -20671> velocity=< 4, 2>
position=< 41858, 52181> velocity=<-4, -5>
position=< 10595, 20953> velocity=<-1, -2>
position=<-10208, 20960> velocity=< 1, -2>
position=<-51817, 10553> velocity=< 5, -1>
position=<-20568, -31074> velocity=< 2, 3>
position=< 52241, 10555> velocity=<-5, -1>
position=< 31452, 41774> velocity=<-3, -4>
position=< 41865, -41485> velocity=<-4, 4>
position=< 10638, -31080> velocity=<-1, 3>
position=<-20571, 31364> velocity=< 2, -3>
position=<-41394, 31363> velocity=< 4, -3>
position=< 31407, 10546> velocity=<-3, -1>
position=< 41870, 31366> velocity=<-4, -3>
position=< 21040, -31074> velocity=<-2, 3>
position=< 10614, -20666> velocity=<-1, 2>
position=<-51801, 52182> velocity=< 5, -5>
position=<-31027, 41767> velocity=< 3, -4>
position=< 31415, -20671> velocity=<-3, 2>
position=<-51817, -41481> velocity=< 5, 4>
position=< 31452, 52177> velocity=<-3, -5>
position=< 21027, -20675> velocity=<-2, 2>
position=<-31035, 20957> velocity=< 3, -2>
position=<-51814, -31077> velocity=< 5, 3>
position=<-20569, 41769> velocity=< 2, -4>
position=< 21013, -51889> velocity=<-2, 5>
position=< 21033, -51892> velocity=<-2, 5>
position=<-10173, -20669> velocity=< 1, 2>
position=<-20596, 41770> velocity=< 2, -4>
position=< 21027, 52183> velocity=<-2, -5>
position=<-41418, -31076> velocity=< 4, 3>
position=< 52264, 52179> velocity=<-5, -5>
position=< 21048, 10551> velocity=<-2, -1>
position=< 52221, 20956> velocity=<-5, -2>
position=<-51793, 20954> velocity=< 5, -2>
position=<-30999, 10550> velocity=< 3, -1>
position=< 41827, -31075> velocity=<-4, 3>
position=<-41423, 10555> velocity=< 4, -1>
position=< 52272, 41771> velocity=<-5, -4>
position=< 21008, 52180> velocity=<-2, -5>
position=<-20595, -31078> velocity=< 2, 3>
position=<-10178, -51890> velocity=< 1, 5>
position=< 21013, 10548> velocity=<-2, -1>
position=<-51793, -41483> velocity=< 5, 4>
position=<-51808, 10547> velocity=< 5, -1>
position=< 21050, -41489> velocity=<-2, 4>
position=<-20599, -41481> velocity=< 2, 4>
position=<-51833, 31367> velocity=< 5, -3>
position=<-51817, -31082> velocity=< 5, 3>
position=<-30995, 31364> velocity=< 3, -3>
position=< 31455, 10546> velocity=<-3, -1>
position=< 31433, 41776> velocity=<-3, -4>
position=<-30995, -10264> velocity=< 3, 1>
position=<-51849, -51889> velocity=< 5, 5>
position=<-31026, 20957> velocity=< 3, -2>
position=< 31468, 31360> velocity=<-3, -3>
position=< 31449, -51893> velocity=<-3, 5>
position=<-41397, 20959> velocity=< 4, -2>
position=<-10181, 10549> velocity=< 1, -1>
position=< 10638, -10268> velocity=<-1, 1>
position=< 41843, -10262> velocity=<-4, 1>
position=<-10189, 52183> velocity=< 1, -5>
position=< 41859, -51893> velocity=<-4, 5>
position=< 41819, -51893> velocity=<-4, 5>
position=< 31431, -10263> velocity=<-3, 1>
position=<-51793, 52179> velocity=< 5, -5>
position=< 10593, -10263> velocity=<-1, 1>
position=< 21040, 31369> velocity=<-2, -3>
position=< 10649, -10267> velocity=<-1, 1>
position=< 41875, -10268> velocity=<-4, 1>
position=<-20600, 10554> velocity=< 2, -1>
position=< 31468, 20953> velocity=<-3, -2>
position=<-31019, 20958> velocity=< 3, -2>
position=<-20624, -51892> velocity=< 2, 5>
position=< 41842, 20953> velocity=<-4, -2>
position=<-20599, -41480> velocity=< 2, 4>
position=< 10645, -51896> velocity=<-1, 5>
position=<-10216, 52175> velocity=< 1, -5>
position=< 21024, 31363> velocity=<-2, -3>
position=< 41870, 10551> velocity=<-4, -1>
position=<-20572, -20672> velocity=< 2, 2>
position=<-20583, 31369> velocity=< 2, -3>
position=<-20586, 31363> velocity=< 2, -3>
position=<-20596, 10549> velocity=< 2, -1>
position=< 31436, 41772> velocity=<-3, -4>
position=<-41418, 20960> velocity=< 4, -2>
position=<-10205, -51889> velocity=< 1, 5>
position=< 10649, -31074> velocity=<-1, 3>
position=< 10622, 20959> velocity=<-1, -2>
position=< 52274, -10268> velocity=<-5, 1>
position=< 52222, -51896> velocity=<-5, 5>
position=< 52266, -31077> velocity=<-5, 3>
position=< 41854, -10259> velocity=<-4, 1>
position=< 31463, -20670> velocity=<-3, 2>
position=< 21045, 10552> velocity=<-2, -1>
position=<-10197, 20955> velocity=< 1, -2>
position=< 31431, -51892> velocity=<-3, 5>
position=< 52250, -10262> velocity=<-5, 1>
position=< 41830, -20672> velocity=<-4, 2>
position=<-20596, -51887> velocity=< 2, 5>
position=<-10203, -41480> velocity=< 1, 4>
position=< 41850, 52174> velocity=<-4, -5>
position=< 10628, 52179> velocity=<-1, -5>
position=<-30986, 31360> velocity=< 3, -3>
position=<-20591, -31074> velocity=< 2, 3>
position=< 52234, -51887> velocity=<-5, 5>
position=<-20604, -51891> velocity=< 2, 5>
position=< 10593, 41768> velocity=<-1, -4>
position=< 31439, 31362> velocity=<-3, -3>
position=< 41859, -51892> velocity=<-4, 5>
position=<-10205, 31369> velocity=< 1, -3>
position=<-31027, 10548> velocity=< 3, -1>
position=< 10633, 10548> velocity=<-1, -1>
position=< 21028, 20953> velocity=<-2, -2>
position=< 21016, 20959> velocity=<-2, -2>
position=< 31434, -10263> velocity=<-3, 1>
position=<-10192, 10555> velocity=< 1, -1>
position=< 52234, 31366> velocity=<-5, -3>
position=< 41822, 31363> velocity=<-4, -3>
position=< 41838, -51895> velocity=<-4, 5>
position=<-31007, 20958> velocity=< 3, -2>
position=<-41402, 31362> velocity=< 4, -3>
position=< 52229, 41769> velocity=<-5, -4>
position=< 10641, -41482> velocity=<-1, 4>
position=<-51804, -41484> velocity=< 5, 4>
position=< 41827, 20962> velocity=<-4, -2>
position=<-10208, -31080> velocity=< 1, 3>
position=< 41826, 41771> velocity=<-4, -4>
position=< 21041, -10266> velocity=<-2, 1>
position=<-51808, -51895> velocity=< 5, 5>
position=<-51841, 41774> velocity=< 5, -4>
position=<-20596, 41774> velocity=< 2, -4>
position=< 41866, 31364> velocity=<-4, -3>
position=< 31407, -20672> velocity=<-3, 2>
position=< 10643, -20671> velocity=<-1, 2>
position=< 41865, 10546> velocity=<-4, -1>
position=<-51792, 10551> velocity=< 5, -1>
position=<-30977, -10262> velocity=< 3, 1>
position=< 31436, 41775> velocity=<-3, -4>
position=< 31443, 20960> velocity=<-3, -2>
position=<-30977, 10549> velocity=< 3, -1>
position=<-31022, -31082> velocity=< 3, 3>
position=<-41434, -41488> velocity=< 4, 4>
position=<-20604, -31074> velocity=< 2, 3>
position=< 52261, 31367> velocity=<-5, -3>
position=< 21029, 20960> velocity=<-2, -2>
position=< 10625, 52174> velocity=<-1, -5>
position=<-41433, 41771> velocity=< 4, -4>
position=< 52269, 20954> velocity=<-5, -2>
position=< 10625, 41768> velocity=<-1, -4>
position=< 41830, 20959> velocity=<-4, -2>
position=< 31431, -10266> velocity=<-3, 1>
position=<-20572, 41774> velocity=< 2, -4>
position=< 52265, -20668> velocity=<-5, 2>
position=< 10595, -41485> velocity=<-1, 4>
position=<-20580, -10264> velocity=< 2, 1>
position=< 52270, -10268> velocity=<-5, 1>
position=<-31019, 31362> velocity=< 3, -3>
position=< 31439, -10262> velocity=<-3, 1>
position=<-20618, 20957> velocity=< 2, -2>
position=<-41407, 31364> velocity=< 4, -3>
position=< 41814, -31080> velocity=<-4, 3>
position=<-10208, -20673> velocity=< 1, 2>
position=< 31412, -20673> velocity=<-3, 2>
position=< 31463, 52183> velocity=<-3, -5>
position=<-30983, -51896> velocity=< 3, 5>
position=< 31447, 10553> velocity=<-3, -1>
position=< 41843, -51895> velocity=<-4, 5>
position=< 31447, -10259> velocity=<-3, 1>
position=<-41438, -31082> velocity=< 4, 3>
position=< 10593, -31078> velocity=<-1, 3>
position=<-30985, 10546> velocity=< 3, -1>
position=<-31027, -41487> velocity=< 3, 4>
position=< 21026, 52174> velocity=<-2, -5>
position=<-51809, 52178> velocity=< 5, -5>
position=<-41429, -10267> velocity=< 4, 1>
position=< 52256, 10551> velocity=<-5, -1>
position=< 52277, 41769> velocity=<-5, -4>
position=<-31000, 41767> velocity=< 3, -4>
position=<-20583, -51887> velocity=< 2, 5>
position=<-20594, 10550> velocity=< 2, -1>
position=<-41393, 20953> velocity=< 4, -2>
position=< 21002, -20671> velocity=<-2, 2>
position=<-10200, 41776> velocity=< 1, -4>
position=< 52258, 31369> velocity=<-5, -3>

@ -0,0 +1,93 @@
defmodule Day11 do
@serial 7511
@size 300
def hundreds(lvl) when lvl < 100, do: 0
def hundreds(lvl) do
[_units, _tens, hundreds | _] = Integer.digits(lvl) |> Enum.reverse()
hundreds
end
def power_level(x, y, serial) do
rack_id = x + 10
rack_id |> Kernel.*(y) |> Kernel.+(serial) |> Kernel.*(rack_id) |> hundreds() |> Kernel.-(5)
end
def grid(serial \\ @serial) do
for x <- 1..@size, y <- 1..@size, into: %{} do
{{x, y}, power_level(x, y, serial)}
end
end
def total_power(x, y, grid) do
for x <- x..(x + 2), y <- y..(y + 2) do
grid[{x, y}]
end
|> Enum.sum()
end
def find_largest_3x3(grid) do
for x <- 1..(@size - 2), y <- 1..(@size - 2) do
{{x, y}, total_power(x, y, grid)}
end
|> Enum.max_by(fn {_, power} -> power end)
end
def part1 do
{{x, y}, power} = grid() |> find_largest_3x3()
"#{x},#{y} (total power #{power})"
end
# Entry point
def largest_total_power_at(x, y, grid) do
size = 1
power = grid[{x, y}]
largest_total_power_at(x, y, grid, size, power, size, power)
end
# Terminating condition, square size has reached the edge of the grid
def largest_total_power_at(x, y, _grid, size, _power, max_size, max_power)
when x + size > @size or y + size > @size do
{x, y, max_size, max_power}
end
# Calculate extra power for square of size + 1 and take the max of the two
def largest_total_power_at(x, y, grid, size, power, max_size, max_power) do
new_x = x + size
new_y = y + size
new_row = for row_x <- x..new_x, do: grid[{row_x, new_y}]
# Minus 1 to avoid counting the corner twice
new_col = for col_y <- y..(new_y - 1), do: grid[{new_x, col_y}]
new_power = Enum.sum([power | new_row ++ new_col])
new_size = size + 1
{max_size, max_power} =
if new_power > max_power do
{new_size, new_power}
else
{max_size, max_power}
end
largest_total_power_at(x, y, grid, new_size, new_power, max_size, max_power)
end
# Still very slow, managed to avoid recalculating larger squares, but still recalculates
# smaller squares that are a subset of a larger square we already calculated.
# Could fix it, but 300x300 completes in a few mins.. maybe will come back to this
# I Wonder if replacing all the comprehensions with reduce would have a strong effect
def part2 do
grid = grid()
{x, y, size, power} =
for x <- 1..@size, IO.inspect(301 - x), y <- 1..@size do
largest_total_power_at(x, y, grid)
end
|> Enum.max_by(fn {_x, _y, _size, power} -> power end)
"Part 2 Answer: #{x},#{y},#{size} (power: #{power})"
end
end
IO.puts(Day11.part1())
IO.puts(Day11.part2())

@ -0,0 +1,101 @@
defmodule Day12 do
defmodule Parser do
def parse(filename) when is_binary(filename) do
File.open!(filename, [:utf8], &parse/1)
end
def parse(file) do
IO.read(file, String.length("initial state: "))
state = file |> IO.read(:line) |> String.trim() |> String.to_charlist() |> Enum.with_index()
IO.read(file, :line)
{state, parse_spec(file)}
end
def parse_spec(file), do: parse_spec(file, IO.read(file, :line), %{})
def parse_spec(_file, :eof, spec), do: spec
def parse_spec(file, line, spec) do
[pattern, <<result::utf8>>] = ~r/(.{5}) => (.)/ |> Regex.run(line, capture: :all_but_first)
parse_spec(
file,
IO.read(file, :line),
Map.put(spec, String.to_charlist(pattern), result)
)
end
end
def evolve(input, _spec, 0, _previous), do: input
def evolve(input, spec, generations, previous) do
next = generation(input, spec)
IO.inspect("#{generations}: #{next |> visualise}")
if stable?(next, input) do
# Stable state, so we can just add the remaining generation count to the index numbers and finish early
next = Enum.map(next, fn {pot, idx} -> {pot, idx + generations - 1} end)
evolve(next, spec, 0, input)
else
evolve(next, spec, generations - 1, input)
end
end
def stable?(next, prev), do: visualise(next) === visualise(prev)