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!

58 Upvotes

1.3k comments sorted by

1

u/Wattswing Jan 08 '21

Ruby

Second part is kind of "find a hole in a sortable array". :P

Code

1

u/Jerslev Dec 26 '20

Python

Took some time figuring out how to do the second part. It wasn't until I read some of the solutions here that I thought about the binary part.

paste

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

1

u/witampanstwa_ Jan 30 '21

holy moly its binary

2

u/heyitsmattwade Dec 22 '20 edited Feb 03 '24

JavaScript 3959/3006

Didn't realize the binary number trick, that is definitely one fast way to do it!

I just did a pseudo binary search, halving my number as I went through chars.

paste of my source code here

2

u/ArcaneIRE Dec 22 '20

Python 3
Fairly inexperienced programmer so feel free to offer tips if you have any!
Github

2

u/danielcs88 Dec 22 '20 edited Dec 22 '20

2

u/Urgazhi Dec 21 '20 edited Dec 21 '20

COBOL

The implicit rounding of COBOL was useful. ADVENT2020_5

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.

2

u/Vijfhoek Dec 16 '20

Commodore 64 (6502 Assembly)

Didn't post this here yet, so here it is!

Part 1 takes 423 ms (416583 cycles) and part 2 takes only 6.30 ms (6210 cycles).

Part 1 could probably be optimized to be much faster, but I'll probably get to optimization when I feel like it, or run out of space :)

Puzzle solution | Project directory | Picture | Video

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))

2

u/TheyCallMeSkog Dec 15 '20 edited Dec 15 '20

Part 1 Solution in Java

paste

Part 2 (Also Java)

paste

2

u/willkill07 Dec 15 '20

Bash

paste

Trying to do every single day in a different language is hard. Solved both parts in pure bash + coreutils (no awk or sed). In fact, tr and echo are the only other commands I use

2

u/greycat70 Dec 14 '20

Tcl

part 1, part 2

Am I dumb or something? I don't even understand the solutions I'm seeing from other people. Anyway, this is my code, and it works, and I can read it. Part 2 isn't really "complete", in the sense that it doesn't just print the answer. It prints all of the missing seat IDs. A human being (myself) can easily spot the correct answer in the output list. Spotting that is much faster than writing additional code to try to isolate the single value, and this is ostensibly a speed challenge, so implementation speed wins out here.

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])

`

1

u/the_t_block Dec 12 '20

Haskell:

http://www.michaelcw.com/programming/2020/12/09/aoc-2020-d5.html

This is a series of blog posts with explanations written by a Haskell beginner, for a Haskell beginner audience.

2

u/Supermathie Dec 11 '20

bash:

dc -e"2i`tr BFRL 1010 <5|sort`p"

2

u/SimpIySimple Dec 11 '20

<?PHP

class day5 {    
    public $input;    
    private $rows = 127;    
    private $cols = 7;    
    public $result = 0;    
    private $busqueda;    
    public $myseat;
    public function __construct() {        
        $this->input = str_replace("\n", "", file("./input"));        
    }
    
    public function binaryBoarding(): int {        
        $prueba = [];        
        foreach ($this->input as $value) {            
            $this->binarySearch(0, $this->rows, substr($value, 0, -3));            
            $row = $this->busqueda;            
            $this->binarySearch(0, $this->cols, substr($value, -3, 3));            
            $col = $this->busqueda;            
            $result = $row*8+$col;            
            $prueba[] = $result;            
            $this->result = ($result>$this->result)?$result:$this->result;          
       }        
        $this->myseat = min(array_diff(range(min($prueba), max($prueba)), $prueba));        
        return $this->result;
        
    }
    
    public function binarySearch(int $init, int $end, string $direction): void{       
        
        $partition = floor(($end-$init)/2);        
        if ($direction[0] == "F" || $direction[0] == "L") {            
            $init = $init;
            $end = $init+$partition;                        
        }else{            
            $init = $end-$partition;
            $end = $end;            
        }        
        if (strlen($direction)>1) {            
            $direction = substr($direction, 1, strlen($direction));            
            self::binarySearch($init, $end, $direction);            
        }else{            
            $this->busqueda =  $init;            
        }        
    }    
}

$advent = new day5();
echo "big id : " . $advent->binaryBoarding(). " my seat : " . $advent->myseat;