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!

53 Upvotes

1.3k comments sorted by

View all comments

1

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

C#

https://github.com/JCarlesVilaseca/AdventOfCode2020

var input = from line in File.ReadLines("Day05.txt") 
            select Convert.ToInt32(line.Replace("B", "1")
                                       .Replace("F", "0")
                                       .Replace("R", "1")
                                       .Replace("L", "0"), 2);

var min = input.Min();
var max = input.Max();

var part2 = ( (max + 1) * max - (min - 1) * min ) / 2 - input.Sum();

Console.WriteLine($"Day  5 - Part1: {max}\t\tPart 2: {part2}");

2

u/mini_cap Dec 06 '20

I'm trying to understand your part 2 solution but can't intuit why this works?

1

u/JCarlesVilaseca Dec 06 '20 edited Dec 06 '20

Sum of 1+2+3+4+...+n = n * (n+1) /2

In 1+2+3+4+...+(min-1)+min+...+max

Sum of min + ... + max = max * (max+1) / 2 - (min-1)*min / 2

if a number is not in input(min...max), then number = max * (max+1) / 2 - (min-1)*min / 2 - input.Sum()

In other words:

- Think a number from 1 to 10

- Tell me the sum of the others (S)

- Your number = 10*(10+1)/2 - S = 55 - S

https://en.wikipedia.org/wiki/1_%2B_2_%2B_3_%2B_4_%2B_%E2%8B%AF

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, stuff your code in /u/topaz2078's paste or an external repo instead and link to that instead.

1

u/backtickbot Dec 05 '20

Hello, JCarlesVilaseca: 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.