Safe HaskellNone

Day13

Description

General - This is a reading problem. Reading it fast once, you start to think about lazy list evaluations. Reading it a second time slow(er), you realize that there is an easy way to calculate the solution, because the minutes you need to wait is the remainder of the departure time by the bus id.

Part 1 - Find the minimum and calculate the solution.

Part 2 - We have a system of equations along the lines of ...

mod x b0 = 0 mod x+1 b1 = 0 ... mod x+n bn = 0

We are looking for the smallest x to make this system work.

The first idea would be to try all numbers until all equations work, but ... this will take a long time. An optimization would be to only try the numbers that we get from the first equation.

The next optimisation is to pick the equation that is most selective (the max of all bus ids).

But (not too surprsingly) this works for the testcases, but not to solve the problem.

Next step is to realize that the system above can be solved using the Chinese Remainder Algorithm. But for that we need to restructure the system to ...

mod x b0 = b0-0 mod x b1 = b1-1 ... mod x bn = bn-n

(Conveniently) The input for the chineseRemainder is the list of [(b0, b0-0), (b1, b1-1), ...] pairs.

But then you realize that you found a solution for the system, but not the smallest one. You can get the smallest solution by caluculating the mod of the chineseRemainder and the product of all moduli.

That's it.

Synopsis

Documentation

data Schedule #

The schedule (with all the busses).

Constructors

Schedule 

Fields

input :: String -> Schedule #

Read the input file.

part1 :: Schedule -> Int #

Solve part1.

part2' :: Schedule -> Integer #

Solve part2 (using an algorithm).

part2 :: Schedule -> Integer #

Solve part2 (using a calculation).