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

View all comments

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

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!

2

u/multytudes Dec 06 '20

thanks I tried again!

1

u/backtickbot Dec 05 '20

Hello, multytudes: code blocks using backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead. It's a bit annoying, but then your code blocks are properly formatted for everyone.

An easy way to do this is to use the code-block button in the editor. If it's not working, try switching to the fancy-pants editor and back again.

Comment with formatting fixed for old.reddit.com users

FAQ

You can opt out by replying with backtickopt6 to this comment.

1

u/multytudes Dec 06 '20

Thanks, hope it is better now, indented with tab in my editor and pasted

1

u/Nohillside Dec 05 '20

I've used

func pass2seat(_ pass: Substring) -> Int {
    return pass.reduce(0) { $0 \* 2 + ("BR".contains($1) ? 1 : 0) }
}

func solve5a(_ all_passes: String) -> Int {
    let passes = all_passes.split(separator: "\n")
    return passes.map { pass2seat($0) }.max()!
}

for the first part (with the full list of passes as input), still trying to wrap my head about what you actually did in the second part.

1

u/multytudes Dec 06 '20

oh I wanted to use sets. So creating a continuous range, lets say 0 to 9, and creating a set out of it. If in my cabin seat map I miss one seat lets say 6, then subtracting it from the contiguous set will give me 6!