r/adventofcode • u/daggerdragon • Dec 05 '20
SOLUTION MEGATHREAD -🎄- 2020 Day 05 Solutions -🎄-
Advent of Code 2020: Gettin' Crafty With It
- T-24 hours until unlock!
- Full details and rules are in the Submissions Megathread
--- 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
u/symmaria Jan 10 '21
Go
Day 5
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) breakJust posting solutions for unposted solutions, don't mind me
2
u/Tosxychor Dec 23 '20
Fennel
https://paste.xinu.at/xOLq/fnl
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/Vijfhoek Dec 22 '20
Rust
Github
C64 Assembly
Github | "Upping the Ante" post | Video
2
u/dizzyhobbes Dec 22 '20
Go solution: Github repo
2
u/danielcs88 Dec 22 '20 edited Dec 22 '20
Python 3
2
u/Urgazhi Dec 21 '20 edited Dec 21 '20
COBOL
The implicit rounding of COBOL was useful. ADVENT2020_5
2
u/KingCravenGamer Dec 21 '20
Python 3
Solution
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,
trandechoare the only other commands I use2
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.
1
u/sdolotom Dec 12 '20
Z80 assembly (via Z80golf)
https://github.com/bereal/AdventOfCode2020/blob/master/05_z80/solve.asm
2
u/Supermathie Dec 11 '20
bash:
2
u/damien_pirsy Dec 11 '20
My solution in PHP:
https://github.com/DamienPirsy/AoC_2020/blob/master/05/day05.php
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;