2020 day 13

This commit is contained in:
tristan 2020-12-13 14:53:10 +00:00
parent 7f73e72ec2
commit b46bf4115d
9 changed files with 53 additions and 12 deletions

2
2020/13/input.txt Normal file
View file

@ -0,0 +1,2 @@
1001796
37,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,457,x,x,x,x,x,x,x,x,x,x,x,x,13,17,x,x,x,x,x,x,x,x,23,x,x,x,x,x,29,x,431,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,19

21
2020/13/part1.js Normal file
View file

@ -0,0 +1,21 @@
// this was fairly simple; just keep increasing the wait until you find a bus that leaves. there is probably a more efficient solution but this works
const fs = require('fs');
const input = fs.readFileSync('input.txt', 'utf-8').split('\n');
const time = Number(input[0]);
const busses = input[1].split(',').filter(v=>v!='x').map(n => Number(n));
let isBus = false;
let timeDif = 0;
let busID;
for (; !isBus; timeDif++) {
for(bus of busses) {
if ((time + timeDif) % bus == 0){
busID = bus;
isBus = true;
break;
}
}
}
// the for loop does an extra increment at the end oops
timeDif--
console.log(timeDif*busID)

19
2020/13/part2.js Normal file
View file

@ -0,0 +1,19 @@
// this was a tough one. I still don't fully understand what the puzzle was asking for. but i played around in excel and found this method to find the solution.
const fs = require('fs');
const input = fs.readFileSync('input.txt', 'utf-8').split('\n');
const busses = input[1].split(',').map(n => Number(n)).map((v,i)=>[v,i]).filter(v => !isNaN(v[0]));
console.log(busses)
// start with a timestamp of 0
let time = 0;
// if you incrment by the ID of the first bus, the first bus will always meet the pattern described in the puzzle.
let inc = busses[0][0]
for (let bus = 1; bus < busses.length; bus++){
// continue incrementing until the next bus meets the pattern described in the puzzle
while (((time+busses[bus][1]) % busses[bus][0]) != 0) {
time += inc
}
// now you can increment by the new bus that met the pattern, and it will continue to meet the pattern
inc*=busses[bus][0]
}
// since we looped through all the busses, we know that all busses meet the pattern.
console.log(time)

2
2020/13/test.txt Normal file
View file

@ -0,0 +1,2 @@
939
7,13,x,x,59,x,31,19

2
2020/13/test2.txt Normal file
View file

@ -0,0 +1,2 @@
67,7,59,61

2
2020/13/test3.txt Normal file
View file

@ -0,0 +1,2 @@
67,x,7,59,61

2
2020/13/test4.txt Normal file
View file

@ -0,0 +1,2 @@
67,7,x,59,61

2
2020/13/test5.txt Normal file
View file

@ -0,0 +1,2 @@
1789,37,47,1889

View file

@ -53,15 +53,4 @@ if (advent) {
10. [:star: :star:](https://adventofcode.com/2020/day/10 "see puzzle") 10. [:star: :star:](https://adventofcode.com/2020/day/10 "see puzzle")
11. [:star: :star:](https://adventofcode.com/2020/day/11 "see puzzle") 11. [:star: :star:](https://adventofcode.com/2020/day/11 "see puzzle")
12. [:star: :star:](https://adventofcode.com/2020/day/12 "see puzzle") 12. [:star: :star:](https://adventofcode.com/2020/day/12 "see puzzle")
13. [:star: :star:](https://adventofcode.com/2020/day/13 "see puzzle")
## Languages Used
* Python
- py
* Node JS
- js
* Java (Processing)
- pde
* HTML, CSS, JavaScript
- html
- css
- js