Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit f68660b

Browse files
authored
Merge pull request #44 from WeiiswurstDev/master
Days 5,7,8 and 9 in typescript
2 parents ff9d716 + 7835d4e commit f68660b

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import fs from 'fs'
2+
import {EOL} from 'os'
3+
4+
const input = fs.readFileSync("input.txt").toString().split(EOL).sort()
5+
console.log(input.length)
6+
7+
function toBin(x:string,binaryOne:string) {
8+
let current = 0;
9+
for(let i = 0; i < x.length; i++)
10+
current += (x[i]==binaryOne?1:0)*(Math.pow(2,x.length-i) / 2)
11+
return current;
12+
}
13+
14+
15+
let maxSeatId = 0;
16+
let lastCol = 0;
17+
let ignoreRow = -1;
18+
for(let line of input) {
19+
let row = toBin(line.substring(0,7),"B")
20+
let col = toBin(line.substring(7,line.length),"R")
21+
//console.log(line,line.substring(0,7),row,line.substring(7,line.length),col)
22+
let seatId = row*8+col;
23+
if(seatId > maxSeatId) maxSeatId = seatId;
24+
25+
if(col-lastCol == 0) ignoreRow = row-1;
26+
else if(row==ignoreRow) ignoreRow = -1;
27+
// My poor attempt at part 2, I fiddled with this if statement until it spat out numbers in "Part 2", then tried the sensible numbers.
28+
// Do not repeat.
29+
//else if(col-lastCol != 1 && col-lastCol != -7 && row > 2) console.log("Part 2",seatId)
30+
lastCol = col;
31+
}
32+
33+
console.log("Part 1",maxSeatId)
34+
console.log("No solution for part 2. Please use another solution.")
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import fs from 'fs'
2+
import {EOL} from 'os'
3+
4+
const input = fs.readFileSync("input.txt").toString().split(EOL)
5+
6+
type BagContainList = {
7+
color:string,
8+
amount:number
9+
}[];
10+
11+
type BagList = {
12+
[key: string]: BagContainList
13+
}
14+
15+
const bags: BagList = {}
16+
17+
for(let line of input) {
18+
const split = line.split(" ");
19+
const contains:BagContainList = [];
20+
if(split[4] != "no") for(let i = 4; i < split.length; i+=4)
21+
contains.push({
22+
color:split[i+1]+" "+split[i+2],
23+
amount:Number.parseInt(split[i])
24+
});
25+
bags[split[0]+" "+split[1]] = contains;
26+
}
27+
28+
let part1total = 0;
29+
30+
for(let bag in bags) {
31+
if(checkBag(bags[bag])) part1total++;
32+
}
33+
34+
function checkBag(containList:BagContainList):boolean {
35+
let hasGoldenBag = false;
36+
for(let contains of containList) {
37+
if(contains.color == "shiny gold") return true;
38+
else if(checkBag(bags[contains.color])) {
39+
hasGoldenBag = true;
40+
break;
41+
}
42+
}
43+
return hasGoldenBag;
44+
}
45+
46+
function countBagsInside(bagList:BagContainList):number {
47+
let count = 1;
48+
for(let bag of bagList) {
49+
count += countBagsInside(bags[bag.color])*bag.amount;
50+
}
51+
return count;
52+
}
53+
54+
console.log("Part 1",part1total)
55+
console.log("Part 2",countBagsInside(bags["shiny gold"])-1)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import fs from 'fs'
2+
import {EOL} from 'os'
3+
4+
const input = fs.readFileSync("input.txt").toString().split(EOL)
5+
6+
const linesVisited = new Set<number>();
7+
8+
let part = 1;
9+
10+
function exec(acc:number,line:number):any {
11+
if(line >= input.length) return false;
12+
if(linesVisited.has(line)) {
13+
if(part == 1) console.log("Part 1",acc)
14+
return true;
15+
}
16+
linesVisited.add(line)
17+
const split = input[line].split(" ");
18+
const exp = split[0]
19+
if(exp=="nop") {
20+
return exec(acc,line+1)
21+
}
22+
else if(exp=="jmp") return exec(acc,line+parseNumber(split[1]))
23+
else if(exp=="acc") {
24+
acc+=parseNumber(split[1])
25+
return exec(acc,line+1)
26+
}
27+
}
28+
29+
function parseNumber(number:string):number {
30+
if(number[0]=="+") return Number.parseInt(number.substring(1,number.length))
31+
else return Number.parseInt(number)
32+
}
33+
34+
exec(0,0)
35+
36+
part = 2;
37+
38+
console.log("No solution for Part 2 (yet). I might add one when I find time for it.")
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {readFileSync} from 'fs'
2+
import {EOL} from 'os'
3+
4+
const input = readFileSync("input.txt").toString().split(EOL).map(line=>Number.parseInt(line))
5+
6+
const last25:number[] = []
7+
let counter = 0;
8+
const sums:number[] = [];
9+
10+
let part1 = 0;
11+
12+
for(let num of input) {
13+
if(last25.length != 25) {
14+
last25.push(num)
15+
if(last25.length == 25)
16+
for(let x of last25)
17+
for(let y of last25)
18+
sums.push(x+y)
19+
} else {
20+
if(!sums.includes(num)) {
21+
part1 = num;
22+
break;
23+
}
24+
last25[counter] = num;
25+
for(let i = 0; i < 25; i++) {
26+
sums[i+counter*25] = num+last25[i];
27+
}
28+
counter++;
29+
if(counter==25) counter=0;
30+
}
31+
}
32+
33+
console.log("Part 1",part1)
34+
35+
for(let i = 0; i < input.length; i++) {
36+
let sum = input[i];
37+
let j = i;
38+
let smallest = Number.MAX_VALUE;
39+
let largest = Number.MIN_VALUE;
40+
while(sum < part1) {
41+
j++
42+
sum += input[j]
43+
smallest = Math.min(smallest,input[j])
44+
largest = Math.max(largest,input[j])
45+
}
46+
if(sum == part1) {
47+
console.log("Part 2",smallest+largest)
48+
break;
49+
}
50+
}

0 commit comments

Comments
 (0)