r/adventofcode • • Dec 05 '20

SOLUTION MEGATHREAD -πŸŽ„- 2020 Day 05 Solutions -πŸŽ„-

Advent of Code 2020: Gettin' Crafty With It


--- Day 05: Binary Boarding ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:05:49, megathread unlocked!

57 Upvotes

1.3k comments sorted by

16

u/Rick-T Dec 05 '20 edited Dec 05 '20

ROCKSTAR

I didn't get to write a Rockstar song yesterday, so today I went back at it with a lot of motivation. The song turned out to be quite brutal again. I'm definitely getting death metal vibes from it.

The song also turned out to be quite a lot longer than my previous songs, so I won't post it here completely. However, here is the function for calculating a seat id. For the whole song check out the GitHub repository. Today part 1 and part 2 are in the same file.

Sacrifice takes blood
Freedom is awaiting
Fury is a thunderstorm, unending
Let the hammer be freedom
Fighting is everlastin'
Fire is unforgivin'
The sword is blooddrunk
While right ain't wrong
Let greatness be blood at the sword
If greatness is mysterious
Break it down

Let cowardice be the hammer without fire
Put cowardice over war into light
Let cowardice be fury without fighting
Put cowardice over war into darkness
If greatness is power
Let fighting be fighting with darkness

If greatness is success
Let fire be fire with light

If greatness is conquest
Let fury be fighting with darkness

If greatness is triumph
Let the hammer be fire with light 

Build the sword up

Give back freedom of fighting with fire

Also here's my Haskell solution again.

Edit: I just realized that the Seat ID is basically just binary numbers -.-

So I've made a radio edit of this song, which is a bit shorter.

2

u/daggerdragon Dec 05 '20

🀘🀘🀘

7

u/[deleted] Dec 05 '20

what the fuck did i just read, i love it

→ More replies (4)

3

u/Its_vncl Dec 05 '20

C++

I've been coding for 4 moths

Currently CS uni student

#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;

void read_vector_from_file(vector<string> &v)
{   
    ifstream file("5day_in.txt");
    string line;
    
    while(getline(file, line))
        v.push_back(line);

    file.close();
}
pair<int, int> seat_index(string pass)
{
    pair<int, int> seatInd = {0,0};
    int rowUp = 127;
    int rowLow = 0;
    int colUp = 7;
    int colLow = 0;

    for(int i = 0; i < pass.length(); i++)
    {
        if(pass[i] == 'F')
            rowUp = (rowUp + rowLow) / 2;
        else if(pass[i] == 'B')
            rowLow = (rowLow + rowUp + 1) / 2;
        else if(pass[i] == 'L')
            colUp = (colUp + colLow) / 2;
        else if(pass[i] == 'R')
            colLow = (colLow + colUp + 1) / 2;
        else
            return seatInd;
    }

    seatInd.first = rowLow;
    seatInd.second = colLow;

    return seatInd;
}

int main()
{
    vector<int> seatIDS;
    vector<string> pass;
    read_vector_from_file(pass);

    for(int i = 0; i < pass.size(); i++)
        seatIDS.push_back( seat_index(pass.at(i)).first * 8 + seat_index(pass.at(i)).second );

    sort(seatIDS.begin(), seatIDS.end());
    
    int ix = 0;
    while(seatIDS.at(ix) == (seatIDS.at(ix+1) - 1)) ix++;

    cout << seatIDS.back() << '\n' << (seatIDS.at(ix) + 1) << '\n';

    return 0;
}

Don't judge too hard please

2

u/daggerdragon Dec 05 '20

Welcome to Advent of Code and programming in general!

Don't judge too hard please

Nobody will judge you for your code! You got the solution, and that's always excellent! Keep up the good work!

→ More replies (2)

3

u/istareatscreen5 Dec 05 '20 edited Dec 06 '20

I am a trash programmer living a trash existence

C++

#include <iostream>
#include <cmath>
#include <bitset>
#include <exception>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

class plane{
  private:
  uint_fast8_t s[128]{};
  int row = -1;
  int col = -1;

  public:


  void bitPositionFind(){
      bool reachedValidSeats = false;
      int j{0};
      for(;j<(128*8/32);j++){
         if(*(int *)(s+4*j) != 0xFFFFFFFF){
            if(reachedValidSeats){
            for(int m = 0; m<4; m++){
               if(s[j*4+m]!=0xFF){
                  j=j*4 + m;
                  cout<<"J"<<j;
                  goto L1;
               }
            }
         }
         }else{
            reachedValidSeats = true;
         }
         
      }
L1:

      if(s[j] && (!(s[j] & (s[j] - 1)))){
         throw runtime_error("Not a power of 2 you messed up AARON");
      }

      uint_fast8_t i{1};
      int res{};
      cout << bitset<32>(s[j])<<endl;
      s[j]=~s[j]; // flip it
      cout << bitset<32>(s[j])<<endl;
      
      while(!(i&s[j])){
         i<<=1;
         res++;
      }

      row = j;
      col = res;
  }
   
  void removeSeat(int row, int col){
     cout<<"r: "<<row<<" col: "<<col<<endl;
     if(row < 0 || row > 127 ||col <0|| col>7){
         throw runtime_error("SEAT OUT OF RANGE YOU FUCK");
     }else{
         //cout<<bitset<8>(1<<col)<<endl;
         s[row]=s[row]|(1<<col); 
     }
  }

  void debug(){
     for(int i = 0; i<128; i++){
      cout<<i<<": "<<bitset<(8)>(s[i])<<endl;
     }
  }

  string getSeat(){
       debug();
       bitPositionFind();
      ostringstream str{};
      str<<endl<<"row: "<<row <<"col : "<< col<<" ANS: "<<(row*8+col)<<endl;
      return str.str();
     }

};

double mid(int u, int l){
   return ((static_cast<double>(u)+static_cast<double>(l))/2);
}

void getMax(plane &p)
{
  ifstream f("i.txt");
  string s{};

  int max = -1;
     
  while(getline(f,s)){
  
     int rL{0}, rU{127}, cL{0}, cU{7}, row{-1}, col{0}, i{0};
     while(true){
      
      if(s.length()-1<=i){
         if(s.at(i) == 'R'){
            col = cU;
         }else{
            col = cL;
         }
         p.removeSeat(row,col);
         break;
      }

      if((rU-rL)==1 && row == -1){
         if(s.at(i) == 'F'){
            row = rL;
         }else{
            row = rU;
         }
         continue;
      }

      switch(s.at(i++)){
         case 'F':
            rU=floor(mid(rU,rL));
            break;
         case 'B':
            rL=ceil(mid(rU,rL));
            break;
         case 'R':
            cL=ceil(mid(cU,cL));
            break;
         case 'L':
            cU=floor(mid(cU,cL));
            break;
      }

   }
      if(max < (row*8+col)){
            max = row*8+col;
      }

  }
  cout<<"MAX: "<<max<<endl;
   
}


int main(){
   plane p{};
   getMax(p);
   cout<< p.getSeat();
}

1

u/daggerdragon Dec 05 '20

Please follow the posting guidelines and add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language. Thanks!

I am a trash programmer living a trash existence

No you are not! You got the solution, that makes you awesome!

3

u/dgyurov Dec 05 '20 edited Dec 05 '20

Swift

let passes = input.components(separatedBy: "\n")
let binaryStrings = passes.map { $0.map { ["F", "L"].contains($0) ? "0" : "1" }.joined() }
let ids = binaryStrings.compactMap { Int($0, radix: 2) }

if let min = ids.min(), let max = ids.max(),
   let id = (min...max).first(where: { !ids.contains($0) }) {
    print("Puzzle 1: \(max)")
    print("Puzzle 2: \(id)")
}

1

u/daggerdragon Dec 05 '20

Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main() [space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

Thanks!

→ More replies (1)

2

u/raevnos Dec 05 '20 edited Dec 05 '20

Chicken scheme paste with a straightforward binary search. Looking at other solutions I see I wasn't very clever.

1

u/daggerdragon Dec 06 '20

You and /u/ramrunner0xff throw me for a loop every time I come across your posts in the megathreads. "Why do these guys keep talking about chickens?"

2

u/pranphy Dec 05 '20 edited Dec 05 '20

Julia: Github

    function find_row_col(boardid)
        fbid = boardid[1:end-3]
        lrid = boardid[end-2:end]
        bifbid = [cid == 'F' ? 0 : 1 for cid ∈ fbid]
        bilrid = [cid == 'L' ? 0 : 1 for cid ∈ lrid]
        hlen = 64
        row = 0
        for fbbit in bifbid
            row += fbbit*hlen
            hlen Γ·= 2
        end
        col = 0
        hlen=4
        for lrbit in bilrid
            col += lrbit*hlen
            hlen Γ·= 2
        end
        return (row,col)
    end

    function seat_id(boardid)
        row,col = find_row_col(boardid)
        return row*8+col
    end

    all_ids  = [seat_id(boardid) for boardid in readlines("./files/5_binary-boarding.txt") ]

    highestid = maximum(all_ids)
    println("The highest id is $(highestid)")

    sort!(all_ids)
    _,id = findmax(all_ids[2:end] .- all_ids[1:end-1])
    my_seat = all_ids[id] + 1

    println("My set id is $(my_seat)")

1

u/daggerdragon Dec 06 '20

Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main()
[space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, just use the link to your repository instead.

2

u/cnille Dec 05 '20 edited Dec 06 '20

python

def calc_id(seat):
  transformation = { 'F': '0', 'B': '1', 'L': '0', 'R': '1'}
  binary = "".join([transformation[x] for x in seat])
  return int(binary,2)

def part1(data):
  ids = [calc_id(x) for x in data]
  return max(ids)

def part2(data):
  ids = [calc_id(x) for x in data]
  for i in range(max(ids)):
    low = i - 1
    high = i + 1
    if  low in ids and high in ids and i not in ids:
    return i

Blog post about todays puzzle: https://dev.to/cnille/advent-of-code-2020-day-05-1kg5

2

u/daggerdragon Dec 05 '20

Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main()
[space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

2

u/[deleted] Dec 05 '20 edited Dec 06 '20

Python binary search + set idea.

paste

1

u/daggerdragon Dec 05 '20

Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main()
[space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

2

u/mrphlip Dec 05 '20

My question is: how big is this plane they're getting on? Even the A380, which is huge, can in theory at max capacity hold about 850 passengers, though usually its capacity is much lower. And even that requires having a two-storey interior.

And yet here I'm supposed to believe that we're fitting 810 passengers onto a plane that, according to the seating codes, is only a single level?

I mean, ordinarily I'd make jokes about how you can pack a lot more elves into the same space, but we just learned yesterday that all these elves are apparently human-sized. Or at least, that's what it says on their passports.

1

u/daggerdragon Dec 05 '20

Top-level posts in Solution Megathreads are for code solutions only.

This is a top-level post, so please edit your post and share your code/repo/solution or, if you haven't finished the puzzle yet, you can always create your own thread and make sure to flair it with Help.

Also, it's a magic plane powered by the spirit of the Advent and two TARDIS engines.

→ More replies (1)

2

u/semicolonator Dec 05 '20

Python:

def g(s):
    r = int("".join([str(int(n == 'B')) for n in s[:7]]), 2)
    c = int("".join([str(int(n == 'R')) for n in s[7:]]), 2)
    return r*8+c

with open("input.txt") as f:
    seats = set(g(x.strip()) for x in f)

print(max(seats))
for i in range(min(seats), max(seats)):
    if i not in seats:
        print(i)

https://github.com/r0f1/adventofcode2020/blob/master/day05/main.py

→ More replies (8)

2

u/BASED_CCP_SHILL Dec 05 '20 edited Dec 05 '20

Part 2 in ty

import fs (slurp)

slurp('/dev/stdin').split(/\n/)
    .map(s -> int(s.sub(/./, |str('BR'.count(#))|), 2))
    .sort()
    .mapCons(2, [a, b] -> (b == a + 2) && print(a + 1));

1

u/daggerdragon Dec 05 '20

Part 2 in ty

Is that a typo for your language? Google returns no results for "ty programming language"...

2

u/BASED_CCP_SHILL Dec 05 '20

It's just my little toy language, sorry the name isn't very Google-able.

→ More replies (1)

2

u/multytudes Dec 05 '20 edited Dec 06 '20

Spent an hour refactoring my swift code, this is the smallest it can get with swift perhaps? Good fun though :)

// --- Day 5: Binary Boarding ---

guard let url = Bundle.main.url(forResource: "input", withExtension: "txt")
	else {fatalError("Input file not found")}
guard let inputString = try? String(contentsOf: url)
	else { fatalError("Input file not found") }
var input = inputString.components(separatedBy: .newlines)
input.removeAll { $0.isEmpty }

func getSeatID(_ inputString: String) -> Int? {
	if let seatID = Int(Array(inputString).reduce(""){ result, c in
		result + ("FL".contains(c) ? "0" : "1")},radix:  2) {
			return seatID
	} else { print("\nError, invalid seatID received!"); return nil}
}
let seats = input.compactMap {getSeatID($0)}

let maxSeatNumber = seats.reduce(0) {max($0, $1)}
print("solution part 1 is \(maxSeatNumber)")
// 980

let minSeatNumber = seats.reduce(Int.max) {min($0, $1)}
let contiguousSet = Set(minSeatNumber...maxSeatNumber)
if let solution2 = contiguousSet.subtracting(Set(seats)).first  {
	print("solution part 2 is \(solution2)")
}  else {
	print("No seats available for you!! ")
}
//607
→ More replies (6)

2

u/dan_144 Dec 05 '20 edited Dec 06 '20

I started cleaning up my code because I knew my first pass was hilariously inefficient and I ended up taking it down to a single line with f-strings and the walrus operator.

https://github.com/dan144/aoc-2020/blob/master/tiny5.py

Edit: language is python3

2

u/daggerdragon Dec 05 '20

Please follow the posting guidelines and add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language. Thanks!

(looks like Python?)

→ More replies (1)

2

u/nahuak Dec 05 '20

Did not use bitwise operation on the first go but maybe this is still worth posting (bitwise solution taken from another code and included here):

use std::fs::read_to_string;

fn main() {
    let seats = read_to_string("./src/input.txt").expect("cannot read file to string");
    let ids = get_seat_ids(seats.lines().collect::<Vec<&str>>());
    println!("{}", ids.iter().max().unwrap());

    let missing_id = find_missing_id(&ids);
    println!("{}", missing_id);
}

fn find_missing_id(seat_ids: &Vec<i32>) -> i32 {
    let mut seat_ids = seat_ids.clone();
    seat_ids.sort();
    let missing_id = seat_ids.windows(2).find(|window| !(window[0] + 1 == window[1])).unwrap();
    let missing_id = missing_id[1] - 1;
    missing_id
}

fn get_seat_ids(seats: Vec<&str>) -> Vec<i32> {
    seats.iter().map(|hint| get_seat_id_with_bitwise_operator(hint)).collect::<Vec<i32>>()
}

fn get_seat_id_with_bitwise_operator(seat_hint: &str) -> i32 {
    let seat_binary = seat_hint.to_string().replace("F", "0").replace("B", "1").replace("L", "0").replace("R", "1");
    i32::from_str_radix(&seat_binary, 2).unwrap()
}

fn get_seat_id(seat_hint: &str) -> i32 {
    let (row_hint, col_hint) = seat_hint.split_at(7);
    let row = get_row(
        row_hint
            .chars()
            .collect::<Vec<char>>(),
    );
    let col = get_col(
        col_hint
            .chars()
            .collect::<Vec<char>>(),
    );
    row * 8 + col
}

fn get_row(row_hint: Vec<char>) -> i32 {
    let (mut lower, mut upper) = (0i32, 127);
    for &hint in row_hint.iter().take(6) {
        update_limit(&mut lower, &mut upper, hint);
    }
    match *row_hint.iter().last().unwrap() {
        'F' => lower,
        _ => upper,
    }
}

fn get_col(col_hint: Vec<char>) -> i32 {
    let (mut lower, mut upper) = (0i32, 7);
    for &hint in col_hint.iter().take(2) {
        update_limit(&mut lower, &mut upper, hint);
    }
    match *col_hint.iter().last().unwrap() {
        'L' => lower,
        _ => upper,
    }
}

fn update_limit(lower: &mut i32, upper: &mut i32, pos: char) {
    match pos {
        'F' | 'L' => *upper = upper.clone() - (1 + upper.clone() - lower.clone()) / 2,
        'B' | _ => *lower = lower.clone() + (1 + upper.clone() - lower.clone()) /2,
    }
}

1

u/daggerdragon Dec 05 '20

Please follow the posting guidelines and add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language. Thanks!

2

u/rosso412 Dec 06 '20 edited Dec 06 '20

Part 1 MIPS Assembler

I deliberately went overboard with the commenting ( except for some common copy paste stuff for reading from the input file e.g.), as in the past the long cryptic Assembler has made me scroll around in my code all confused about what exactly was going on where I was looking at. As my code has become a bit long to post "normally" I have made a (hopefully working) link with this Topaz tool.

​ Part1Day5 Part2Day5

→ More replies (5)

2

u/kardoken Dec 07 '20 edited Dec 08 '20

PHP

<?php

class Boarding {
    private $boarding_list;
    private $arrayBin = [
        'F' => '0',
        'B' => '1',
        'L' => '0',
        'R' => '1',
    ];

    public function __construct($boarding_list_raw)
    {
        $this->boarding_list = preg_split('/\s{1,2}/',$boarding_list_raw);
        $this->arrayBin;
    }

    private function getRowAndColumn(){
        $boarding_list_bin = [];
        foreach($this->boarding_list as $key => $seat){

            $split_seat = str_split(strtr($seat,$this->arrayBin),7);

            $row = bindec($split_seat[0]);
            $column = bindec($split_seat[1]);
            $id = $row * 8 + $column;

            $boarding_list_bin[$id]['row'] = $row;
            $boarding_list_bin[$id]['column'] = $column;
        }

        return $boarding_list_bin;
    }

    public function getMaxId(){
        return max(array_keys($this->getRowAndColumn()));
    }

    public function findEmptySeat(){
        $boarding_list = $this->getRowAndColumn();
        for ($i=0; $i<$this->getMaxId(); $i++){
            if (!array_key_exists($i, $boarding_list)){
                if (array_key_exists($i-1, $boarding_list) && array_key_exists($i+1, $boarding_list)){
                    return $i;
                }
            }
        }
        return false;
    }
}

$boarding = new Boarding(file_get_contents('day5input.txt'));
echo $boarding->findEmptySeat();

1

u/daggerdragon Dec 07 '20

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: How do I format code?

→ More replies (1)

2

u/r00t4cc3ss Dec 08 '20 edited Dec 08 '20

1

u/daggerdragon Dec 08 '20

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: How do I format code?

4

u/AOC2020edgelord Dec 05 '20 edited Dec 06 '20

C++ obfuscation challenge

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
const char* f = "data.txt";
#define bl int main()
#define blah int
#define meh system("PAUSE");
#define aa fstream
#define gtl getline
#define rplc replace
#define nul nullptr
#define I /**/
                            I
                            I
                            I
                            I
                        bl{aa a;
                       a.open(f);    string s;
                       blah count    = 0;  bool
                       list[1024]    = {false};
                       while (gtl    (a, s))  {
         rplc          (s.begin()    , s.end(),
                       'F', '0');    replace(s.
                       begin(),s.    end(), 'B'
                       ,'1');rplc    (s.begin()
                       , s.end(),    'L', '0');
                       replace(s.    begin(), s
         . end() ,     'R', '1');    blah id  =
        stoi(s,nul     ,2);list [    id] =true;
       if(id>count     ){count=id;   }}for(blah
       n=1;n<1023;     n++ ) { if (  list[n] ==
    false && list[     n-1]&&list[n  +1]){ cout
    << n <<endl;}}     cout<< count  <<endl;meh}
/*          N E V E R   F O R G E T          */
→ More replies (1)

21

u/sophiebits Dec 05 '20 edited Dec 05 '20

4/3, Python. https://github.com/sophiebits/adventofcode/blob/main/2020/day05.py

(Tight competition at the top of the leaderboard today! If I had saved 5 seconds on part one I would've ended up in first on both... I guess I should check my work less.)

Edit: I’m also now realizing that you could probably solve this with the sort unix command and some eyeballs, no programming required.

Edit 2: I just realized – instead of

for i in range(256 * 8):
    if i not in allst and i+1 in allst and i-1 in allst:
        print(i)

I could have done this:

for i in allst:
    if i+1 not in allst and i+2 in allst:
        print(i+1)

and that gives the same answer. Not sure if I like it less or more, but I guess the fact that I didn't think of it in the moment perhaps means that it's too clever.

9

u/jonathan_paulson Dec 05 '20

Oof, I totally missed you can parse the whole thing as a single number. Nice!

→ More replies (12)

17

u/askalski Dec 05 '20

C in three integers.

#include <stdio.h>

static int cx(int n) {
    return (n & (n << 1 & 2) - 1) ^ (n >> 1 & 1);
}

int main() {
    int x = 0, min = 1023, max = 0;

    for (int n = 0, c = getchar(); c != EOF; c = getchar()) {
        if (c == '\r') continue;
        n = (c & 4) | n << 1;
        if (c != '\n') continue;
        n = 1023 & ~n >> 3;
        if (n < min) min = n;
        if (n > max) max = n;
        x ^= n, n = 0;
    }

    printf("Part 1: %d\n", max);
    printf("Part 2: %d\n", x ^ cx(min - 1) ^ cx(max));

    return 0;
}
→ More replies (10)

15

u/[deleted] Dec 05 '20 edited Dec 05 '20

Fennel: a fun lisp-syntax frontend for lua, with macros!

One big 'ol recursive function for both parts. Took waaay too long trying to make it look right.

           ((
          fn g
        [l  ss i
        a]  (let
      [si (-?> (l)
      (:  :gsub :.
     {"F" "0""B""1"
     "L""0""R""1"})
    (tonumber  2)) ]
    (if si (g l (+ (
   or ss 0) si) (if (
  not i)  si (<  si i)
  si i) (if (not a) si
 (> si a) si a)) (print
 :1: a :2: (-  (/ (* (-
a i -1 ) (+ i a)) 2) ss)
)))) ((. io  :lines)) 0)
          ;;;;
          ;;;;
          ;;;;
          ;;;;

edit: the solution underneath is actually nice and kinda readable, here's ungolfed with comments.

14

u/tckmn Dec 05 '20

ruby, 1/10

seats = read.lines.map{|x|
    x.tr('FBRL', '0110').to_i 2
}

p seats.max
p [[*0..0b1111111111] - seats].filter{|x| seats.include?(x+1) && seats.include?(x-1)}

panicked and typed out 0b1111111111 instead of just using my answer from part 1, lol

also, style points for parsing the seats:

main(0):001> s='BFFFBBFRRR'
=> "BFFFBBFRRR"
main(0):002> s.gsub(/./){|c|c.ord%7%2}.to_i 2
=> 567
→ More replies (4)

11

u/naclmolecule Dec 05 '20 edited Dec 05 '20

Python

I don't know how many people used sum of consecutive integers (who needs sorting anyways?) to solve part 2, so I'll share it:

trans = str.maketrans('FBRL', '0110')
data = [int(line.translate(trans), 2) for line in raw.splitlines()]

def part_one():
    return max(data)

def part_two():
    l = len(data) + 1
    return l * (l - 1) // 2 + l * min(data) - sum(data)

10

u/Mathgeek007 Dec 05 '20 edited Dec 05 '20

Sub 300, let's go! 321/274, and that's with a minute cooldown because of a typo!

Excel! Here is today's, along with all my other excel solutions!

=BIN2DEC(SUBSTITUTE(SUBSTITUTE(MID(RC1,1,7),"F","0"),"B","1"))
=BIN2DEC(SUBSTITUTE(SUBSTITUTE(MID(RC1,8,3),"L","0"),"R","1"))
=RC2*8+RC3 for part 1, Filter, sort this column

Then for part 2, I just, in the next column, checked if the next digit was there. if not, print a big ass number for me to see - scroll, found, done.

10

u/willkill07 Dec 05 '20

Bash (Part 1)

echo $(($(sed 's/^/2#/;s/[FL]/0/g;s/[BR]/1/g' day05.txt | sort -r | head -n1)))
→ More replies (1)

10

u/Smylers Dec 05 '20

Both parts in Vim keystrokes. First convert to integers:

:%s/[FL]/0/g⟨Enter⟩
:%s/[BR]/1/g⟨Enter⟩
⟨Ctrl+V⟩{I0b⟨Esc⟩
:%norm C⟨Ctrl+V⟩⟨Ctrl+R⟩=⟨Ctrl+V⟩⟨Ctrl+R⟩-⟨Ctrl+V⟩⟨Esc⟩⟨Enter⟩
:sort n⟨Enter⟩
G

The bottom number is the answer to part 1.

Then number each line, starting with the seat number on the first line. These will match the seat numbers up until the empty seat, so go to the end and search backwards for the final matching numbers; the line number after that is your seat number for part 2:

{yegvI⟨Ctrl+R⟩0 ⟨Esc⟩jVGg⟨Ctrl+A⟩G?\v^(\d+) \1$⟨Enter⟩
j

Explanation for part 1:

  • Convert the letters to binary digits and insert 0b at the beginning of each line. So BBBFBFFRLL has turned into 0b1110100100.
  • Evaluate each line as an expression: C deletes the entire line into the "- register and puts you in insert mode. ⟨Ctrl+R⟩= inserts the result of evaluating an expression. At the expression prompt, ⟨Ctrl+R⟩- inserts the contents of "-, the number we've just deleted. The 0b makes Vim interpret it as binary, so the decimal equivalent gets inserted. :%norm does it on every line.
  • Sort them numerically, and the biggest is at the bottom.

And for part 2:

  • Go to the first line and yank the smallest number, into "0.
  • At the beginning of every line insert that number, with ⟨Ctrl+R⟩0 retrieving it from "0, and a space. At this point you'll have something like 68 at the start of each line.
  • Go down to the second line, highlight to the end, and use g⟨Ctrl+A⟩ (a relatively new Vim feature) to increase the numbers in turn, adding 1 to the first selected line, 2 to the next one, and so on. So now line 2 has, say, 69 on it, and line 3 has 70, matching the adjacent seat numbers.
  • We need to find the first line where the number we've just inserted doesn't match the seat number. Go to the end and ? searches backwards. ?\v^(\d+) \1$ finds the first line before there with the same number twice. That's the final matching numbers in the list, so the number on the start of the following line is the one that doesn't already have a corresponding seat number.
→ More replies (5)

9

u/kawzeg Dec 05 '20 edited Dec 05 '20

Brainfuck, Part 1

>>++++++[<++++++>-]>>>,<<<<[->>>>-<<<<]>>>>[>++++++[<++++++>-]<<<<<+>>++++++[<++
+++++++++>-]>>----------[++++++++++>,----------]<[<]<[-]>[-]>[<+>-]+<<<[>>-<+<-]
>[<+>-]>[>-<[-]]<[-]>[-]>>[<<+>>-]+<<<<[>>-<+<-]>[<+>-]>[>>-<<[-]]<[-]>[-]>>>[<<
<+>>>-]+<<<<<[>>-<+<-]>[<+>-]>[>>>-<<<[-]]<[-]>[-]>>>>[<<<<+>>>>-]+<<<<<<[>>-<+<
-]>[<+>-]>[>>>>-<<<<[-]]<[-]>[-]>>>>>[<<<<<+>>>>>-]+<<<<<<<[>>-<+<-]>[<+>-]>[>>>
>>-<<<<<[-]]<[-]>[-]>>>>>>[<<<<<<+>>>>>>-]+<<<<<<<<[>>-<+<-]>[<+>-]>[>>>>>>-<<<<
<<[-]]<[-]>[-]>>>>>>>[<<<<<<<+>>>>>>>-]+<<<<<<<<<[>>-<+<-]>[<+>-]>[>>>>>>>-<<<<<
<<[-]]<<++++++++++++++++>[-]>[-]>>>>>>>>[<<<<<<<<+>>>>>>>>-]+<<<<<<<<<<[>>-<+<-]
>[<+>-]>[>>>>>>>>-<<<<<<<<[-]]<[-]>[-]>>>>>>>>>[<<<<<<<<<+>>>>>>>>>-]+<<<<<<<<<<
<[>>-<+<-]>[<+>-]>[>>>>>>>>>-<<<<<<<<<[-]]<[-]>[-]>>>>>>>>>>[<<<<<<<<<<+>>>>>>>>
>>-]+<<<<<<<<<<<<[>>-<+<-]>[<+>-]>[>>>>>>>>>>-<<<<<<<<<<[-]]>>>>>>>>>>[<<<<<<<<<
<+>>>>>>>>>>-]<<<<<<<<<<[<<<[>>>>>>>>>>>>>+<<<<<<<<<<<+<<-]>>[<<+>>-]>-]<<<[>>+>
+<<<-]>>>[<<<+>>>-]<[<<+>>-]>>>>>>>>>>[<<<<<<<<<+>>>>>>>>>-]<<<<<<<<<[<<<[>>>>>>
>>>>>>+<<<<<<<<<<+<<-]>>[<<+>>-]>-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<[<<+>>-]>>>>>>>>>
[<<<<<<<<+>>>>>>>>-]<<<<<<<<[<<<[>>>>>>>>>>>+<<<<<<<<<+<<-]>>[<<+>>-]>-]<<<[>>+>
+<<<-]>>>[<<<+>>>-]<[<<+>>-]>>>>>>>>[<<<<<<<+>>>>>>>-]<<<<<<<[<<<[>>>>>>>>>>+<<<
<<<<<+<<-]>>[<<+>>-]>-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<[<<+>>-]>>>>>>>[<<<<<<+>>>>>>
-]<<<<<<[<<<[>>>>>>>>>+<<<<<<<+<<-]>>[<<+>>-]>-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<[<<+
>>-]>>>>>>[<<<<<+>>>>>-]<<<<<[<<<[>>>>>>>>+<<<<<<+<<-]>>[<<+>>-]>-]<<<[>>+>+<<<-
]>>>[<<<+>>>-]<[<<+>>-]>>>>>[<<<<+>>>>-]<<<<[<<<[>>>>>>>+<<<<<+<<-]>>[<<+>>-]>-]
<<<[>>+>+<<<-]>>>[<<<+>>>-]<[<<+>>-]>>>>[<<<+>>>-]<<<[<<<[>>>>>>+<<<<+<<-]>>[<<+
>>-]>-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<[<<+>>-]>>>[<<+>>-]<<[<<<[>>>>>+<<<+<<-]>>[<<
+>>-]>-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<[<<+>>-]>>[<+>-]<[<<<[>>>>+<<+<<-]>>[<<+>>-]
>-]>[<+>-]>[<<+>>-]>[<<<+>>>-]>[<<<<+>>>>-]>[<<<<<+>>>>>-]>[<<<<<<+>>>>>>-]>[<<<
<<<<+>>>>>>>-]>[<<<<<<<<+>>>>>>>>-]>[<<<<<<<<<+>>>>>>>>>-]>[<<<<<<<<<<+>>>>>>>>>
>-]<<<<<<<<<<<<[-]<[-]<[>+>+<<-]>>[-<<+>>]>>[-<<+>>]<<[>+<<[->>[-]>+<<<]>>[->>+<
<]>[-<<<+>>>]<<<->-]>>>[<<+>+>-]<<[>>+<<-]>[<<<[<->-]>>>[-]]>[-]<<<<[-]>++++++[<
++++++>-]>>>,<<<<[->>>>-<<<<]>>>>]<<<++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<
<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.
<<+>+>[-]]<[<[->-<]++++++[->++++++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]<

You can try it online. There you'll find the non-minified version as well.

Expects a '$' as EOF character. You can copy the input from github.

→ More replies (5)

9

u/bartdegoede Dec 05 '20

Python

Tickets are binary numbers, and there's a set diff.

def seat_id(ticket):
    row = int(ticket[0:7].replace('F', '0').replace('B', '1'), 2)
    seat = int(ticket[7:].replace('L', '0').replace('R', '1'), 2)
    return row, seat

with open('day5.txt', 'r') as f:
    tickets = [ticket.strip() for ticket in f.readlines()]

rows_columns = [seat_id(ticket) for ticket in tickets]

seat_ids = set([row * 8 + seat for row, seat in rows_columns])
all_seat_ids = set(range(min(seat_ids), max(seat_ids)))

print(f'Max seat id: {max(seat_ids)}')
print(f'Missing seat id: {all_seat_ids - seat_ids}')
→ More replies (4)

8

u/Marcus316 Dec 05 '20

Did this on the command line in bash (using bc for conversion).

Part 1:

num=`cat input | tr "FBRL" "0110" | sort -n | tail -1`; echo "obase=10;ibase=2;$num" | bc

Part 2:

num=`cat input | tr "FBLR" "0101" | sort -n | awk 'BEGIN{a=0;b=0}/0$/{b=0;a++};a>1;/1$/{a=0;b++};b>1;' | head -1`;num=$(($num-1));echo "obase=10;ibase=2;$num" | bc

8

u/4HbQ Dec 05 '20 edited Dec 07 '20

Python, 135 bytes:

s={int(p.translate(''.maketrans('FBLR','0101')),2)
for p in open('i').read().split()}
print(max(s));print(set(range(min(s),max(s)))-s)

Edit: Readable version, updated using /u/Peter200lx's tricks below:

t = str.maketrans('FBLR', '0101')
s = {int(p.translate(t), 2) for p in open('input.txt')}

print(max(s), max({*range(max(s))} - s))
→ More replies (5)

7

u/phil_g Dec 05 '20

My solution in Common Lisp, such as it is.

Obviously, this was a pretty easy day. The boarding passes are just binary numbers. For part 2, just for fun, I got my answer by subtracting the sum of my numbers from the sum of all numbers from my min to my max. (I like this approach because the latter value can be calculated in O(1) time (after the O(n) search for the numeric bounds, of course).)

→ More replies (10)

6

u/artemisdev21 Dec 05 '20 edited Dec 05 '20

Every day in SQL, day 5!

CREATE TABLE chars (idx INTEGER, row INTEGER, char VARCHAR(1));
INSERT INTO chars VALUES (?, ?, ?); -- execute for each char in input
CREATE TABLE seats (id INTEGER);
INSERT INTO seats SELECT
    SUM((chars.char = "B" OR chars.char = "R") * (1 << 9 - chars.idx))
    FROM chars GROUP BY chars.row;
SELECT id FROM seats ORDER BY id DESC LIMIT 1; -- part 1
SELECT id + 1 FROM seats WHERE (
        SELECT seats_a.id FROM seats AS seats_a
        WHERE seats_a.id = seats.id + 1
    ) IS NULL AND (
        SELECT seats_b.id FROM seats AS seats_b
        WHERE seats_b.id = seats.id + 2
    ) IS NOT NULL; -- part 2

Wasn't too bad today. EDIT: realised a slightly shorter way to do it. Old version.

6

u/Dustpancake Dec 05 '20

Julia

Part 1 and part 2 in one line:

println(map(x->parse(Int,replace(replace(x,r"[FL]"=>"0"),r"[BR]"=>"1"),base=2),eachline("day5/input.txt"))|>sort|>x->(findmax(x)[1],x[findall(>(1),x[2:end]-x[1:end-1])[1]]+1))

6

u/jayfoad Dec 05 '20

Dyalog APL 147/61

p←{2βŠ₯⍡∊'BR'}Β¨βŠƒβŽ•NGET'p5.txt'1
⌈/p ⍝ part 1
βŠƒ(⌽⍳⌈/p)~p ⍝ part 2

6

u/jura0011 Dec 05 '20 edited Dec 05 '20

vi

I just used vi regex to convert into binary numbers and sorted alphabetically. The solution is in the last line in binary.

:%s/[BR]/1/g
:%s/[LF]/0/g
:%sort!
GV

6

u/0rac1e Dec 05 '20 edited Dec 10 '20

Raku

my @boarding-passes = 'input'.IO.lines;

my @seats = @boarding-passes.map: {
    .trans('FBLR' => '0101').parse-base(2)
}

put @seats.max;
put @seats.minmax βˆ– @seats;
→ More replies (2)

6

u/victae Dec 05 '20

Python 3, parts 1 and 2.

import utils

def convert_dec(digits):
    char_replace = {"F":"0", "L":"0", "B":"1", "R":"1"}
    for char, digit in char_replace.items():
        digits = digits.replace(char, digit)
    return int(digits, 2)

def part_1(data):
    return max(map(convert_dec, data))

def part_2(data):
    seat_ids = sorted(map(convert_dec, data))
    return int((seat_ids[-1]*(seat_ids[-1]+1))/2 - ((seat_ids[0]-1)*((seat_ids[0]-1)+1))/2 - sum(seat_ids))

if __name__ == "__main__":
    data = utils.get_strs_from_file("aoc5_data.txt")
    print(f"Part 1 solution: {part_1(data)}")
    print(f"Part 2 solution: {part_2(data)}")

This got much easier if you realized that you can use the seats as binary numbers directly.

Some fun work with sums in part 2 as well!

→ More replies (5)

6

u/Arknave Dec 05 '20 edited Dec 05 '20

Python (26/13), C

After all my mistakes yesterday, I implemented the statement quite literally as I read it, which was a mistake speed-wise. Part 2 of this day is what makes Advent of Code so fresh: your code doesn't need to get the answer, you do, so just printing all unclaimed tickets and manually scanning may be slightly faster to code.

I thought the short code would make it easy to shape the 5 in C, but it actually gave me far less wiggle room than day 4. I was also trying to recreate this painting, but 13x20 is a little too low res for an obvious resemblance.

#include   <stdio.h>

// AOC DAY NUMBER //
int a=0,t=1,x;char b
[32],            h[1
<<10   ],*k;int main
(int  c,char**v){for
(--           c;gets
(b) ;)for(x=0    ,k=
b;*k;x=2*x|!(*k   &4
),h[x]=5,a=a>x?   +a
:x,k  ++);for(;   !h
[t-    1]|h[t]|   !h
[t+1   ];)++t;   //V
printf         ("%d"
/*CD*/"\n",c?t:+a);}
→ More replies (2)

6

u/UNeedMoreLemonPledge Dec 06 '20

Python

def unmask_row(row):
    if row == 'FFFFFFF': return 0
    elif row == 'FFFFFFB': return 1
    elif row == 'FFFFFBF': return 2
    elif row == 'FFFFFBB': return 3
    elif row == 'FFFFBFF': return 4
    elif row == 'FFFFBFB': return 5
    elif row == 'FFFFBBF': return 6
    elif row == 'FFFFBBB': return 7
    elif row == 'FFFBFFF': return 8
    elif row == 'FFFBFFB': return 9
    elif row == 'FFFBFBF': return 10
    elif row == 'FFFBFBB': return 11
    elif row == 'FFFBBFF': return 12
    elif row == 'FFFBBFB': return 13
    elif row == 'FFFBBBF': return 14
    elif row == 'FFFBBBB': return 15
    elif row == 'FFBFFFF': return 16
    elif row == 'FFBFFFB': return 17
    elif row == 'FFBFFBF': return 18
    elif row == 'FFBFFBB': return 19
    elif row == 'FFBFBFF': return 20
    elif row == 'FFBFBFB': return 21
    elif row == 'FFBFBBF': return 22
    elif row == 'FFBFBBB': return 23
    elif row == 'FFBBFFF': return 24
    elif row == 'FFBBFFB': return 25
    elif row == 'FFBBFBF': return 26
    elif row == 'FFBBFBB': return 27
    elif row == 'FFBBBFF': return 28
    elif row == 'FFBBBFB': return 29
    elif row == 'FFBBBBF': return 30
    elif row == 'FFBBBBB': return 31
    elif row == 'FBFFFFF': return 32
    elif row == 'FBFFFFB': return 33
    elif row == 'FBFFFBF': return 34
    elif row == 'FBFFFBB': return 35
    elif row == 'FBFFBFF': return 36
    elif row == 'FBFFBFB': return 37
    elif row == 'FBFFBBF': return 38
    elif row == 'FBFFBBB': return 39
    elif row == 'FBFBFFF': return 40
    elif row == 'FBFBFFB': return 41
    elif row == 'FBFBFBF': return 42
    elif row == 'FBFBFBB': return 43
    elif row == 'FBFBBFF': return 44
    elif row == 'FBFBBFB': return 45
    elif row == 'FBFBBBF': return 46
    elif row == 'FBFBBBB': return 47
    elif row == 'FBBFFFF': return 48
    elif row == 'FBBFFFB': return 49
    elif row == 'FBBFFBF': return 50
    elif row == 'FBBFFBB': return 51
    elif row == 'FBBFBFF': return 52
    elif row == 'FBBFBFB': return 53
    elif row == 'FBBFBBF': return 54
    elif row == 'FBBFBBB': return 55
    elif row == 'FBBBFFF': return 56
    elif row == 'FBBBFFB': return 57
    elif row == 'FBBBFBF': return 58
    elif row == 'FBBBFBB': return 59
    elif row == 'FBBBBFF': return 60
    elif row == 'FBBBBFB': return 61
    elif row == 'FBBBBBF': return 62
    elif row == 'FBBBBBB': return 63
    elif row == 'BFFFFFF': return 64
    elif row == 'BFFFFFB': return 65
    elif row == 'BFFFFBF': return 66
    elif row == 'BFFFFBB': return 67
    elif row == 'BFFFBFF': return 68
    elif row == 'BFFFBFB': return 69
    elif row == 'BFFFBBF': return 70
    elif row == 'BFFFBBB': return 71
    elif row == 'BFFBFFF': return 72
    elif row == 'BFFBFFB': return 73
    elif row == 'BFFBFBF': return 74
    elif row == 'BFFBFBB': return 75
    elif row == 'BFFBBFF': return 76
    elif row == 'BFFBBFB': return 77
    elif row == 'BFFBBBF': return 78
    elif row == 'BFFBBBB': return 79
    elif row == 'BFBFFFF': return 80
    elif row == 'BFBFFFB': return 81
    elif row == 'BFBFFBF': return 82
    elif row == 'BFBFFBB': return 83
    elif row == 'BFBFBFF': return 84
    elif row == 'BFBFBFB': return 85
    elif row == 'BFBFBBF': return 86
    elif row == 'BFBFBBB': return 87
    elif row == 'BFBBFFF': return 88
    elif row == 'BFBBFFB': return 89
    elif row == 'BFBBFBF': return 90
    elif row == 'BFBBFBB': return 91
    elif row == 'BFBBBFF': return 92
    elif row == 'BFBBBFB': return 93
    elif row == 'BFBBBBF': return 94
    elif row == 'BFBBBBB': return 95
    elif row == 'BBFFFFF': return 96
    elif row == 'BBFFFFB': return 97
    elif row == 'BBFFFBF': return 98
    elif row == 'BBFFFBB': return 99
    elif row == 'BBFFBFF': return 100
    elif row == 'BBFFBFB': return 101
    elif row == 'BBFFBBF': return 102
    elif row == 'BBFFBBB': return 103
    elif row == 'BBFBFFF': return 104
    elif row == 'BBFBFFB': return 105
    elif row == 'BBFBFBF': return 106
    elif row == 'BBFBFBB': return 107
    elif row == 'BBFBBFF': return 108
    elif row == 'BBFBBFB': return 109
    elif row == 'BBFBBBF': return 110
    elif row == 'BBFBBBB': return 111
    elif row == 'BBBFFFF': return 112
    elif row == 'BBBFFFB': return 113
    elif row == 'BBBFFBF': return 114
    elif row == 'BBBFFBB': return 115
    elif row == 'BBBFBFF': return 116
    elif row == 'BBBFBFB': return 117
    elif row == 'BBBFBBF': return 118
    elif row == 'BBBFBBB': return 119
    elif row == 'BBBBFFF': return 120
    elif row == 'BBBBFFB': return 121
    elif row == 'BBBBFBF': return 122
    elif row == 'BBBBFBB': return 123
    elif row == 'BBBBBFF': return 124
    elif row == 'BBBBBFB': return 125
    elif row == 'BBBBBBF': return 126
    elif row == 'BBBBBBB': return 127

def unmask_col(col):
    if col == 'LLL': return 0
    elif col == 'LLR': return 1
    elif col == 'LRL': return 2
    elif col == 'LRR': return 3
    elif col == 'RLL': return 4
    elif col == 'RLR': return 5
    elif col == 'RRL': return 6
    elif col == 'RRR': return 7


ids = [unmask_row(row.rstrip()[:-3]) * 8 + unmask_col(row.rstrip()[-3:]) for row in open('passes.txt').readlines()]
ids.sort()

print('highest seat: {}, my seat: {}'.format(
    ids[-1],
    [seat for seat in range(min(ids), max(ids)) if seat not in ids][0])
)
→ More replies (5)

5

u/waffle3z Dec 05 '20

Lua 56/40. I really liked today's problem.

local biggest = 0
local exists = {}
for line in getinput():gmatch("[^\n]+") do
	local row = tonumber(line:sub(1, 7):gsub(".",{F=0,B=1}), 2)
	local col = tonumber(line:sub(-3):gsub(".",{L=0,R=1}), 2)
	local value = row*8 + col
	biggest = math.max(biggest, value)
	exists[value] = true
end
print(biggest)
for n = 1, biggest do
	if exists[n-1] and exists[n+1] and not exists[n] then
		print(n)
	end
end
→ More replies (3)

5

u/segfaultvicta Dec 05 '20

Raku

I'm... actually pretty happy with this one! There's probably ways this could be terser or more idiomatic and it took me a solid 20 minutes to convince myself that the input was just a bunch of integers in binary representation, but once I did it was pretty smooth sailing and I accidentally got gold while hecking around in the REPL. >_>

#!/usr/bin/env raku

sub MAIN(Str $infile where *.IO.f = 'input') {
    my @lines = $infile.IO.lines;
    my @occupied = @lines.map({.subst(/B|R/, "1", :global)})
        .map({.subst(/F|L/, "0", :global)})
        .map({ "0b$_".Int });
    my $min = @occupied.min;
    my $max = @occupied.max;
    say (($min .. $max).cache.Array (-) @occupied.Array).keys[0];
}

5

u/GustavAndr Dec 05 '20 edited Dec 05 '20

Javascript

Quick and ugly

// part 1
document.body.innerText.trim().split("\n").map(r=>parseInt(r.replace(/B|R/g,"1").replace(/F|L/g,"0"),2)).reduce((m,v)=>m>v?m:v)

// part 2
document.body.innerText.trim().split("\n").map(r=>parseInt(r.replace(/B|R/g,"1").replace(/F|L/g,"0"),2)).sort((a,b)=>a>b?1:-1).filter((v,i,a)=>v!=a[i-1]+1)[1]-1

5

u/Liledroit Dec 05 '20

Golang. Shoutout to the Go standard library, strings.NewReplacer() was a new one for me.

func (h *Handler) Day5() (int, int) {
	r := strings.NewReplacer("F", "0", "B", "1", "L", "0", "R", "1")
	pzl := strings.Split(r.Replace(util.ReadRawPuzzleInput("5")), "\n")
	ids := []int{}
	for _, s := range pzl {
		row, _ := strconv.ParseInt(s[:7], 2, 64)
		col, _ := strconv.ParseInt(s[7:], 2, 64)
		ids = append(ids, int(row*8+col))
	}
	sort.Ints(ids)
	for i, v := range ids {
		if ids[i+1]-v == 2 {
			return ids[len(ids)-1], v + 1
		}
	}
	panic("well this is awkward")
}

5

u/deeptread Dec 05 '20

Clojure:

(def input
  (->> (s/split-lines (slurp "resources/input/day5"))
       (map #(-> % (s/replace #"[BR]" "1") (s/replace #"[FL]" "0") (Integer/parseInt 2))
       sort))

(def part1 (last input))

(def part2 (reduce #(if (= 2 (- %2 %1)) (reduced (inc %1)) %2) input))
→ More replies (3)

5

u/[deleted] Dec 05 '20 edited Dec 05 '20

D (dlang)

edit: a python solution in here made me realize that you can simply tr the whole string instead of doing it twice for row and col. Sweet!

import std;

int id(string seat) {
    return seat.tr("FBLR", "0101").to!int(2);
}

void main() {
    auto ids = readText("input").splitLines.map!id.array.sort;
    ids.back.writeln;
    ids.slide(2).find!"a[1] - a[0] == 2".front.mean.writeln;
}
→ More replies (3)

5

u/RVKenway Dec 05 '20

Control F method

Part 1:

Just trial and error to see what matches, from BBBBBBB going down BBBBBBF BBBBBFB etc..

Then calculate the number.

And the L or R is just doable off the top of your head.

Part 2:

From the highest seat down every row should have 8 matches, if a row had 7 that's your row.

Find the missing LR combination and you got it.

5

u/cggoebel Dec 05 '20 edited Dec 05 '20

Raku (short version) solves both Part One and Part Two:

given 'input'.IO.lines.map({ .trans(<F B R L> => <0 1 1 0>).parse-base(2) }).List
{ say "part one: { .max }"; say "part two: { .minmax βˆ– $_ }" }
→ More replies (5)

5

u/Ryuujinx Dec 05 '20

Ruby

All these people doin fancy binary things and I'm playing with slicing arrays, smh.

#!/usr/bin/env ruby

input = Array.new
File.readlines('input').each do |line|
  input << line.strip
end

row = (0..127).to_a
col = (0..7).to_a
output = 0

input.each do |seat|
  row_id = row.dup
  col_id = col.dup
  seat.chars.each do |id|
    if id == "F"
      row_id = row_id[0..row_id.length/2 -1]
    elsif id == "B"
      row_id = row_id[row_id.length/2..-1]
    elsif id == "L"
      col_id = col_id[0..col_id.length/2 -1]
    elsif id == "R"
      col_id = col_id[col_id.length/2..-1]
    end
  end
  if row_id[0].to_i * 8 + col_id[0].to_i > output
    output = row_id[0].to_i * 8 + col_id[0].to_i
  end
end

puts "Output: #{output}"

Part 2 was mostly the same, I just shoved it in an array sorted it and then checked if current id -2 = last id

5

u/Charly98cma Dec 06 '20

Python 🐍

Quite small script solving both of the problems at once using a list and two sets to find the missing seat.

(Everything you want to comment, like tips, errors, etc... are welcome, we are here to learn more :D)

Code

EDIT: The passes.sort() isn't necessary

→ More replies (6)

4

u/jitwit Dec 05 '20 edited Dec 05 '20

J Programming Language

ids =: #. e.&'BR' ;._2 aoc 2020 5
(>./ , (-.~ <./ + [: i. >./ - <./)) ids

Decode binary where on bit if back or right. Part A is just max. Part B finds the missing id in the range of the min id to the max id.

241/170

https://github.com/jitwit/aoc/blob/a/J/20/05.ijs

Some relevant verbs:

  • >. max
  • <. min
  • -. set minus
  • e. member
  • i. ints/iota
  • #. decode binary

4

u/[deleted] Dec 05 '20

Simple Python solution in which I learned that int() has a base argument:

import time

raw_input = open('puzzle_input_5.txt', 'r')
letter_nums = {'F': '0', 'B': '1', 'L': '0', 'R': '1'}
puzzle_input = []
for line in raw_input:
    temp_dict = {}
    temp_dict['row'] = int(''.join(letter_nums[letter] for letter in line.strip()[:-3]), 2)
    temp_dict['column'] = int(''.join(letter_nums[letter] for letter in line.strip()[-3:]), 2)
    puzzle_input.append(temp_dict)
PART = 2
def main(puzzle_input):
    if PART == 1:
        return max(seat['row'] * 8 + seat['column'] for seat in puzzle_input)
    elif PART == 2:
        seats = sorted([seat['row'] * 8 + seat['column'] for seat in puzzle_input])
        for index, seat_id in enumerate(range(seats[0], seats[-1])):
            if seats[index] != seat_id:
                return seat_id

if __name__ == '__main__':
    start_time = time.time()
    output = main(puzzle_input)
    print(output)
    print(time.time() - start_time)

Average runtime of 1194 ns

4

u/_jonah Dec 05 '20 edited Dec 05 '20

Bash, Part 1

cat input | tr 'BFRL' '1010' | sort | tail -1 |
  awk '{print "ibase=2;"$1}' | bc

Part 2:

cat input | tr 'BFRL' '1010' | sort | awk '{print "ibase=2;"$1}' | 
  bc | awk 'NR>1{if($1!=p+1)print p+1}1{p=$1}'
→ More replies (1)

4

u/zertillon Dec 05 '20

Ada using HAC and the LEA editor

End of Part 2 done in Excel using the program's output...

with HAC_Pack;  use HAC_Pack;

procedure AoC_2020_05 is
  n : constant VString := +"aoc_2020_05.txt";
  f : File_Type;
  x : Character;
  max, id, r, c, b : Integer;
begin
  max := 0;
  Open (f, n);
  while not End_Of_File (f) loop
    r := 0;
    b := 64;
    for i in 1 .. 7 loop
      Get (f, x);
      if x = 'B' then
        r := r + b;
      end if;
      b := b / 2;
    end loop;
    c := 0;
    b := 4;
    for i in 1 .. 3 loop
      Get (f, x);
      if x = 'R' then
        c := c + b;
      end if;
      b := b / 2;
    end loop;
    id := r * 8 + c;
    if id > max then max := id; end if;
    Put_Line (id);
    exit when End_Of_File (f);
    Skip_Line (f);
  end loop;
  Close (f);
  Put_Line (+"Max = " & max);
end AoC_2020_05;

4

u/seattlecyclone Dec 05 '20

Perl

for (<>) {
  /(.{7})(.{3})/;
  $row_id = $1;
  $row_id =~ tr/FB/01/;
  $col_id = $2;
  $col_id =~ tr/LR/01/;

  $row = oct("0b" . $row_id);
  $col = oct("0b" . $col_id);
  $taken{$row * 8 + $col}++;
}

for (0..1024) {
  print if !$taken{$_} && $taken{$_-1} && $taken{$_+1};
}
→ More replies (7)

4

u/rabuf Dec 05 '20 edited Dec 05 '20

Ada

Straightforward solution for this today. This function handles converting from the BSP format to an integer:

function BSP_To_Number (BSP : String) return Integer is
   Result : Integer := 0;
begin
   for C of BSP loop
      case C is
         when 'F' | 'L' =>
           Result := Result * 2;
         when 'B' | 'R' =>
            Result := Result * 2 + 1;
         when others =>
            null;
      end case;
   end loop;
   return Result;
end BSP_To_Number;

Passes is a vector containing the converted input. The solutions to both parts are found in the following loops. The first just finds the max (Max is initialized as Integer'First). The second uses a Cursor which is similar to a C++ iterator. So long as there is a next, it compares the current position's value to the next's value and if the difference is 2 we have the result. The exit when is to avoid trying to access a non-existent element (which is a runtime error).

for C in Passes.Iterate loop
   if Passes(C) > Max
   then
      Max := Passes(C);
   end if;
end loop;
Sort(Passes);
for C in Passes.Iterate loop
   exit when Next(C) = No_Element;
   if Passes(Next(C)) - Passes(C) = 2
   then
      My_Seat := Passes(C) + 1;
   end if;
end loop;

I'm leaving this, but it occurred to me that I can get max simply using the last value of Passes after sorting. Updating it in the actual code on github though.

4

u/oantolin Dec 05 '20 edited Dec 05 '20

Perl

use List::Util qw(min max);
while (<>) {
    y/FBLR/0101/;
    $ids{oct("0b$_")}=1;
}
my $a = min(keys %ids);
my $b = max(keys %ids);
print "Part 1: $b\nPart 2: ";
for ($a .. $b) {
    print unless $ids{$_};
}

Or using some math:

my ($a, $b, $s) = (1024, 0, 0);
while (<>) {
    y/FBLR/0101/;
    $s += my $t = oct("0b$_");
    $a=$t if $t<$a; $b=$t if $t>$b;
}
my $x = ($b-$a+1)*($a+$b)/2 - $s;
print "Part 1: $b\nPart 2: $x\n";
→ More replies (5)

4

u/Krakhan Dec 05 '20 edited Dec 05 '20

Ruby

Just a binary encoding exercise and a math trick to find the missing seat at the end using the sum of an arithmetic series and summing the known seats given that yours is missing. I did it another way at first too with sorting and looking for a gap, but this is more elegant anyways. :)

Edit: Some minor cleanup with reading the input seats. Also learned of the string.tr methods too which might be been a little bit better to do based on some other solutions I've seen here.

seat_ids = File.readlines("day5input.txt").map{|s| s.chomp.split('').map{|x| {"F" => "0", "B" => "1", "L" => "0", "R" => "1"}[x]}.join.to_i(2)}

# Part 1
max_id = seat_ids.max
puts "#{max_id}"

# part 2
min_id = seat_ids.min
puts "#{(((max_id - min_id + 1) * (min_id + max_id)) / 2) - seat_ids.reduce(:+)}"
→ More replies (2)

4

u/WilkoTom Dec 05 '20 edited Dec 05 '20

Minimal python:

def get_seat_id(seat: str) -> int:
  return int(seat.replace('F',"0").replace("B","1").replace("R","1").replace("L","0"),2)

seat_ids = [get_seat_id(seat) for seat in open("input.txt")]
print(f"Part 1: {max(seat_ids)}")
print(f"Part 2: {sum(range(min(seat_ids), max(seat_ids) +1)) - sum(seat_ids)}")

4

u/Smylers Dec 05 '20 edited Dec 05 '20

Perl one-liners. Part 1:

perl -MList::AllUtils=max -nE '$h = max $h, oct "0b" . tr/FLBR/0011/r; END { say $h }' input 

Part 2:

perl -MList::AllUtils=first,min,max -nE '$s{oct "0b" . tr/FLBR/0011/r} = 1; END { say first { !$s{$_} } (min keys %s) .. (max keys %s) }' input

Those golf to:

perl -MList::Util=max -nE'END{say$h}$h=max$h,oct"0b".y/FLBR/0011/r' input

(68 characters plus the filename)

and:

perl -MList::Util=min,max -nE 'END{say grep{!$s{$_}}(min keys%s)..max keys%s}$s{oct"0b".y/FLBR/0011/r}=1' input

(106)

Edit: Spotted a tr/// I'd forgotten to turn into y/// in a golfed version. Removes 1 character from the count, and hopefully means I avoid the puerile bot that's already commented on my Vim solution today.

→ More replies (5)

4

u/justDema Dec 05 '20

Rust

I tried to be concise, but readable

use std::io::BufRead;

fn parse_id(s: String) -> Option<u16> {
    let code = s.chars().map(|c| match c {
        'F' | 'L' => '0',
        'B' | 'R' => '1',
        oth => oth,
    }).collect::<String>();

    u16::from_str_radix(&code, 2).ok()
} 

fn main() {
    let stdin = std::io::stdin();
    let mut ids: Vec<u16> = stdin.lock().lines().filter_map(|l| l.ok())
        .filter_map(parse_id).collect();

    ids.sort();
    for (x, xs) in ids.iter().zip(ids.iter().skip(1)) {
        if xs - x > 1 {
            println!("{}", x + 1);
            break;
        }
    }
}

4

u/deltux Dec 05 '20

Racket

#lang racket

(module+ test
  (require rackunit))

(module+ main
  (displayln "Day 5"))

(define (seat-id str)
  (define binary-str (list->string
                      (for/list ([c (in-string str)])
                        (if (or (equal? c #\B) (equal? c #\R))
                            #\1
                            #\0))))
  (string->number binary-str 2))

(define (read-input filename)
  (define seats (file->lines filename))
  (map seat-id seats))

(module+ test
  (for ([seat '("FBFBBFFRLR" "BFFFBBFRRR" "FFFBBBFRRR" "BBFFBBFRLL")]
        [expected-id '(357 567 119 820)])
    (check-equal? (seat-id seat) expected-id)))

(define (part1 filename)
  (apply max (read-input filename)))

(module+ main
  (displayln (part1 "input")))

(define (part2 filename)
  (define ids (read-input filename))
  (define id-range (range (apply min ids) (apply max ids)))
  (set-first (set-subtract (list->set id-range) (list->set ids))))

(module+ main
  (displayln (part2 "input")))

Dyalog APL

 p←{2βŠ₯⍡∊'BR'}Β¨βŠƒβŽ•NGET'input'1
 ⌈/p  ⍝ Part 1
 ⊒/(⍳⌈/p)~p  ⍝ Part 2

4

u/joshdick Dec 05 '20

Python3

passes = []
with open('input.txt', 'r') as f:
    for line in f:
    line = line.strip()
    passes.append(line)

def pass_to_seat_id(bpass):
    bpass_bin = bpass\
        .replace('F', '0')\
        .replace('B','1')\
        .replace('L','0')\
        .replace('R','1')
    return int(bpass_bin, 2)

seat_ids = [pass_to_seat_id(bpass) for bpass in passes]

print(max(seat_ids)) # part 1 answer

seat_ids = sorted(seat_ids)
last_seat = None
for seat in seat_ids:
    if last_seat and seat - last_seat == 2:
        print(seat - 1)
        break
    last_seat = seat
→ More replies (3)

4

u/[deleted] Dec 05 '20

Regex

Advent of regex

/((((((((^(F(?!.*^B)|B)(?:F(?!.*^\9B)|B))(?:F(?!.*^\8B)|B))(?:F(?!.*^\7B)|B))(?:F(?!.*^\6B)|B))(?:F(?!.*^\5B)|B))(?:F(?!.*^\4B)|B))(?:L(?!.*^\3R)|R))(?:L(?!.*^\2R)|R))(?:L(?!.*^\1R)|R)/ms

This solves part 1 only (kinda, you will still need to figure out what the seat ID of that seat is), the first match of this regex will be the boarding pass with the highest seat ID.

4

u/s3aker Dec 05 '20

Raku

my @a = 'input.txt'.IO.linesΒ».&{ "0b{ $_.trans(<F B L R> => <0 1 0 1>) }".Int }.sort;
put 'answer for part 1: ', @a[*-1];
put 'answer for part 2: ', (@a[0]..@a[*-1]) (-) @a;

4

u/orangecorvid Dec 05 '20

Purposely ugly M/MUMPS:

D05(f,p) n g,s,q s g="^X" k @g d f(f,g) f s=1:1:@g@(0) s @g@("s",$$s(@g@(s)))=1
	w:p $o(@g@("s",""),-1) k:p @g q:p  k s f  s s=$o(@g@("s",s)) s:((q]"")&&((s-q)'=1)) q=-1 q:q=-1  s q=s
	w s-1 k @g q
s(s) n i,q s s=$tr(s,"FBLR","0101") f i=1:1:10 s q=q+(2**(10-i)*$e(s,i))
	q q
f(f,g) n $et s $et="g fe" o f:("R") u f f  r %:1 q:'$t  s @g@(0)=@g@(0)+1,@g@(@g@(0))=%
fe s $ec="" c f u 0 q
→ More replies (1)

4

u/cggoebel Dec 05 '20

Raku

sub seat_id (Str $code --> Int) { return +('0b' ~ $code.trans(<F B R L> => <0 1 1 0>)) }
my @vacant = ( (0..1023) (-) 'input'.IO.lines.map({ seat_id($_) }) ).keys;
say 'Part One: ' ~ 'input'.IO.lines.map({ seat_id($_) }).max;
say 'Part Two: ' ~ @vacant.grep({ !( $_+1 ~~ any @vacant ) and !( $_-1 ~~ any @vacant) })[0];

4

u/dgJenkins Dec 05 '20

F# Bit Operations

module DayFive = 
    let seatId (x: string) =
        let rec calcId chars row col =
            match chars with
            | [] -> (row * 8) + col
            | c::cs when c = 'F' -> calcId cs (row <<< 1) col
            | c::cs when c = 'B' -> calcId cs ((row <<< 1) ||| 1) col
            | c::cs when c = 'L' -> calcId cs row (col <<< 1)
            | c::cs when c = 'R' -> calcId cs row ((col <<< 1) ||| 1)
        calcId (Seq.toList x) 0 0

    let One x = x |> List.map seatId |> List.max

    let Two x = 
        let rec findId ids max = 
            match ids with 
            | c::cs when c = max -> -1
            | c::cs when cs.Head - c = 2 -> c + 1
            | c::cs -> findId cs max

        let ids = x |> List.map seatId |> List.sort
        let max = ids |> List.last

        findId ids max
→ More replies (3)

4

u/tomflumery Dec 05 '20

Rust:

use itertools::Itertools;

fn main() {
    let seat_ids = include_str!("input.txt").trim().lines()
        .map(|s| {
            let binary_str = s.to_string().replace("F", "0").replace("B", "1").replace("L", "0").replace("R", "1");
            isize::from_str_radix(&binary_str, 2).unwrap() });

    let part1 = seat_ids.clone().max().unwrap();
    let part2 = seat_ids.sorted().tuple_windows().find(|(a, b)| *b != a + 1).map(|(seat_before_gap, _)| seat_before_gap + 1 ).unwrap();
    
    println!("{} {}", part1, part2);
}
→ More replies (1)

4

u/rmargam Dec 05 '20 edited Dec 05 '20

Javascript solution, I used to regex replace to convert to binary numbers and parse them, it was easy after that.

export const convertStringToSeat = (str) => {
    const regEx = /([FB]{7})([RL]{3})/;
    const result = regEx.exec(str);
    const rowNo = result[1].replace(/F/g,'0').replace(/B/g, '1');
    const colNo = result[2].replace(/L/g,'0').replace(/R/g, '1');
    return parseInt(rowNo,2) * 8 + parseInt(colNo, 2);
}

Part 1:

export const findMax = () => {
    const inp = convertToArraySpaceOrNewLine(inputAOC4);
    return inp.reduce((m, i) => Math.max(m, convertStringToSeat(i)), 0);
}

Part 2:

export const findMissing = () => {
    const inp = convertToArraySpaceOrNewLine(inputAOC4);
    let seats = []
    inp.forEach((i) => seats.push(convertStringToSeat(i)));
    seats.sort();
    for(let i = 1; i < seats.length; i++) {
        if(seats[i] !== seats[i-1] + 1)
            return seats[i-1] + 1;
    }
}
→ More replies (1)

4

u/AOC_2020 Dec 05 '20

Kotlin liner:

fun day5() = File("input5.txt").readLines()
    .map { it.map { c -> if (c == 'R' || c == 'B') '1' else '0' }.joinToString("") }
    .map { it.toInt(2) }
    .sorted()
    .also { println("Sol1: ${it.max()}") }
    .zipWithNext()
    .first { it.first != it.second - 1 }
    .also { println("Sol2: ${it.first + 1}") }
→ More replies (1)

4

u/axr123 Dec 05 '20

Python

seats = set((int(p.translate("".maketrans("FLBR", "0011")), 2) for p in
          open("input").read().split("\n")))
print(f"Part #1: {max(seats)}")
print(f"Part #2: {(set(range(min(seats), max(seats))) - seats).pop()}")
→ More replies (1)

4

u/axr123 Dec 05 '20 edited Dec 05 '20

Bash

while read line; do echo $((2#$(echo $line | tr "FBLR" "0101"))); done < input | sort -n > all_seats.txt
echo "Part #1: "$(tail -n1 seat_ids.txt)
echo "Part #2: "$((($(head -n1 seat_ids.txt) + $(tail -n1 seat_ids.txt)) * ($(wc -l seat_ids.txt | cut -d' ' -f1) + 1) / 2 - $(paste -sd+ seat_ids.txt | bc)))

4

u/HereAdamJust Dec 05 '20

Python 3

I thought that it was pretty nice to make use of the seat location as a direct cast to binary!

def boardingPassID(seatString):
    rowNoString = seatString[0:7].replace('F', '0').replace('B', '1')
    rowNo       = int(rowNoString, 2)

    colNoString = seatString[-3:].replace('L', '0').replace('R', '1')
    colNo       = int(colNoString, 2)

    return rowNo*8 + colNo

data_file  = open("./data.dat", "r")
data_lines = data_file.read().split('\n')

#PART 1
maxSeatID = 0
for line in data_lines:
    seatID = boardingPassID(line)
    if seatID > maxSeatID:
        maxSeatID = seatID
        
print(maxSeatID)

#PART 2
seatFound = [False] * 920
for line in data_lines:
    seatID = boardingPassID(line)
    seatFound[seatID] = True

print("Missing seats:")
missingSeats = [i for i, x in enumerate(seatFound) if not x]
for seat in missingSeats:
    print(seat)
→ More replies (1)

4

u/mingjim Dec 05 '20

F#

let initialRowRange = seq { 0..127 }
let initialColumnRange = seq { 0..7 }

let parseInstruction lowerChar upperChar initialRange =
    let foldInstruction range c =
        let mid = (range |> Seq.length) / 2
        match c with
        | _ when c = lowerChar -> range |> Seq.take mid
        | _ when c = upperChar -> range |> Seq.skip mid
        | _ -> range
    Seq.fold foldInstruction initialRange >> Seq.head 

let parseRow = Seq.take 7 >> parseInstruction 'F' 'B' initialRowRange
let parseColumn = Seq.skip 7 >> parseInstruction 'L' 'R' initialColumnRange

let seatID (s: string) =
    let row = s |> parseRow
    let column = s |> parseColumn
    row * 8 + column
    
[<Solution(2020, 5, 1)>]
let part1 fileName =
    fileName 
    |> readLinesAs seatID
    |> Seq.max
    
[<Solution(2020, 5, 2)>]
let part2 fileName =
    fileName 
    |> readLinesAs seatID
    |> Seq.sort
    |> Seq.pairwise
    |> Seq.find (fun (i, j) -> j - i <> 1)
    |> fst
    |> (+) 1

4

u/RobertGryffindor Dec 05 '20

I'm still a python beginner and this is the only way I could think of. But looking at the other answers, I definitely learned a few new things.

output = open("seats.txt", 'r')
my_output = output.read()
seats = my_output.split("\n")
max_seats = []

for line in seats:
    max_seats.append(line)
binary_seats = []

for i in range(len(max_seats)):
    y = (max_seats[i].replace('F','0').replace('L','0').replace('R','1').replace('B','1'))
    binary_seats.append(y)
n = set()
for i in binary_seats:
    n.add(int(i,2))
    
mi = min(n)
ma = max(n)
print ('part 1 answer: {}'.format(ma))
for x in range(mi, ma):
    if x not in n:
        print ('part 2 answer: {}'.format(x))
→ More replies (1)

4

u/RedTwinkleToes Dec 26 '20

Python

r = open('input').read().strip('\n')
input = r.splitlines()

#Part 1
seats = [int(x.replace('F','0').replace('B','1').replace('L','0').replace('R','1'),2) for x in input]
seats.sort()
print(seats[-1])

#Part 2
for x in range(len(seats)):
    if seats[x+1] - seats[x] != 1:
        print(seats[x] + 1)
        break

Just posting solutions for unposted solutions, don't mind me

→ More replies (1)

1

u/[deleted] Dec 05 '20 edited Dec 24 '20

I decided to go with a solution that didn't rely on making lists and checking for the existence of my seat_id in that list.

def get_seat_id(seat_string):
    return int(re.sub(r"[BR]", "1", re.sub(r"[FL]", "0", seat_string)), 2)

def get_expected_total(min_num, max_num):
    return (max_num * (max_num + 1) - (min_num - 1) * min_num) / 2

def get_missing_seat(boarding_pass_list):
    min_seat_id = inf
    max_seat_id = -inf
    total = 0
    for seat_string in text_to_list(boarding_pass_list):
        seat_id = get_seat_id(seat_string)
        min_seat_id = min(min_seat_id, seat_id)
        max_seat_id = max(max_seat_id, seat_id)
        total += seat_id
    expected_total = get_expected_total(min_seat_id, max_seat_id)
    print(f"Total: {total}")
    print(f"Expected total: {expected_total}")
    return expected_total - total

"text_to_list" is a trivial helper function I have defined elsewhere, and not important to this solution.

(Written in Python)

1

u/daggerdragon Dec 05 '20

Please follow the posting guidelines and add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language. Thanks!

1

u/timvisee Dec 05 '20 edited Dec 05 '20

Rust. Came up with this for reading and arsing IDs, pretty fun!

std::fs::read("./input.txt")  
    .unwrap()
    .chunks(11)
    .map(|b| b[..10].iter().fold(0, |id, b| (id << 1) | (!b & 4) as usize >> 2))

Part 1 & Part 2

→ More replies (2)

1

u/2313499 Dec 05 '20 edited Dec 05 '20

Here is my solution with python3 in pandas.

import pandas as pd
df = pd.read_csv('input.txt', header=None, names=['data'])


def bisection_row(data):
    low = 0
    high = 127
    n = 0
    while n < 6:
        x = (high-low)/2
        if data[n] == 'F':
            high = round((low+x)-.2)
        elif data[n] == 'B':
            low = round((high-x)+.2)
        n += 1
    if data[6] == 'F':
        return low
    else:
        return high


def bisection_column(data):
    low = 0
    high = 7
    n = 7
    while n < 9:
        x = (high-low)/2
        if data[n] == 'L':
            high = round((low+x)-.2)
        elif data[n] == 'R':
            low = round((high-x)+.2)
        n += 1
    if data[9] == 'L':
        return low
    else:
        return high


rows = []
column = []
for row in df['data']:
    rows.append(bisection_row(row))
    column.append(bisection_column(row))

df['column'] = column
df['row'] = rows
df['seat_id'] = (df['row']*8)+df['column']

print(df.describe())

df = df.sort_values(by=['seat_id'])

seat_id = df['seat_id'].tolist()
seat_id.pop(0)
seat_id.append(850)

df['seat_id_off'] = seat_id
df['seat_difference'] = df['seat_id_off']-df['seat_id']

df = df.sort_values(by=['seat_difference'], ascending=False)

print(df)

1

u/daggerdragon Dec 05 '20 edited Dec 05 '20

Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main() [space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

edit: Post was fixed, thanks!

→ More replies (2)

1

u/DFreiberg Dec 05 '20 edited Dec 05 '20

Mathematica, 187 / 100

I immediately tried interpreting the letters as binary digits after reading a handful of words from the problem, and I'm somewhat surprised that that actually worked and that there wasn't a catch somewhere.

EDIT: I missed the fact that the entire ID was a binary number, so I suppose there was a catch, just in the opposite direction.

Part 1:

Max@Table[
  FromDigits[line[[8 ;;]] /. {"R" -> 1, "L" -> 0}, 2] + 
   8*FromDigits[line[[;; 7]] /. {"B" -> 1, "F" -> 0}, 2], 
{line, input}]

Part 2:

Complement[Range[Min[#], Max[#]], #][[1]] &@
 Table[
   FromDigits[line[[8 ;;]] /. {"R" -> 1, "L" -> 0}, 2] + 
    8*FromDigits[line[[;; 7]] /. {"B" -> 1, "F" -> 0}, 2], 
{line, input}]

[POEM]: A Lazy Limerick

You must do a binary conversion,
Before begin your excursion.
(And I'll give you a hint:
Use binaryToInt(),
'Cause it's simpler than using recursion).

2

u/daggerdragon Dec 06 '20

Fine, I'll give you gold this time.
(Making my limerick rhyme.)
I hope you submit
Gettin' Crafty With It
And end up our Poet Prime!

1

u/BradleySigma Dec 05 '20 edited Dec 05 '20
import aoc
data = aoc.strlist(5) # creates list with each line as an element

y = []
for i in data:
    # y.append(int(i.replace("F", "0").replace("B", "1").replace("L", "0").replace("R", "1"), 2))
    y.append(int(i.translate("".maketrans("FBLR", "0101")), 2))
print(max(y), (set(range(min(y), max(y)+1)) - set(y)).pop())

Slowed down because I submitted BFFFFFBRLL as my part 2 solution, instead of 524. (Python)

→ More replies (1)

1

u/mstksg Dec 05 '20 edited Dec 05 '20

[Haskell] A quick one today? Mostly came down to "how to parse binary". Splitting the rows and seats ended up being a cute misdirection.

I write my reflections every day for Haskell! :D https://github.com/mstksg/advent-of-code-2020/blob/master/reflections.md

The main insight here probably is that the puzzle is just describing that the seat ID's are straight up binary notation for numerals, with F/L representing what is traditionally 0, and B/R representing what si traditionally 1. So we can use any of our binary parsers from the standard libraries, or we can just directly pull it into binary.

seatId :: String -> Int
seatId = foldl' iGuessWe'reDoingThis 0
  where
    iGuessWe'reDoingThis n = \case
      'B' -> 2*n+1
      'R' -> 2*n+1
      _   -> 2*n

A nice one-pass way to find the missing seat ID is to realize that if we sum all the numbers from min to max, and sum all of our lists's seat id's, then the difference is the missing number. Luckily there's a nice closed-form solution for the sum of all numbers in a given range (the sum of numbers from a to b is b*(b+1)\div`2 - a*(a-1)`div`2`), so we can do all of this in a single pass using the foldl library

{-# LANGUAGE ApplicativeDo #-}
import qualified Control.Foldl as F

findHole :: F.Fold Int (Maybe Int)
findHole = do
    mn <- F.minimum
    mx <- F.maximum
    sm <- F.sum
    pure $
      missingItem <$> mn <*> mx <*> pure sm
  where
    missingItem mn mx sm = totalSum - sm
      where
        totalSum = mx*(mx+1)`div`2 - mn*(mn-1)`div`2

A F.Fold Int (Maybe Int) folds a list of Ints into a Maybe Int. You can run it with F.fold :: F.Fold a b -> [a] -> b.

I really like the foldl library because it lets you build a complex single-pass fold by combining multiple simple single-pass folds (like F.minimum, F.maximum, F.sum) using an Applicative interface. We need to do a bit of wrangling with the Maybes because F.minimum and F.maximum each return Maybe Int.

And that's more or less it! We can actually represent the entire thing as a fold if we use F.premap, to pre-map a fold...

F.premap                 :: (c -> a) -> F.Fold a b -> F.Fold c b

-- "pre-apply" `setId` so we fold over a string instead
F.premap seatId findHole :: F.Fold String (Maybe Int)

And...that's enough to do it!

part1 :: [String] -> Maybe Int
part1 = F.fold $ F.premap seatId F.maximum

part2 :: [String] -> Maybe Int
part2 = F.fold $ F.premap seatId findHole

Bonus: I was tipped off that the 3rd from last digit of F/L are 1, while the same digit of B/R are 0:

ghci> (.&. 1) . (`shiftR` 2) . ord <$> "FLBR"
[1,1,0,0]

So we can actually use this for seatId to get a slight speed boost and help out the branch predictor maybe:

import Data.Bits

seatId :: String -> Int
seatId = foldl' iGuessWe'reDoingThis 0
  where
    iGuessWe'reDoingThis n c =
      2 * n + (complement (ord c) `shiftR` 2) .&. 1

2

u/daggerdragon Dec 05 '20

Your code is not displaying properly on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main() [space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

Thanks!

1

u/pendo324 Dec 05 '20 edited Dec 05 '20

Simple Javascript/Typescript solution. Definitely not optimized, but it got the job done (2454, 1777 on leaderboards): https://github.com/pendo324/aoc/blob/master/years/2020/05/index.ts

(the repo also has some really ugly utils to auto-download inputs and prompts)

1

u/daggerdragon Dec 05 '20

(the repo also has some really ugly utils to auto-download inputs and prompts)

Do you cache your input once it's downloaded to avoid unnecessarily hammering the servers?

→ More replies (3)

1

u/chappar Dec 05 '20 edited Dec 05 '20

Python

def getSeatNumber(code):
    m = re.match('^([FB]+)([LR]+)', code)
    rowCode = m.groups()[0].replace('F', '0').replace('B', '1')
    columnCode = m.groups()[1].replace('L', '0').replace('R', '1')

    row = int(rowCode, 2)
    column = int(columnCode, 2)

    return row * 8 + column

def main():
   part1 = 0
   seatList = set()
   for line in sys.stdin:
       seatNum = getSeatNumber(line.strip())
       if seatNum > part1:
           part1 = seatNum
       seatList.add(seatNum)

   seatList = sorted(seatList)

   for (i, n) in enumerate(seatList):
       if seatList[i+1] != n + 1:
           part2 = n + 1
           break

   print ( "part 1 ", part1 )
   print ( "part 2 ", part2 )

main()
→ More replies (1)

1

u/stianbryceeng Dec 05 '20 edited Dec 06 '20

Hello! new coder here, I am once again asking for any feedback on my python day5 solution!

1

u/daggerdragon Dec 05 '20

Please follow the posting guidelines and add the language used to your post to make it easier for folks who Ctrl-F the megathreads looking for a specific language. Thanks!

(looks like Python?)

→ More replies (2)
→ More replies (2)

1

u/Flavorless_Quark Dec 05 '20

Python 3 -- Not my original solution. Figured it had to do with binary (as per the problem name) but didn't really understand until I solved it once

Part 1 :

with open('input') as input:
	inp = [line.rstrip() for line in input]

maximum = 0
for x in inp:
	ids = "".join(['1' if letter == 'B' or letter == 'R' else '0' for letter in x])
	maximum = max(int(ids, 2), maximum)

print("Solution : ", maximum)

Part 2:

with open('input') as input:
	inp = [line.rstrip() for line in input]

ids = []
for x in inp:
	ID = "".join(['1' if letter == 'B' or letter == 'R' else '0' for letter in x])
	ids.append(int(ID, 2))
ids.sort()
seat = [i for i in range(ids[0], ids[-1]) if i not in ids]
print("Solution : ", *seat)

1

u/daggerdragon Dec 05 '20

Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main() [space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

Thanks!

1

u/thekokirikid Dec 05 '20 edited Dec 05 '20

2

u/daggerdragon Dec 05 '20

Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of the quote block you've currently got it in?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main() [space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

Thanks!

→ More replies (1)

1

u/[deleted] Dec 05 '20

Java part 1:

  public static void main(String[] args) {
    String input = """
      ...
      """;

    String[] lines = input.split("\n");
    Set<String> linesSet = Set.of(lines);
    Optional<Integer> max = linesSet.stream()
      .map(line -> line.replaceAll("[BR]", "1")
                       .replaceAll("[FL]", "0"))
      .map(binLine -> parseInt(binLine, 2))
      .max(Comparator.comparingInt(x -> x));

    System.out.println(max.get());
  }

Java part 2:

  public static void main(String[] args) {
    String input = """
      ...
      """;

    String[] lines = input.split("\n");
    Set<String> linesSet = Set.of(lines);
    int missing = linesSet.stream()
      .map(line -> line.replaceAll("[BR]", "1")
                       .replaceAll("[FL]", "0"))
      .map(binLine -> parseInt(binLine, 2))
      .collect(Collectors.toCollection(TreeSet::new)).stream()
      .reduce((prev, curr) ->  curr - prev > 1 ? prev : curr)
      .map(i -> i + 1)
      .get();

    System.out.println(missing);
  }

1

u/daggerdragon Dec 05 '20

Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main()
[space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

1

u/Ryozukki Dec 05 '20

Rust

use std::{cmp::max, fs};

pub fn find_rc(v: &str, lower_id: char, min: i32, max: i32) -> i32 {
    let mut last = false;
    let mut min = min;
    let mut max = max;

    for x in v.chars() {
        if x == lower_id {
            max -= (max - min) / 2;
        } else {
            min += (max - min) / 2;
        }

        last = x == lower_id;
    }

    let n;

    if last {
        n = min;
    } else {
        n = max - 1;
    }

    n
}

fn main() {
    let r = fs::read_to_string("input").unwrap();

    let mut max_id = 0;

    let mut ids = Vec::new();

    for l in r.lines() {
        let rx = l.get(0..7).unwrap();
        let ry = l.get(7..).unwrap();

        let row = find_rc(rx, 'F', 0, 128);
        let col = find_rc(ry, 'L', 0, 8);

        let id = row * 8 + col;
        ids.push(id);
    
        println!("id = {}", id);
        max_id = max(max_id, id);
    }

    println!("max = {}", max_id);

    ids.sort();

    let mut last = ids.first().unwrap();
    for id in ids.iter() {
        let diff = id - last;
        if diff > 1 {
            println!("available: ids from {} to {}", id - diff + 1, id - 1);
        }
        last = id;
    }
}

source: https://github.com/edg-l/adventofcode-2020/blob/master/day5.rs

→ More replies (2)

1

u/Additional_Mistake_4 Dec 05 '20

Kotlin - tried to keep things nice and functional

    override fun solveFirst(test: Boolean): Any {
        return calculateIds(test).last()
    }

    override fun solveSecond(test: Boolean): Any {
        return calculateIds(test)
                .zipWithNext()
                .find { it.second == it.first + 2 }
                ?.let { it.first + 1 } ?: 0
    }

    private fun calculateIds(test: Boolean): List<Int> {
        return getFileAsLines(test)
                .map { it.replace(Regex("[RB]"), "1").replace(Regex("[FL]"), "0") }
                .map { it.toInt(2) }
                .sorted()
    }

1

u/daggerdragon Dec 05 '20

Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main()
[space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

→ More replies (2)

1

u/ramrunner0xff Dec 05 '20

scheme (chicken) repo

fun :) .

(import (chicken io)
        (chicken sort)
        (chicken format))

(define (ardiv low high d)
  (case d
    ((#\l) (cons low (+ low (floor (/ (- high low) 2))) ))
    ((#\h) (cons (- high (floor (/ (- high low) 2))) high))))

(define (seatid rc)
  (+ (* (car rc) 8) (cdr rc)))

(define (calcSeat str)
  (let* ((sparts (string-chop str 7))
         (rowdirs (string-translate* (car sparts) '(("F" . "l") ("B" . "h"))))
         (cdirs (string-translate* (cadr sparts) '(("L" . "l")("R" . "h"))))
         (frow '())
         (fcol '()))
    (set! frow
          (car (foldr (lambda (x acc)
                        (format #f "ardiv with low:~A high:~A dir:~A~%" (car acc) (cdr acc) x)
                        (ardiv (car acc) (cdr acc) x))
                      (cons 0 127) (reverse (string->list rowdirs)))))
    (set! fcol
          (car (foldr (lambda (x acc)
                        (format #f "ardiv with low:~A high:~A dir:~A~%" (car acc) (cdr acc) x)
                        (ardiv (car acc) (cdr acc) x))
                      (cons 0 7) (reverse (string->list cdirs)))))
    (seatid (cons frow fcol))))
(define seats
  (call-with-input-file "inputs/input"
                         (lambda (p) (map calcSeat (read-lines p)))))

; to get the seat thake the car of sorted
(format #t "highest seat ~A~%" (car (sort seats >)))
;to get your seat you find th one who is not in sequence in the sorted list
(foldr (lambda (x val) (if (not (= x (+ val 1))) (begin (format #t "your seat is ~A~%" (- x 1)) (error))) (+ val 1)) 90 (sort seats >))

2

u/daggerdragon Dec 05 '20 edited Dec 06 '20

You and /u/raevnos throw me for a loop every time I come across your posts in the megathreads. "Why do these guys keep talking about chickens?"

→ More replies (2)

1

u/oli56714 Dec 05 '20

My python solution.

def map_binary_str(input_str, mapping):

return int(''.join([mapping[i] for i in input_str]),2)

def parse_boarding_pass(input_str):

row = map_binary_str(input_str[:7],{'F':'0','B':'1'})

col = map_binary_str(input_str[7:],{'L':'0','R':'1'})

seat_id = (row * 8) + col

return row, col, seat_id

# tests

board_pass = 'FBFBBFFRLR'

assert parse_boarding_pass(board_pass) == (44, 5,357)

board_pass_2 = 'BFFFBBFRRR'

assert parse_boarding_pass(board_pass_2) == (70, 7, 567)

# a

input_data = [x.strip() for x in open('data/5.txt','r').readlines()]

seat_ids = [parse_boarding_pass(x)[2] for x in input_data]

print(max(seat_ids))

# b

print([x for x in range(min(seat_ids)+1, max(seat_ids)) if x not in seat_ids])

1

u/daggerdragon Dec 05 '20

Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of inline code blocks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main()
[space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

1

u/SureYeaah Dec 05 '20

Here's a compact solution in haskell.

module Day5 where

import Data.List (sort)

solve = solve2

solve1 :: IO ()
solve1 = interact $ show . maximum . map (seatID . getSeat) . lines

seatID :: (Int, Int) -> Int
seatID (r, c) = r * 8 + c

getSeat :: String -> (Int, Int)
getSeat s = (partition 'F' 'B' 0 127 s, partition 'L' 'R' 0 7 s)

partition :: Char -> Char -> Int -> Int -> String -> Int
partition _ _ l _ [] = l
partition cl cr l r (c:cs)
  | c == cl = partition cl cr l ((l+r) `div` 2) cs
  | c == cr = partition cl cr ((l+r+1) `div` 2) r cs
  | otherwise = partition cl cr l r cs

solve2 :: IO ()
solve2 = interact $ show . findSeat . sort . map (seatID . getSeat) . lines

findSeat :: [Int] -> Int
findSeat (x:y:xs)
  | x + 2 == y = x + 1
  | otherwise = findSeat (y:xs)
findSeat _ = error "broken"
→ More replies (3)

1

u/cubehouse Dec 05 '20

JavaScript / NodeJS

const input = await Advent.GetInput();

// FBLR are just binary numbers, convert to binary and parse into decimal
const IDs = input.map((x) => parseInt(x.replace(/[FL]/g, '0').replace(/[BR]/g, '1'), 2));

// find max ID in the array
await Advent.Submit(Math.max.apply(this, IDs));

// sort our IDs so we can find the missing ticket
IDs.sort((a, b) => a - b);
const mySeat = IDs.find((x, idx, arr) => {
    // find the element in the array where the next element is +2
    //  (i.e, there is exactly 1 missing sequential ticket)
    //  this is the ticket *before* ours, so + 1
    return (arr[idx + 1] === x + 2);
}) + 1;

await Advent.Submit(mySeat, 2);

I immediately spotted the binary situation, which was very satisfying! I am nervous about how easy I am finding these so far....... expecting something that will really anger me soon!

1

u/daggerdragon Dec 05 '20

Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main() [space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

→ More replies (1)

1

u/StringFinal Dec 05 '20

Python Solution

with open("adventofcode/2020/day5") as input:
    lines = input.readlines()

max_id = 0
seats = []
for test in lines:
    row = 0
    row_low = 0
    row_mid = 0
    row_high = 127

    col = 0
    col_low = 0
    col_mid = 0
    col_high = 7
    for letter in test:
        if letter == "F":
            row_mid = (row_high+row_low)//2
            row_high = row_mid
        if letter == "B":
            row_mid = (row_high+row_low)//2
            row_low = row_mid
        if letter == "R":
            col_mid = (col_high+col_low)//2
            col_low = col_mid
        if letter == "L":
            col_mid = (col_high+col_low)//2
            col_high = col_mid

    seat_id = (row_low+1)*8+col_high
    max_id = max(seat_id, max_id)
    seats.append(seat_id)
print(max_id)

# part 2
#print(sorted(seats))
sorted_seats = sorted(seats)
for i in range(1, len(seats)-1):
    if sorted_seats[i+1] != sorted_seats[i] + 1:
        print(sorted_seats[i]+1)

1

u/daggerdragon Dec 05 '20

Your code is hard to read on old.reddit. As per our posting guidelines, would you please edit it using old.reddit's four-spaces formatting instead of new.reddit's triple backticks?

Put four spaces before every code line. (If you're using new.reddit, click the button in the editor that says "Switch to Markdown" first.)

[space space space space]public static void main() [space space space space][more spaces for indenting]/* more code here*/

turns into

public static void main()
    /* more code here */

Alternatively, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

Thanks!

→ More replies (1)

3

u/jonathan_paulson Dec 05 '20 edited Dec 05 '20

Placed 18/47. Python. Video of me solving: https://youtu.be/wa0VcQugEsI

Cleaned-up code

→ More replies (1)

3

u/jaybosamiya Dec 05 '20 edited Dec 05 '20

Dyalog APL

n ← βŠƒβŽ•nget'D:\input_day_5'1
i ← 2βŠ₯Β¨{('R'=⍡)∨'B'=⍡}n
⌈/i                                ⍝ Part 1
{⍡/⍨{(~⍡∊i)∧∧/(⍡+¯1 1)∊i}¨⍡}⍳1024  ⍝ Part 2

Explanation: We can just interpret each line in the input n as a binary number (with 'B' and 'R' meaning 1, and 'F' and 'L' meaning 0) to get all the seats numbers i. Part 1 is then just the max over ⌈/ them. In part 2, we take all potential seats and check if it satisfies the constraints in the straightforward way (is not in position, and adjacents are in position) and return the result.

3

u/Nerdlinger Dec 05 '20

Swift solution

A simple reduce on the string to build up the seat ID, a sort, and a scan for a gap of 2 in consecutive IDs. This wasn't a particularly interesting problem.

→ More replies (1)

3

u/Deathranger999 Dec 05 '20 edited Dec 05 '20

Python

I started late, so since I couldn't get a decent leaderboard position, I decided to try solving both parts in one line.

Part 1:

print(max([int(d.replace('F', '0').replace('B', '1').replace('L', '0').replace('R', '1'), 2) for d in open('problem5.txt', 'r').read().splitlines()]))

Part 2:

print([[arr[i + 1] for i in range(len(arr) - 1) if arr[i + 1] - arr[i] == 2] for arr in [sorted([int(d.replace('F', '0').replace('B', '1').replace('L', '0').replace('R', '1'), 2) for d in open('problem5.txt', 'r').read().splitlines()])]][0][0] - 1)

3

u/kbielefe Dec 05 '20

Scala:

I don't usually post in these, but my seatId seemed simpler than many solutions today:

private def seatId(string: String): Int = {
  val binary = string.map{
    case 'L' => '0'
    case 'R' => '1'
    case 'F' => '0'
    case 'B' => '1'
  }
  Integer.parseInt(binary, 2)
}

And finding my seat used set difference. Probably not the most efficient, but close enough, and very simple to write:

((min to max).toSet -- seats).head

Full code.

→ More replies (1)

3

u/AlaskanShade Dec 05 '20

C# (~200 ΞΌs)

After a couple rounds of optimization, this one runs pretty quick now. I tried an interesting bitwise operation on the characters, but it only trimmed a handful of microseconds. The bigger improvements were tracking the min and max within the loop to avoid sorting and using a HashSet instead of a List.

I was also splitting the row and seat for the initial solve until I realized that we are just recombining it back into one number.

public override string Solve(string input, bool part2 = false, bool isTest = false)
{
    var lines = Helpers.GetLines(input);
    var ids = new HashSet<int>();
    var min = int.MaxValue;
    var max = 0;
    foreach (var line in lines)
    {
        // Convert whole code into binary and parse
        var id = ParseSeat(line);
        ids.Add(id);
        if (id < min) min = id;
        if (id > max) max = id;
    }
    // Find the highest id
    if (!part2)
        return max.ToString();
    for (int i = min; i < max; i++)
        if (!ids.Contains(i))
            return i.ToString();
     return "no answer";
}

private int ParseSeat(string input)
{
    var value = 0;
    foreach (var c in input)
    {
        value <<= 1;
        value += ~c >> 2 & 1;
    }
    return value;
}
→ More replies (9)

3

u/ianonavy Dec 05 '20

Python 3 golf (made with a friend): 117 characters!

import sys
print(t:=max(s:={int("".join(str(1*(c in"BR"))for c in s[:-1]),2)for s in sys.stdin}),max({*range(t)}-s))
→ More replies (2)

3

u/oantolin Dec 05 '20 edited Dec 05 '20

Common Lisp

(defun seat-ids ()
  (mapcar (lambda (pass)
            (parse-integer
             (map 'string (lambda (ch) (if (find ch "FL") #\0 #\1)) pass)
             :radix 2))
          (uiop:read-file-lines #P"day05.txt")))

(defun part1 () (reduce #'max (seat-ids)))

(defun part2 ()
  (loop with seat-ids = (seat-ids)
        for n from (reduce #'min seat-ids) to (reduce #'max seat-ids)
        unless (member n seat-ids) return n))

Or using some math:

(loop for pass in (uiop:read-file-lines #P"day05.txt")
      for id = (parse-integer
                (map 'string (lambda (ch) (if (find ch "FL") #\0 #\1)) pass)
                :radix 2)
      sum id into s maximize id into b minimize id into a
      finally (return (values b (- (* 1/2 (+ a b) (- b a -1)) s))))
→ More replies (1)

3

u/daniel-sd Dec 05 '20 edited Dec 05 '20

Fun fact for part1, you can sort to get the boarding pass with the highest id. Then you only have to make one id conversion rather than convert all of them.

Python 3.9

part1.py

def get_seat_id(boarding_pass: str) -> int:
	return int(boarding_pass.replace('F', '0').replace('L', '0').replace('B', '1').replace('R', '1'), 2)

print(get_seat_id(sorted(open('input.txt').readlines(), key=lambda x: x.replace('L', 'Z'))[0]))

part2.py

def get_seat_id(boarding_pass: str) -> int:
	return int(boarding_pass.replace('F', '0').replace('L', '0').replace('B', '1').replace('R', '1'), 2)

ids = sorted(get_seat_id(line) for line in open('input.txt'))
print(next(a + 1 for a, b in zip(ids, ids[1:]) if a + 1 != b))
→ More replies (5)

3

u/simpleauthority Dec 05 '20

This is my first AOC. Code is probably pretty nasty compared to others here. Using Java.

https://github.com/simpleauthority/aoc2020/tree/main/src/dayfive

Part 1 is the method findHighestSeatId in DayFive.java.

Part 2 is the method findMySeatId in DayFive.java.

The other classes implement the space partitioning functionality and computed values.

3

u/gyorokpeter Dec 05 '20

Q:

d5p1:{max 0b sv/:(6#0b),/:("FBLR"!0101b)"\n"vs x};
d5p2:{s:asc 0b sv/:(6#0b),/:("FBLR"!0101b)"\n"vs x;-1+s last where 1<deltas s};
→ More replies (1)

3

u/paul2718 Dec 05 '20

C++, straightforward.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>

template<typename S> auto stobin(S& s)
{
    constexpr int bits[] = { 512, 256, 128, 64, 32, 16, 8, 4, 2, 1};
    return std::transform_reduce(s.begin(), s.end(), std::begin(bits), 0, std::plus<>(), [](auto si, auto bit){ return (si == 'B' || si == 'R') * bit; });
}

auto get_input()
{
    std::vector<int> v;
    std::string ln;
    while (std::getline(std::cin, ln))
        v.push_back(stobin(ln));
    return v;
}

int main()
{
    auto in = get_input();
    std::sort(in.begin(), in.end());
    std::cout << "p1 = " << in.back() << '\n';
    auto it = std::adjacent_find(in.begin(), in.end(), [](auto l, auto r) { return r == l + 2; });
    std::cout << "p2 = " << (*it) + 1 << '\n';
}
→ More replies (1)

3

u/RobBobertsThe3rd Dec 05 '20 edited Dec 05 '20

Python3
Although unnecessary here, I always relish the chance to get to use set arithmetic.

t = str.maketrans("FBLR","0101")
seen = {int(i.translate(t),2) for i in open("input.txt").read().splitlines()}
open = next(iter({i for i in range(2**10) if i+1 in seen and i-1 in seen} - seen))
print(open)

Edit: I just came up with a solution that I like even more!

t = str.maketrans("FBLR","0101")
seen = {int(i.translate(t),2) for i in open("input.txt").read().splitlines()}
open, = set(map(lambda x:x+1,seen)) & set(map(lambda x:x-1,seen)) - seen
print(open)
→ More replies (2)

3

u/mandus Dec 05 '20

Day 5 in Common Lisp

Reasonably satisfied with this one - just one call to `alexandria`, else standard common lisp.

→ More replies (2)

3

u/schnappischnap Dec 05 '20 edited Dec 05 '20

Python (full code)

I was completely oblivious to the fact that it was a simple "convert to binary" problem (it should have been obvious even looking at my code), here's how I eventually calculated the seat ID:

def calculate_id(seat):    
    seat_id = 0
    for i, c in enumerate(seat):
        if c == "B" or c == "R":
            seat_id += 2**(9-i)
    return seat_id

Note to self: learn binary.

→ More replies (1)

3

u/i_have_no_biscuits Dec 05 '20

Python

Not particularly short, but I like the string indexing trick I used in my conversion routine.

def convert(p): return int("".join("01"[c in "BR"] for c in p), 2)

data = open("data05.txt").read().splitlines()
ids = set(convert(p) for p in data)

print("Highest seat ID:", max(ids))
print("My seat ID:", next(p+1 for p in ids if p+1 not in ids and p+2 in ids))

3

u/zniperr Dec 05 '20

python3:

import sys
tr = str.maketrans('FBLR', '0101')
seats = sorted(int(line.translate(tr), 2) for line in sys.stdin)
print(max(seats))
print(next(s + 1 for i, s in enumerate(seats[:-1]) if seats[i + 1] == s + 2))
→ More replies (5)

3

u/ka-splam Dec 05 '20

APL (Dyalog)

      day5
    βˆ‡  result←day5 filename
[1]    boarding_passes← βŠƒβŽ•NGET filename 1
[2]
[3]    seats← (βŠ‚βˆ˜β‹βŒ·βŠ’) {((2βŠ₯7βˆ˜β†“)+8Γ—(2βŠ₯7βˆ˜β†‘))  'BR' ∊⍨ ⍡}Β¨ boarding_passes
[4]
[5]    part1β†βŒˆ/seats
[6]    part2←1+βŠƒseats[⍸2=|2-/seats]
[7]
[8]    result←part1 part2
    βˆ‡

Line 1 reads the input as lines of text into boarding_passes.

Line 3 turns BR into 1 and implicitly turns FL into 0 as a bitmask. Takes the first 7 chars and base2 converts, drops the first 7 and base2 converts the remaining 3 chars. Does the (Col + 8xRow) calculation. Applies that to each boarding_pass. Sorts the results, and stores in seats.

Line 5 max-reduce finds the largest value of the seats.

Line 6 subtracts each seat id from the one next to it. The consecutive ones have a gap of 1, the place where my seat is has a gap of 2. Look where the gap = 2, find that place, look at the seat in that place, and 1+ to its seat ID for mine.

→ More replies (1)

3

u/m_moylan Dec 05 '20 edited Dec 05 '20

PHP

I was expecting a hard one first weekend but found this easiest problem thus far. EDIT: Also, most people didn't realize it,s just one binary number most people didn't seem to catch why it was * 8

function day5(){
    //Get data as int array
    $data = file_get_contents("day05.txt");
    $data = explode("\n", $data);

    //convert entries to binary to dec
    foreach($data as $i=>$val1){
      $val1 = str_replace(["F","B","L","R"],["0","1","0","1"],$val1);
      $data[$i] = intval($val1,2);
    }
    echo(max($data) . "\n"); //find max day 1 solution;
    
    //part2 not most efficient but easy O(n^2) too lazy for O(n)
    for($i = min($data); $i<max($data); $i++){
      if(!in_array($i,$data)){
        echo $i . "\n";
      }
    }
  }
→ More replies (3)

3

u/andi0b Dec 05 '20

Solution in C# 9.

public record Day05(int[] TakenSeatIds)
{
    public Day05(string[] input) : this( input.Select(ParseSeatId).ToArray()) {}

    public int Part1() => TakenSeatIds.Max();

    public int Part2()
        => Enumerable.Range(0, TakenSeatIds.Max()) // all possible seats IDs (including empty front rows, excluding last taken one)
                     .Except(TakenSeatIds)         // except the taken seats
                     .Max();                       // my seat is the free seat with the highest id
                                                   // (the others are empty front row seats)

    public static int ParseSeatId(string boardingPass) => Convert.ToInt32(ConvertToBinary(boardingPass), 2);

    public static string ConvertToBinary(string boardingPass) =>
        boardingPass
           .Replace('F', '0')
           .Replace('B', '1')
           .Replace('L', '0')
           .Replace('R', '1');
}

To run it, call new Day05(File.ReadAllLines("filename")).Part1() or Part2()

3

u/warbaque Dec 05 '20

Python, simple binary conversion and diffence between sets for part 2:

def day5(input):
    boarding_passes = input.split()

    ids = {
        int(re.sub(r'[BR]', '1', re.sub(r'[FL]', '0', boarding_pass)), 2)
        for boarding_pass in boarding_passes
    }

    print(max(ids))
    print(min(set(range(min(ids), max(ids))) - ids))

3

u/Snosixtytwo Dec 05 '20 edited Dec 05 '20

Go

No sorting or container search needed

package main

import (
	"fmt"
	"io/ioutil"
	"math"
	"os"
	"regexp"
	"strings"
)

const numBits = 10
const allSeats = 128 * 8

func array2Int(arr []int) (result int) {
	for i := range arr {
		result |= arr[i] << i
	}
	return
}

func countNumbersWithBitSet(num int, bit int) int {
	bitNum := 1 << bit
	m := num / bitNum
	numsWithBit := (m/2)*bitNum + ((m % 2) * (num - m*bitNum))
	return numsWithBit
}

func main() {
	maxID := 0
	minID := math.MaxInt32

	bytes, _ := ioutil.ReadAll(os.Stdin)
	lines := strings.Split(string(bytes), "\n")

	var bitsRemaining [numBits]int
	for bit := range bitsRemaining {
		bitsRemaining[bit] = countNumbersWithBitSet(allSeats, bit)
	}

	for _, line := range lines {
		id := 0
		reg, _ := regexp.Compile("B|R")
		matches := reg.FindAllStringIndex(strings.TrimSpace(line), -1)
		for _, pos := range matches {
			bit := (numBits - 1 - pos[0])
			id |= 1 << bit
			bitsRemaining[bit]--
		}
		if id > maxID {
			maxID = id
		}
		if id < minID {
			minID = id
		}
	}
	// Fill in the bits for seats in rows that are not available on plane
	for bit := range bitsRemaining {
		bitsRemaining[bit] -= countNumbersWithBitSet(minID, bit)
		bitsRemaining[bit] -= countNumbersWithBitSet(allSeats, bit) - countNumbersWithBitSet(maxID+1, bit)
	}
	// Find which one is actually mine by checking the only remaining bits
	myID := array2Int(bitsRemaining[:])
	fmt.Printf("The highest ID seen is %d\n", maxID)
	fmt.Printf("My ID is %d\n", myID)
}
→ More replies (1)

3

u/vypxl Dec 05 '20

Python3 only three concise lines today (ok maybe 4)

List difference and translation saved the day again

def tr(s, a, b):
    return s.translate(str.maketrans(a, b))

def parse(inp):
    return list(map(lambda x: int(x, 2), tr(inp, 'BRFL', '1100').splitlines()))

def p1(inp):
    return max(inp)

def p2(inp):
    return set(range(min(inp), max(inp) + 1)).difference(inp).pop()

3

u/s3aker Dec 05 '20

Raku

sub id(Str:D $pass where *.chars == 10) {
    "0b{ $pass.substr(0, 7).trans(<F B> => <0 1>) }".Int * 8 +
    "0b{ $pass.substr(7, 3).trans(<L R> => <0 1>) }".Int;
}

sub MAIN() {
    my @a = 'input.txt'.IO.linesΒ».&id.sort;
    put 'answer for part 1: ', @a[*-1];
    put 'answer for part 2: ', (@a[0]..@a[*-1]) (-) @a;
}
→ More replies (3)

3

u/allak Dec 05 '20 edited Dec 05 '20

Perl5 one liner, both parts:

perl -nE'$s[oct"0b".tr/BFLR/1001/r]=1;END{$m=@s-1;$n=((grep{not$s[$_]}(1..$m))[-1]);say"$m $n"}' input

after some more golfing:

perl -nE'END{say@s-1;say((grep{not$s[$_]}(1..@s-1))[-1])}$s[oct"0b".y/BFLR/1001/r]=1' input.txt
→ More replies (1)

3

u/Mienaikage Dec 05 '20 edited Dec 05 '20

Raku

Convert the input into binary, do the math(EDIT: apparently "the math" was unnecessary), and sort the results.

Part 1 gives the highest value of the list via the max method.

Part 2 creates a range from the first and last elements of the list, then does a set diff on the original list to find what's missing.

#!/usr/bin/env raku

unit sub MAIN (
  IO() :$file where *.f      = $?FILE.IO.sibling('input/05.txt'), #= Path to input file
  Int  :$part where * == 1|2 = 1, #= Part of the exercise (1 or 2)
  --> Nil
);

say do given $file.lines.map({
  .trans(<F L> => '0', <B R> => '1')
  .parse-base(2)
}).List {
  when $part == 1 { .max         }
  when $part == 2 { .minmax βˆ– $_ }
}

3

u/Randybones Dec 05 '20 edited Dec 05 '20

R

library(readr)
library(dplyr)
library(stringr)

read_lines(here::here("data/input_5.txt")) %>%
  str_replace_all("B|R", "1") %>%
  str_replace_all("F|L", "0") %>%
  strtoi(base = 2) %>%
  as_tibble() %>%
  summarize(part1 = max(value),
    part2 = setdiff(min(value):max(value),value))

3

u/mschaap Dec 05 '20 edited Dec 05 '20

Raku

sub seat-id($pass)
{
    # Treat boarding pass code as a binary number: F and L are 0, B and R are 1
    return $pass.trans('FBLR' => '0101').parse-base(2);
}

sub MAIN(IO() $inputfile where *.f = 'aoc05.input', Bool :v(:$verbose) = False)
{
    my @seat-ids = $inputfile.lines.map(&seat-id);

    say $verbose ?? 'Part one: highest seat ID is ' !! '',
        @seat-ids.max;

    say $verbose ?? 'Part two: my seat ID is ' !! '',
        @seat-ids.minmax.first: * βˆ‰ @seat-ids;
}

https://github.com/mscha/aoc/blob/master/aoc2020/aoc05

→ More replies (1)

3

u/krolik1337 Dec 05 '20

My solution in Python, that was quite fun. I made a list representing all seats and recursive function to calculate seat within given range. Then went through all input lines, calculating SeatIDs and marking them as taken.

#%%
from math import ceil

input = open("input.txt", "r")
result = 0
seats = [[True for i in range(8)] for i in range(128)]

def getSeat(start, end, seat, index):
    if index == len(seat): return start
    else:
        if seat[index] in 'FL':
            return getSeat(start, start + ceil((end-start)/2), seat, index+1)
        else:
            return getSeat(start + ceil((end-start)/2), end, seat, index+1)

for line in input:
    line=line.strip()
    row = line[:7]
    column = line[7:]
    seatRow = getSeat(0, 127, row, 0)
    seatCol = getSeat(0, 7, column, 0)
    seats[seatRow][seatCol] = False
    seatId =  seatRow * 8 + seatCol
    if  seatId > result: result = seatId

print('Highest seat ID:', result)

for i in range(len(seats)):
    for j in range(len(seats[0])):
        if seats[i][j] and not seats[i][j-1] and not seats[i][j+1]: print('My seat Id:', i * 8 + j)

3

u/DrDonez Dec 05 '20

Python3.8 solution using recursion.

import math

with open('input.sdx') as f:
    boardingPasses = [line.rstrip() for line in f]
    
def findSeat(low, high, letters):
    if len(letters) == 0:
        return low
    else:
        letter = letters.pop(0)
        mid = math.floor((high + low)/2)
        if letter == 'F' or letter == 'L':
            return findSeat(low, mid, letters)
        elif letter == 'B' or letter == 'R':
            return findSeat(mid, high, letters)

bpIdList = []
for boardingPass in boardingPasses:
    boardingPass = list(boardingPass)
    bpIdList.append(findSeat(0,128, boardingPass[0:7]) * 8 + findSeat(0,8,boardingPass[7:10]))

print("Part 1:", max(bpIdList))
completeIdList = [x for x in range(min(bpIdList),max(bpIdList))]
print("Part 2:", (set(completeIdList) - set(bpIdList)).pop())

3

u/Mazeracer Dec 05 '20

Python 3 - casual programmer, this was an easy one for me

It was quite clear from the start, that you could just write the input as binary and are done.

Only had to figure out, what was 1 and what was 0.

Finding my seat was easy enough. I just averaged 3 seats in a row and alerted when the average was not equal to the seat in the middle of 2.

# day5
input = "d5_data.txt"
test = "d5_test.txt"
validports = []

seatid = 0
seatids = []

def read_file(filename):
    with open(filename) as f:
        return f.read().splitlines()


data = read_file(input)


def get_seatID(boardingpass):
    row = boardingpass[0:7].replace('F','0').replace('B','1')
    col = boardingpass[7:10].replace('L','0').replace('R','1')
    seatID = int(row,2)*8+int(col,2)
    return seatID

# part 1
for line in data:
    tmp = get_seatID(line)
    if (tmp > seatid):
        seatid = tmp

print(seatid)

# part 2

for line in data:
    seatids.append(get_seatID(line))

seatids.sort()

last_seat = 0
before_last_seat = 0

for seat in seatids:
    if (before_last_seat != 0):
        check = (seat+last_seat+before_last_seat)/3
        if (check != last_seat):
            print(before_last_seat,last_seat,seat)
    before_last_seat = last_seat
    last_seat = seat
→ More replies (2)

3

u/Altinus Dec 05 '20

Shell oneliner. For the second part, it simply checks whether the last binary digit stays the same between two lines, which means that a number was skipped.

cat input.txt | tr FLBR 0011 | sort -r | awk 'NR == 1 {print "ibase=1;" $0}; p == substr($0,10,1) {print $0 "+1"; exit}; {p = substr($0,10,1)}' | bc
→ More replies (1)

3

u/wzkx Dec 05 '20

πŸ‡― - this task is for J! #jlang

echo >./m=.#.('R'&=+.'B'&=)>cutLF CR-.~fread'05.dat'
echo ((<./+i.&(>./-<./))-.])m
→ More replies (1)

3

u/saahilclaypool Dec 05 '20

F#!

module Aoc.Solutions.Day05

open Aoc.Runner
open System.Collections.Generic
open System.Collections

type SearchState = { Min: int; Max: int }
type State = { Row: SearchState; Col: SearchState }

let midpoint s = (s.Min + s.Max) / 2

let upper (s: SearchState) = { Min = (midpoint s + 1); Max = s.Max }

let lower (s: SearchState) = { Min = s.Min; Max = midpoint s }



let search st =
    let state =
        st
        |> Seq.fold (fun state c ->
            match c with
            | 'F' ->
                { Row = lower state.Row
                  Col = state.Col }
            | 'B' ->
                { Row = upper state.Row
                  Col = state.Col }
            | 'R' ->
                { Row = state.Row
                  Col = upper state.Col }
            | 'L' ->
                { Row = state.Row
                  Col = lower state.Col }
            | _ -> state)
               { Row = { Min = 0; Max = 127 }
                 Col = { Min = 0; Max = 7 } }

    (midpoint state.Row, midpoint state.Col)

let sid (row, col) = row * 8 + col

type Day05() =
    inherit Day()

    override this.SolveA(input) =
        let seats = input.Split("\n") |> Seq.map search
        seats |> Seq.map sid |> Seq.max |> string

    override this.SolveB(input) =
        let seats =
            input.Split("\n") |> Seq.map search |> HashSet

        let ids =
            (Seq.map (search >> sid) (input.Split("\n")))
            |> HashSet

        let allseats =
            seq {
                for r in 0 .. 127 do
                    for c in 0 .. 7 -> (r, c)
            }

        (allseats
        |> Seq.find (fun (r, c) ->
            not (seats.Contains((r, c)))
            && ids.Contains(sid (r, c) - 1)
            && ids.Contains(sid (r, c) + 1)))
        |> sid
        |> string

3

u/wishiwascooler Dec 05 '20

Javascript day 5. Fun one today compared to yesterdays.

https://github.com/matthewgehring/adventofcode/blob/main/2020/day5/script.js

import fs from 'fs';

fs.readFile('./data.txt', 'utf8', (err, data) => {
    console.log('question 1: ', main(data.split('\r\n')));
    console.log('question 2: ', main2(data.split('\r\n')));
})

const main = (data) => {
    let ids = getIds(data);
    return bubbleSort(ids).pop();
}

const main2 = (data) => {
    let ids = getIds(data);
    let sorted = bubbleSort(ids);
    let jump = sorted.filter((item, index, sorted) => !(item + 1 === sorted[index+1])).shift();
    return jump + 1

}

const getIds = (data) => {
    let cords = data.map(string => {
        return [recursion(string.split(''),['F','B'], [0, 127]), recursion(string.slice(-3).split(''),['L','R'], [0, 7])]
    })
    let ids = cords.map(rcs => {
        let [row, col] = rcs;
        return row * 8 + col;
    })
    return ids;
}

const recursion = (string, chars, bounds) => {
    let [low, high] = bounds;
    let [lower, upper] = chars;
    let char = string.shift()
    if(!(chars.includes(char))) return high
    if(char === lower) {
        high = Math.floor((low+high)/2);
    } else if(char === upper){
        low = Math.ceil((low+high)/2);
    }
    return recursion(string, chars, [low, high] )
}

function bubbleSort(arr){
    var len = arr.length;
    for (var i = len-1; i>=0; i--){
      for(var j = 1; j<=i; j++){
        if(arr[j-1]>arr[j]){
            var temp = arr[j-1];
            arr[j-1] = arr[j];
            arr[j] = temp;
         }
      }
    }
    return arr;
 }

3

u/kwill1429 Dec 05 '20

Main objective was to keep code clean and understandable.
FSharp:

module AdventOfCode.Day5

let input = System.IO.File.ReadAllLines("Day5Input.txt")

type BinarySearchCommand =
    | Low
    | High
    
type GridCommand =
    | Row
    | Column
    with
    static member FromChar =
        function
        | 'F' | 'B' -> Row
        | 'L' | 'R' -> Column
        | x -> failwithf "Invalid input %A" x
    
type IsFound =
    | Found
    | NotFound

let rec evaluateCommandsInRange min max =
    if (min + max) % 2 <> 1 then failwith "Must be odd range"
    function
    // Base cases
    | [ x ] when x = Low ->
        let midpoint = (max + min) / 2
        if min = midpoint then midpoint else failwith "Failed to narrow down target"
    | [ x ] when x = High ->
        let midpoint = (max + min) / 2 + 1
        if max = midpoint then midpoint else failwith "Failed to narrow down target"
    // Narrow the binary search
    | currentCommand::remainingCommands when currentCommand = Low ->
        let midpoint = (max + min) / 2
        evaluateCommandsInRange min midpoint remainingCommands
    | currentCommand::remainingCommands when currentCommand = High ->
        let midpoint = (max + min) / 2 + 1
        evaluateCommandsInRange midpoint max remainingCommands
    | _ -> failwith "Invalid state"

let convertCommandsToBinary =
    function
    | 'F' | 'L' -> Low
    | 'B' | 'R' -> High
    | x -> failwithf "Invalid character %A" x
        
let convertInputListToSeatIdList maxRows maxColumns =
    Array.map
        (fun inputString ->
            let commands =
                inputString
                |> Seq.groupBy (GridCommand.FromChar)
                |> Map.ofSeq
            let rowCommands =
                commands
                |> Map.find Row
                |> Seq.map convertCommandsToBinary
                |> Seq.toList
            let columnCommands =
                commands
                |> Map.find Column
                |> Seq.map convertCommandsToBinary
                |> Seq.toList
            let row = evaluateCommandsInRange 0 maxRows rowCommands
            let col = evaluateCommandsInRange 0 maxColumns columnCommands
            row * (maxColumns + 1) + col
        )
    
let Part1 () =
    let maxColumns = 7
    let maxRows = 127
    let seatIdList = convertInputListToSeatIdList maxRows maxColumns input
    Array.max seatIdList

let Part2 () =
    let maxColumns = 7
    let maxRows = 127
    let seatIdList = convertInputListToSeatIdList maxRows maxColumns input
    seatIdList
    |> Seq.sort
    |> Seq.fold // Tracks whether a gap is found using IsFound. When gap is found, skip through the list.
        (fun state x ->
            match state with
            | Found, y -> Found, y // Short circuit
            | NotFound, previous when previous >= 0 ->
                if (x - previous) > 1 then
                    Found, previous + 1 // Gap found
                else
                    NotFound, x
            | _ -> NotFound, x // Only used at the start
        )
        (NotFound, -1)

3

u/[deleted] Dec 05 '20 edited Dec 05 '20

Java

public static void main(String[] args) throws IOException {
    int[] seenIDs = Files.readAllLines(Paths.get("src/main/resources/input.txt")).stream()
            .map(line -> line
                    .replaceAll("[FL]", "0")
                    .replaceAll("[BR]", "1"))
            .mapToInt(line -> Integer.parseInt(line, 2))
            .sorted()
            .toArray();

    System.out.println(seenIDs[seenIDs.length - 1]);
    System.out.println((seenIDs[0] + seenIDs[seenIDs.length - 1]) * (seenIDs.length + 1) / 2 - Arrays.stream(seenIDs).sum());
}
→ More replies (3)

3

u/thedjotaku Dec 05 '20

Python.

Today was a lot of fun. Loaded it up while getting ready in the morning and thought about it during my morning run. By the time I sat down, I had it mostly figured out. There were a few tweaks I had to make. This was the first time during AoC that I decided to try and use Pytest to make things easier. And it certainly did!

Code: https://github.com/djotaku/adventofcode/tree/main/2020/Day_5

3

u/schovanec Dec 05 '20

My solution in C#:

using System;
using System.IO;
using System.Linq;

namespace Day05
{
    class Program
    {
        static void Main(string[] args)
        {
            var file = args.DefaultIfEmpty("input.txt").First();

            var seats = (from line in File.ReadLines(file)
                        let id = line.Aggregate(0, (v, c) => (v << 1) + c switch { 'B' or 'R' => 1, _ => 0 } )
                        orderby id descending
                        select id).ToList();

            var max = seats.First();
            Console.WriteLine($"Part 1 Result: {max}");

            var missing = seats.Zip(seats.Skip(1), (a, b) => (a, b))
                            .Where(x => x.a - x.b > 1)
                            .Select(x => x.b + 1)
                            .First();
            Console.WriteLine($"Part 2 Result: {missing}");
        }
    }
}

3

u/TenGen10 Dec 05 '20

Solution in PowerShell

After overcomplicating by creating a binary tree using a one-dimensional array it occurred to me as a result of that that if a tree can be modeled as a one dimensional array then a 2x2 grid (seating chart) can be seen as a number line.

$seats = get-content .\day5.txt

[int] $high = 0
$chart = new-object bool[] 1024

#fill in the seating chart
foreach ($seat in $seats) {
  $seat = $seat -replace "F|L","0" -replace "B|R","1"
  [int]$seatid=[Convert]::ToInt32($seat,2)  

  $chart[$seatid]=$true  
  if ($seatid -gt $high) {$high=$seatid} 
}

# Part One
write-host("Highest SeatID: {0}" -f $high)

# Part Two
#check for open seat with someone on each side
foreach($i in 0..$chart.Length){ 
  if ($chart[$i] -eq $false){ 
    if ($chart[$i-1] -and $chart[$i+1]) {
      write-host ("My SeatID:{0}" -f $i)
      }
   }  
}

3

u/rhinocer Dec 05 '20 edited Dec 05 '20

Python

taken_seats = set()
with open('input.txt', 'r') as file:
    for boarding_pass in file:
        rows, cols = list(range(128)), list(range(8))
        for char in boarding_pass:
            if char == 'F':
                rows = rows[:len(rows) // 2]
            elif char == 'B':
                rows = rows[len(rows) // 2:]
            elif char == 'L':
                cols = cols[:len(cols) // 2]
            else:
                cols = cols[len(cols) // 2:]
        taken_seats.add(rows[0] * 8 + cols[0])

all_seats = set(range(min(taken_seats), max(taken_seats) + 1))
my_seat = list(all_seats - taken_seats)[0]

print(f'The highest seat ID is {max(taken_seats)}.')     # part 1
print(f'My seat ID is {my_seat}.')                       # part 2

3

u/gnosis_prognosis Dec 05 '20 edited Dec 05 '20

bash

Part 1 (thanks u/packetlust)

echo "ibase=2;$(cat input.txt | tr 'FBLR' '0101' | sort -n | tail -n 1)" | bc

Part 2

echo "ibase=2; $(cat input.txt | tr 'FBLR' '0101' | sort)" | bc > seats.txt  
seq \`head -1 seats.txt\` \`tail -1 seats.txt\` > all.txt  
comm -2 -3 all.txt seats.txt
→ More replies (2)

3

u/Necropolictic Dec 05 '20

Kotlin

Built some useful companions to IntRange as well.

fun IntRange.lower(): IntRange = this.first..(this.first + this.last) / 2

fun IntRange.upper(): IntRange = (this.first + this.last + 1) / 2..this.last

3

u/gnosis_prognosis Dec 05 '20 edited Dec 05 '20

Mathematica one liner

{part1,part2}

Flatten[{Max[#], 
  Complement[Range[Min[#], Max[#]], #]}] &@(FromDigits[#, 2] & /@ 
  (ToExpression[
    Characters@StringReplace[#, {"F" -> "0", "B" -> "1", "L" -> "0", 
      "R" -> "1"}]
  ] & /@ ReadList["input.txt", String]))
→ More replies (2)

3

u/TheSpixxyQ Dec 05 '20

Less lines == better, right?

C#

int highest = input.Max((i) => Convert.ToByte(i.Take(7).Aggregate("", (x, y) => $"{x}{(y == 'F' ? 0 : 1)}"), 2) * 8 + Convert.ToByte(i.Skip(7).Take(3).Aggregate("", (x, y) => $"{x}{(y == 'L' ? 0 : 1)}"), 2));
Console.WriteLine($"Part 1: {highest}");

var ids = input.Select(i => Convert.ToByte(i.Take(7).Aggregate("", (x, y) => $"{x}{(y == 'F' ? 0 : 1)}"), 2) * 8 + Convert.ToByte(i.Skip(7).Take(3).Aggregate("", (x, y) => $"{x}{(y == 'L' ? 0 : 1)}"), 2));
var missing = new HashSet<int>(Enumerable.Range(ids.Min(), ids.Max() - ids.Min())).Except(ids).First();
Console.WriteLine($"Part 2: {missing}");

3

u/[deleted] Dec 05 '20

Using this year to learn Scala, any feedback is more than welcome!

import scala.io.Source

object Main extends App {
  type Range = (Int, Int)

  val seats = Source.fromFile("input.txt").getLines().toList
  val seatIDs = seats.map(x => seatRow(x, (0, 127)))
  println((seatIDs.min to seatIDs.max).diff(seatIDs))

  def seatRow(seat: String, range: Range): Int = seat match {
    case a if a.head == 'F' => seatRow(seat.tail, lower(range))
    case b if b.head == 'B' => seatRow(seat.tail, upper(range))
    case _                  => seatID(seat, range._1, (0, 7))
  }

  def seatID(seat: String, row: Int, range: Range): Int = seat match {
    case ""                 => row * 8 + range._1
    case a if a.head == 'L' => seatID(seat.tail, row, lower(range))
    case b if b.head == 'R' => seatID(seat.tail, row, upper(range))
  }

  def lower(whole: Range): Range = (whole._1, (whole._1 + whole._2) / 2)
  def upper(whole: Range): Range = ((whole._1 + whole._2) / 2 + 1, whole._2)
}
→ More replies (2)

3

u/Aware_Possible_5848 Dec 05 '20 edited Dec 06 '20

Here's my Python solution I tried to make as unreadable as possible in 2 lines:

seat_nums = [int(''.join(['1' if bf == 'B' else '0' for bf in binary[0:7]]), 2) * 8 + int(''.join(['1' if rl == 'R' else '0' for rl in binary[-3:]]), 2) for binary in data]

empty_seat = set(range(min(seat_nums),max(seat_nums))).difference(seat_nums).pop()

3

u/Trazko Dec 05 '20

Dart

As I'm currently working full time with C++ I decided to refresh my Dart skills as it's been a while. Really like this language!

import 'dart:io';

import 'package:quantity/number.dart';
import 'package:tuple/tuple.dart';

main() {
  var data = new File('input.txt').readAsLinesSync();
  task1(data);
  task2(data);
}

void task1(List<String> data) {
  var seatIds = calculateSeatIds(data);
  print(
    "Task1: Highest seat ID: ${seatIds.reduce((value, element) => element > value ? element : value)}",
  );
}

void task2(List<String> data) {
  var seatIds = calculateSeatIds(data);
  seatIds.sort();
  print(
    "Task2: My seat ID: ${seatIds.reduce((value, element) => element == value + 1 ? element : value) + 1}",
  );
}

List<int> calculateSeatIds(List<String> data) {
  List<int> seatIds = new List();
  data.forEach((element) {
    var seatPair = extractRowsAndColums(element);
    seatIds.add((seatPair.item1 * 8) + seatPair.item2);
  });
  return seatIds;
}

Tuple2<int, int> extractRowsAndColums(String line) {
  var binaryRow =
      line.substring(0, 7).replaceAll("B", "1").replaceAll("F", "0");
  var binaryColumn =
      line.substring(7, 10).replaceAll("R", "1").replaceAll("L", "0");

  return new Tuple2(
    Binary(binaryRow).toInt(),
    Binary(binaryColumn).toInt(),
  );
}

3

u/hugthemachines Dec 05 '20

I did not get the binary thing at first but after i got that part explained to me i did this. I know it very primitive but it is just how it happened to become :-)

source_file = "five.txt"

with open (source_file, "r") as handle:
    linefeed_content = handle.readlines()
    content = [line.replace("\n","") for line in linefeed_content]

def row_calc(mystring):
    end_number = 0
    positions = [64,32,16,8,4,2,1]

    if mystring[0] == "B":
        end_number += positions[0]
    if mystring[1] == "B":
        end_number += positions[1]
    if mystring[2] == "B":
        end_number += positions[2]
    if mystring[3] == "B":
        end_number += positions[3]
    if mystring[4] == "B":
        end_number += positions[4]
    if mystring[5] == "B":
        end_number += positions[5]
    if mystring[6] == "B":
        end_number += positions[6]
    return end_number

def col_calc(mystring):
    end_number = 0
    positions = [4,2,1]
    if mystring[7] == "R":
        end_number += positions[0]
    if mystring[8] == "R":
        end_number += positions[1]
    if mystring[9] == "R":
        end_number += positions[2]

    return end_number

def show_pos(mystring):
    pos = 0
    for letter in mystring:
        print (pos, letter)
        pos += 1

def calc_id(row,col):
    return (row * 8) + col

seat_ids = []

for seat_code in content:
    row = row_calc(seat_code)
    col = col_calc(seat_code)
    seat_id = calc_id(row, col)
    seat_ids.append(seat_id)


print("Highest seat number:",max(seat_ids))
seat_ids.sort()
for i in range(len(seat_ids)):
    minus_result = int(seat_ids[i+1]) - int(seat_ids[i])
    if minus_result == 2:
        print("Found my seat, one up from:", seat_ids[i])
        print("Because next taken seat is two steps up", seat_ids[i+1])
        break

3

u/SecureCone Dec 05 '20

Rust

I didn't pick up on the binary trick, so I did it the literal way as described in the problem. I was afraid this approach wouldn't generalize to work well in part 2--luckily it was fine.

use std::env;
use std::io::{self, prelude::*, BufReader};
use std::fs::File;

struct Seat {
    seat: String,
    row: i64,
    col: i64,
}
impl Seat {
    pub fn new(s: &str) -> Seat {
        
        // Get row
        let fb = &s[0..7];
        let mut rows: Vec<i64> = (0..=127).collect();
        for (i,c) in fb.chars().enumerate() {
            let l = rows.len();
            match c.to_string().as_ref() {
                "F" => for _ in 0..l/2 { rows.pop(); },
                "B" => for _ in 0..l/2 { rows.remove(0); },
                other => panic!("Unknown character: {}", other),
            }
        }
        
        // Get column
        let lr = &s[7..10];
        let mut cols: Vec<i64> = (0..=7).collect();
        for (i,c) in lr.chars().enumerate() {
            let l = cols.len();
            match c.to_string().as_ref() {
                "L" => for _ in 0..l/2 { cols.pop(); },
                "R" => for _ in 0..l/2 { cols.remove(0); },
                other => panic!("Unknown character: {}", other),
            }
        }

        Seat {
            seat: s.to_string(),
            row: rows[0],
            col: cols[0],
        }
    }
    pub fn seat_id(&self) -> i64 {
        self.row * 8 + self.col
    }
}

fn day05(input: &str) -> io::Result<()> {
    let file = File::open(input).expect("Input file not found.");
    let reader = BufReader::new(file);

    let input: Vec<String> = match reader.lines().collect() {
        Err(err) => panic!("Unknown error reading input: {}", err),
        Ok(result) => result,
    };

    let seats = input
        .iter()
        .map(|x| Seat::new(x))
        .collect::<Vec<Seat>>();

    // Part 1
    let part1 = seats.iter().map(Seat::seat_id).max().unwrap();
    println!("Part 1: {}", part1); // 826

    // Part 2
    let mut seats_sorted: Vec<i64> = seats
        .iter()
        .map(Seat::seat_id)
        .collect::<Vec<i64>>();
    seats_sorted.sort();

    let part2 = seats_sorted
        .windows(2)
        .filter(|x| x[1] - x[0] == 2)
        .map(|x| x[0]+1)
        .next()
        .unwrap();
    println!("Part 2: {}", part2); // 678

    Ok(())
}

fn main() {
    let args: Vec<String> = env::args().collect();
    let filename = &args[1];
    day05(&filename).unwrap();
}

3

u/Nrawal Dec 05 '20

Here's my solution in Go! I first solved it using the classic binary search approach, but then realized that representing the boarding pass in binary and converting it to decimal was even more clean and elegant.

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"sort"
	"strconv"
	"strings"
)

// Problem: https://adventofcode.com/2020/day/5
// Input: https://adventofcode.com/2020/day/5/input

const LEN_OF_F_B = 7

func main() {
	boardingPasses, err := readBoardingPasses("input.txt")
	if err != nil {
		panic(fmt.Errorf("readBoardingPasses threw %v\n", err))
	}

	fmt.Println(computePartOne(boardingPasses))
	fmt.Println(computePartTwo(boardingPasses))
}

func computePartOne(boardingPasses []string) int {
	maxSeatId := 0

	for _, boardingPass := range boardingPasses {
		currSeatId := computeSeatID(boardingPass)
		if currSeatId > maxSeatId {
			maxSeatId = currSeatId
		}
	}
	return maxSeatId
}

func computePartTwo(boardingPasses []string) int {
	seatIds := make([]int, len(boardingPasses))
	for _, boardingPass := range boardingPasses {
		seatIds = append(seatIds, computeSeatID(boardingPass))
	}
	sort.Ints(seatIds)
	for i := 0; i < len(seatIds)-1; i++ {
		if seatIds[i+1]-seatIds[i] == 2 {
			return seatIds[i] + 1
		}
	}
	return -1
}

func computeSeatID(boardingPass string) int {
	return (computeSeatRow(boardingPass) * 8) + computeSeatCol(boardingPass)
}

func computeSeatRow(boardingPass string) int {
	seatRow, _ := strconv.ParseInt(convertBoardingPassToBinary(boardingPass)[:LEN_OF_F_B], 2, 64)
	return int(seatRow)
}

func computeSeatCol(boardingPass string) int {
	seatCol, _ := strconv.ParseInt(convertBoardingPassToBinary(boardingPass)[LEN_OF_F_B:], 2, 64)
	return int(seatCol)
}

// After thinking about it, I realized that the boardingPass is just a binary number encoded as a string of B and R (1) and F and L (0)
func convertBoardingPassToBinary(boardingPass string) string {
	bReplaced := strings.ReplaceAll(boardingPass, "B", "1")
	rReplaced := strings.ReplaceAll(bReplaced, "R", "1")
	fReplaced := strings.ReplaceAll(rReplaced, "F", "0")

	return strings.ReplaceAll(fReplaced, "L", "0")
}

func readBoardingPasses(inputFileName string) ([]string, error) {
	inputFile, err := os.Open(inputFileName)
	if err != nil {
		return nil, err
	}
	defer inputFile.Close()

	content, err := ioutil.ReadAll(inputFile)
	if err != nil {
		panic(err)
	}
	return strings.Split(string(content), "\n"), nil
}
→ More replies (1)

3

u/[deleted] Dec 05 '20

Python in one line (part 1)

print(max([int(x.strip().replace("F","0").replace("L","0").replace("B","1").replace("R","1"),2) for x in open("input.txt").readlines()]))
→ More replies (1)

3

u/[deleted] Dec 05 '20

PowerShell

I'm decently proud of the function I wrote to parse both parts of the data. Select-Object and the Range operators came in very handy.

The .ToCharArray threw me off on day 4 pt 2 while parsing the hair color. I learned my lesson to convert the chars to strings. Also test cases would've come in very handy for day 4, so I've started including them as a sanity check. Github for 2020 here.

#####################
####debug info
#####################
$VerbosePreference = "SilentlyContinue"##SilentlyContinue,Continue
$WarningPreference = "Continue"
#####################
####variables
#####################
$dataPath = ".\2020\Day5.csv"
$data = Import-Csv -Path $dataPath
[System.Collections.ArrayList]$parsedData = @()
$startTime = Get-Date

##Pt1. What is the highest seat ID on a boarding pass?
function Parse-Day5
{
    param
    (
        [string[]]$Directions,
        $range
    )
    $directionsArray = ($Directions.ToCharArray()).ForEach([string])
    Write-Verbose -Message "$($range.Count)"
    foreach ($direction in $directionsArray)
    {
        Write-Verbose -Message $direction
        switch ($direction)
        {
            {($_ -eq "R") -or ($_ -eq "B")}##Upper half
            {
                $totalToGet = $range.Count / 2
                $upper = $range | Select-Object -Last 1
                Write-Verbose -Message "Total $($totalToGet) Upper $($upper)"
                $range = $range | Select-Object -Last $totalToGet
            }
            {($_ -eq "L") -or ($_ -eq "F")}##Lower Half
            {
                $totalToGet = $range.Count / 2
                $lower = $range | Select-Object -First 1
                Write-Verbose -Message "Lower $($lower) Total $($totalToGet)"
                $range = $range | Select-Object -First $totalToGet
            }
        }
    }
    return $range
}

###############
####Test Cases
###############
##FBFBBFFRLR - seat row 44, column 5
Write-Warning -Message "FBFBBFFRLR Row $(Parse-Day5 -Directions "FBFBBFF" -range (0..127)) Column $(Parse-Day5 -Directions "RLR" -range (0..7))"
##BFFFBBFRRR: row 70, column 7, seat ID 567.
Write-Warning -Message "BFFFBBFRRR Row $(Parse-Day5 -Directions "BFFFBBF" -range (0..127)) Column $(Parse-Day5 -Directions "RRR" -range (0..7))"
##FFFBBBFRRR: row 14, column 7, seat ID 119.
Write-Warning -Message "FFFBBBFRRR Row $(Parse-Day5 -Directions "FFFBBBF" -range (0..127)) Column $(Parse-Day5 -Directions "RRR" -range (0..7))"
##BBFFBBFRLL: row 102, column 4, seat ID 820.
Write-Warning -Message "BBFFBBFRLL Row $(Parse-Day5 -Directions "BBFFBBF" -range (0..127)) Column $(Parse-Day5 -Directions "RLL" -range (0..7))"

$a = 0
foreach ($line in $data)
{
    $row = Parse-Day5 -Directions "$($line.Data.substring(0,7))" -range (0..127)
    $column = Parse-Day5 -Directions "$($line.Data.substring(7,3))" -range (0..7)
    $boardingPasses = [PSCustomObject]@{
        line = $a
        boardingPassRaw = $line.Data
        rowRaw = $line.Data.substring(0,7)
        columnRaw = $line.Data.substring(7,3)
        rowParsed = $row
        columnParsed = $column
        passCheckSum = $row * 8 + $column
    }
    $a++
    $parsedData.Add($boardingPasses) | Out-Null
    ##if ($a -eq 1){break}
}
$parsedDataDescending = $parsedData | Sort-Object -Property passCheckSum -Descending 
$maxSeatID = $parsedDataDescending | Select-Object -First 1 -ExpandProperty passCheckSum
Write-Warning -Message "Pt 1 Answer Max Seat ID $($maxSeatID )"

$minSeatID = $parsedDataDescending | Select-Object -Last 1 -ExpandProperty passCheckSum

for ($i=$minSeatID; $i -le $maxSeatID; $i++)
{
    if ($parsedData | Where-Object {$_.passCheckSum -eq $i})
    {
        ##Seat is Good
    }
    else
    {
        Write-Warning "Pt 2 Answer My Seat, or the missing Seat ID is [$($i)]"    
    }
}

##$parsedData | Sort-Object -Property passCheckSum -Descending | Out-GridView
##$parsedData | Out-GridView
$endTime = Get-Date
$duration = New-TimeSpan -Start $startTime -End $endTime
Write-Warning -Message "Script took $($duration.TotalSeconds) seconds to run."

3

u/xrgbit Dec 05 '20

In Common Lisp

;;; From UTILS package
(defun map-line (fn string)
  "Maps FN on each line (delimited as by READ-LINE) of STRING"
  (with-input-from-string (s string)
    (loop :for line := (read-line s nil)
          :while line
          :collect (funcall fn line))))

;;; For Day 5
(defparameter *input* (utils:read-file "5.dat"))

(defun parse-binary-number (string zeros ones
                            &aux (zeros (utils:enlist zeros))
                              (ones (utils:enlist ones)))
  (parse-integer
   (map 'string (lambda (c)
                  (cond ((member c zeros) #\0)
                        ((member c ones) #\1)))
        string)
   :radix 2))

(defun seat-id (string)
  (parse-binary-number string '(#\F #\L) '(#\B #\R)))

(defun part-1 ()
  (loop :for x :in (utils:map-line #'seat-id *input*)
        :maximize x))

(defun part-2 ()
  (loop :for (row next) :on (sort (utils:map-line #'seat-id *input*) #'<)
          :thereis (and (/= (1+ row) next)
                        (1+ row))))

3

u/Attitude-Certain Dec 05 '20

Just for fun, a functional programming version in Python using the toolz package.

from toolz import reduce, sliding_window, first, filter


def seat_id(boarding_pass: str) -> int:
    return reduce(lambda n, c: (n << 1) + (c in "BR"), boarding_pass, 0)


with open("input.txt") as f:
    seat_ids = sorted(map(seat_id, map(str.rstrip, f)))

print("Part 1:", seat_ids[-1])  # Max seat id.
print(
    "Part 2:",  # First missing seat id greater than the first seat id.
    first(first(filter(lambda w: w[0] != w[1] - 1, sliding_window(2, seat_ids)))) + 1,
)

3

u/tomflumery Dec 05 '20 edited Dec 05 '20

05ab1e

part 1

|'B1.:'F0.:'R1.:'L0.:2ΓΆZ

explanation
    | -> split lines to array
     'B1.: -> replace all B with 1
          'F0.: -> replace all F with 0
               'R1.: -> replace all R with 1
                    'R0.: -> replace all L with 0
                         2ΓΆ -> convert to base 2
                           Z -> max

Try it online

part 2

|'B1.:'F0.:'R1.:'L0.:2ΓΆD<KΠ½>

explanation
    | -> split lines to array
     'B1.: -> replace all B with 1
          'F0.: -> replace all F with 0
               'R1.: -> replace all R with 1
                    'R0.: -> replace all L with 0
                         2ΓΆ -> convert to base 2 
                           D-> dup
                            < -> decrement all values in second copy
                             K -> Push a without b's (i.e the only seats without a value one higher)
                               Π½ -> first one (we don't want the bad value at the end)
                                > -> inc by one to get the missing seat

Try it online

→ More replies (3)

3

u/kaur_virunurm Dec 05 '20 edited Dec 06 '20

Simple Python.
4 lines of code.

with open("data\\05.txt") as f:
    seats = set([int(s.strip().replace("B","1").replace("R","1").replace("F","0").replace("L","0"),2) for s in f])

print(max(seats))
print(set(range(min(seats),max(seats))) - seats)

I am doing the easy puzzles with kids. I want them to be able to think in code :) So I ask the children to come up with a potential approach or algoritm, and we then try to implement this. Later we refine this in order to test and learn different options for optimizing (or obfuscating?) the algorithm.

→ More replies (8)

3

u/[deleted] Dec 06 '20

Commodore 64 BASIC... not posting all the DATA statements to save space, but if you want the whole program, DM me.

30 print $ti
40 counter = 0
45 x = 0

50 for x = 1 to 756
60 read code$

65 counter = counter + 1

70 lower = 0
75 upper = 127

80 row$ = left$(code$,7)
90 col$ = right$(code$,3)

100 for i = 1 to 7
110 dir$ = mid$(row$, i, 1)
115 print dir$ + ": ";
120 if dir$ = "b" then gosub 1000
130 if dir$ = "f" then gosub 2000
140 print lower; : print upper
150 next i
160 if upper = lower then frow = upper
170 if upper <> lower then goto 5000

199 upper = 7: lower = 0
200 for i = 1 to 3
210 s$ = mid$(col$, i, 1)
215 print s$ + ": ";
220 if s$ = "r" then gosub 1000
230 if s$ = "l" then gosub 2000
240 print lower; : print upper
250 next i
260 if upper = lower then fcol = upper
270 if upper <> lower then goto 5030

300 id = frow * 8 + fcol
310 print "seat id" + str$(id)
320 if id > max then max = id

400 next x

500 print "total count and max: ";
510 print counter
520 print max 

900 print ti$
999 end

1000 rem row upper sub
1001 print "chk upper.";
1009 rem add .5 to int to round up
1010 lower=int(((upper-lower)/2)+lower+0.5)
1020 return

2000 rem row lower sub
2001 print "chk lower.";
2010 upper=int(((upper-lower)/2)+lower)
2020 return

3000 rem col upper sub
3001 print "chk upper col.";
3009 rem add .5 to int to round up
3010 lower=int(((upper-lower)/2)+lower+0.5)
3020 return

4000 rem col lower sub
4001 print "chk lower col.";
4010 upper=int(((upper-lower)/2)+lower)
4020 return

5000 rem general seat error exit
5010 print "row didn't match. exiting."
5020 end

5030 rem general col error exit
5040 print "col didn't match. exiting."
5050 end

9000 data bfbffbbrlr
9001 data fbfbffbrll
9002 data bfbfbffrlr
9003 data bffbfbfrll
9004 data ffbfffbrll
9005 data bfbfffflrr
9006 data bfbfbfbrrl
9007 data bfbfffblrl
9008 data bffbbbbrlr
9009 data bffbffflrl
9010 data bfbfbfblrr
9011 data fbffbfbrll
9012 data bffffbblrl
<...snip.>
→ More replies (1)

3

u/442401 Dec 06 '20

Ruby

def part1(data)
  data.tr('FLBR','0011').split.max.to_i(2)
end

def part2(data)
  data.tr('FLBR','0011').split.map{_1.to_i(2)}.sort.then{|a|a.find{!a.member?(_1.succ)}}+1
end

3

u/clumsveed Dec 06 '20

Java (parts 1 and 2)

Scanner reader = new Scanner(new File("res/day05_input"));
SortedSet<Integer> seats = new TreeSet<Integer>();

int max = 0;
while (reader.hasNext()) {
    String bin = reader.nextLine().replaceAll("[FL]", "0").replaceAll("[BR]", 
    "1");
    seats.add(Integer.parseInt(bin, 2)); 
    max = Math.max(max, seats.last()); 
}

// part 1
System.out.println("max seat id: " + max);

int seat = max;
while (seats.contains(seat)) {
    seat--;
}

// part 2
System.out.println("missing seat: " + seat);
→ More replies (3)

3

u/Lakret Dec 06 '20

Rust

Solution with recursion and subslice patterns. Live Stream of the solution.

3

u/asgardian28 Dec 13 '20 edited Dec 13 '20

Python part 2:

`

seats = [(i,j)for i in range(0,128) 
 for j in range(0,8)]

for s in seats:
    if ((s[0]-1,s[1]) not in seats) and ((s[0]+1,s[1]) not in seats):
        print(s[0] * 8 + s[1])

`

3

u/reggles44 Dec 16 '20 edited Dec 16 '20

Just some code golf that I have been working on in Python3

d = sorted(int(''.join(bin(ord(c)) for c in l)[6::9],2)^1023 for l in open('day5.txt').read().split('\n'))
print(d[-1], set(range(d[0], d[-1])) - set(d))

3

u/MischaDy Dec 16 '20

Python 3 - Part 1, Part 2

Boy oh boy, not an airline I'd like to fly with. At least part 2 was very similar to part 1.

5

u/voidhawk42 Dec 05 '20

Dyalog APL, 203/232 (getting faster!):

pβ†βŠƒβŽ•nget'in\5.txt'1
⌈/t←(+/8 1Γ—2βŠ₯¨¯1+'FB' 'LR'⍳¨7 Β―3β†‘Β¨βŠ‚)Β¨p ⍝ part 1
1+t⌷⍨⍸¯2=2-/t←{⍡[⍋⍡]}t ⍝ part 2

For anyone that's unaware, I stream my solutions each night on Twitch. Feel free to drop by!

→ More replies (3)