Last active
December 6, 2020 03:36
-
-
Save neenjaw/bd930e9d039b634cd84b29e4b664d03b to your computer and use it in GitHub Desktop.
day 5
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
| tickets = File.read!("input.txt") |> String.split("\n", trim: true) | |
| row_num = fn row -> | |
| row | |
| |> String.replace("F", "0") | |
| |> String.replace("B", "1") | |
| |> String.to_integer(2) | |
| end | |
| seat_num = fn seat -> | |
| seat | |
| |> String.replace("R", "1") | |
| |> String.replace("L", "0") | |
| |> String.to_integer(2) | |
| end | |
| seat_id = fn row_num, seat_num -> row_num * 8 + seat_num end | |
| tickets | |
| |> Enum.map(fn ticket -> | |
| {row, seat} = String.split_at(ticket, -3) | |
| rn = row_num.(row) | |
| sn = seat_num.(seat) | |
| seat_id.(rn, sn) | |
| end) | |
| |> Enum.max() | |
| |> IO.inspect(label: "part 1") | |
| potential_seat_ids = | |
| tickets | |
| |> Enum.reduce([], fn ticket, acc -> | |
| {row, seat} = String.split_at(ticket, -3) | |
| rn = row_num.(row) | |
| sn = seat_num.(seat) | |
| cond do | |
| rn != 0 && rn != 127 -> [seat_id.(rn, sn) | acc] | |
| true -> acc | |
| end | |
| end) | |
| |> Enum.sort(:asc) | |
| potential_min = hd(potential_seat_ids) | |
| potential_seat_ids | |
| |> Enum.with_index(potential_min) | |
| |> Enum.drop_while(fn {seat, idx} -> | |
| seat == idx | |
| end) | |
| |> Enum.take(1) | |
| |> hd() | |
| |> elem(1) | |
| |> IO.inspect(label: "part 2") |
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
| def row_num(code) | |
| code | |
| .gsub(/F/, '0') | |
| .gsub(/B/, '1') | |
| .to_i(2) | |
| end | |
| def seat_num(code) | |
| code | |
| .gsub(/R/, '1') | |
| .gsub(/L/, '0') | |
| .to_i(2) | |
| end | |
| def code_to_seat(code) | |
| row = code.chomp[0...-3] | |
| seat = code.chomp[-3..] | |
| [row, seat] | |
| end | |
| def seat_to_id(row, seat) | |
| row * 8 + seat | |
| end | |
| tickets = | |
| File | |
| .readlines('input.txt') # .take(1) | |
| .inject([]) do |seat_numbers, ticket| | |
| # puts ticket | |
| row, seat = code_to_seat(ticket) | |
| # puts row.inspect | |
| # puts seat.inspect | |
| rn = row_num(row) | |
| sn = seat_num(seat) | |
| # puts rn.inspect | |
| # puts sn.inspect | |
| seat_id = seat_to_id(rn, sn) | |
| seat_numbers << seat_id if rn != 0 && rn != 127 | |
| end | |
| .sort | |
| min = tickets.min | |
| max = tickets.max | |
| puts tickets | |
| .zip((min..max)) | |
| .drop_while {|(seat, n)| seat == n } | |
| .take(1)[0][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 AdventOfCode.Day05 do | |
| import Bitwise, only: [<<<: 2] | |
| def walk(line) do | |
| line | |
| |> String.reverse() | |
| |> do_walk() | |
| end | |
| def do_walk(ticket, sum \\ 0, place \\ 0) | |
| def do_walk(<<>>, sum, _), do: sum | |
| # ?F and ?L are equal to binary 0 | |
| def do_walk(<<l, rest::binary>>, sum, place) when l in 'FL' do | |
| do_walk(rest, sum, place + 1) | |
| end | |
| # ?B and ?R are equal to binary 1 | |
| def do_walk(<<l, rest::binary>>, sum, place) when l in 'BR' do | |
| do_walk(rest, sum + (1 <<< place), place + 1) | |
| end | |
| def part1(args) do | |
| args | |
| |> Stream.map(&walk/1) | |
| |> Enum.max() | |
| end | |
| def part2(args) do | |
| # O(n * m) n - number of tickets, m - length of ticket string | |
| {sum, min, max, count} = | |
| args | |
| |> Enum.reduce({0, 2 <<< 9, 0, 0}, fn ticket, {sum, min, max, count} -> | |
| n = walk(ticket) | |
| sum = sum + n | |
| min = Enum.min([min, n]) | |
| max = Enum.max([max, n]) | |
| count = count + 1 | |
| {sum, min, max, count} | |
| end) | |
| expected_sum = ((count + 1) * (min + max) / 2) |> trunc() | |
| expected_sum - sum | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment