This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Day12 do | |
| def part1(), do: | |
| connections() |> group("0") |> Enum.count() | |
| def part2(), do: | |
| connections() |> all_groups() |> Enum.count() | |
| defp connections(), do: | |
| "input.txt" | |
| |> File.stream!() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Day13 do | |
| def part1(), do: | |
| scanners() |> severities(0) |> Enum.sum() | |
| def part2(), do: | |
| 0 | |
| |> Stream.iterate(&(&1+1)) | |
| |> Stream.transform(scanners(), &{[severities(&2, &1)], &2}) | |
| |> Stream.take_while(&(not Enum.empty?(&1))) | |
| |> Enum.count() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Day14 do | |
| def part1(input), do: | |
| input |> grid_points(128) |> Enum.count() | |
| def part2(input), do: | |
| input |> groups(128) |> Enum.count() | |
| defp groups(input, size), do: | |
| input |> new_groupper(size) |> Stream.unfold(&pop_group/1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Day15 do | |
| require Bitwise | |
| def part1(a, b), do: | |
| judge(40_000_000, a, b, &rem(&1 * 16807, 2147483647), &rem(&1 * 48271, 2147483647)) | |
| def part2(a, b), do: | |
| judge(5_000_000, a, b, &find_next_factor(&1, 16807, 4), &find_next_factor(&1, 48271, 8)) | |
| defp judge(steps, a, b, generator_a, generator_b, count \\ 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Day16 do | |
| def part1(), do: | |
| new_group() |> dance(instructions()) |> programs() |> Enum.join() | |
| def part2(), do: | |
| dances(1_000_000_000, instructions()) | |
| defp dances(num_dances, instructions) do | |
| {found_orders, cycle_start_pos, cycle_length} = search_cycle(instructions) | |
| desired_pos = rem(num_dances - cycle_start_pos, cycle_length) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Day17 do | |
| def part1(num_steps), do: | |
| insert(2017, num_steps, [], &List.insert_at/3) | |
| |> Stream.chunk_every(2, 1) | |
| |> Enum.find(&match?([2017, _], &1)) | |
| |> Enum.at(1) | |
| def part2(num_steps), do: | |
| insert(50_000_000, num_steps, nil, | |
| fn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Day18 do | |
| defmodule Machine.Core do | |
| def new(instructions), do: | |
| %{ | |
| registers: %{}, | |
| position: 0, | |
| instructions: instructions |> Enum.to_list() |> :array.from_list(fixed: true) | |
| } | |
| def done?(machine), do: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Day19 do | |
| def part1(), do: | |
| read_map() |> trip_steps() |> encountered_letters() |> Enum.to_list() | |
| def part2(), do: | |
| read_map() |> trip_steps() |> Enum.count() | |
| defp encountered_letters(trip_steps), do: | |
| trip_steps |> Stream.map(&Map.fetch!(&1.map, &1.pos)) |> Stream.reject(&(&1 in [?|, ?-, ?+])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Day20 do | |
| def part1() do | |
| {id, _particle} = Enum.min_by(particles(), &particle_weight/1) | |
| id | |
| end | |
| def part2() do | |
| particles = particles() | |
| count_survivors(particles, sorted_collisions(particles)) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Day21 do | |
| def part1(), do: | |
| enhancements() |> generate_art(5) |> count_pixels() | |
| def part2(), do: | |
| enhancements() |> generate_art(18) |> count_pixels() | |
| defp count_pixels(grid), do: | |
| Enum.count(for <<bit::1 <- grid>>, bit == 1, do: bit) |