2020 day 11

This commit is contained in:
tristan 2020-12-11 16:13:10 +00:00
parent bb6d22cd13
commit 29b64b4715
5 changed files with 230 additions and 0 deletions

93
2020/11/input.txt Normal file
View file

@ -0,0 +1,93 @@
LLLLLL.LL.LL.LLLLLL.LLL.L.LLLLLLLLLLLLLLLL.LLLLLLLLLLL.LLLL.LLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLLL
LLLLLLLLLLLL.LL.LLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLL
LLLLLL.LLLLL.LLLL.L.LLLLLLLLLLLLL.LLLLL.LL.LLL.LL.LLLL.LLLLLLLLLLLL.LLLLLLLL.LLLL.L.LLLLLLLLLLLLL
LLLLLL.LLLLLLLLLLLL.LLLLL..LLLLLL.LLLLLLLL.LLLLLLLLLLL.LLLLLLLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLLLLLLL
LLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLL.L.LL.LLLL.LLLLLLL.LLLLLLLL.LLLLLL.LLL.LL.LLLL..
L....L...L...LL..LLL.....L..L..L.L.L..LL..LL......L.L.L..L...L.....LL.......L.L.L..L....L...L....
LLLLLL.LLLLL.LLL.LL..LLLL.LLLLLLL.LLLLLLLL.LLLLLL.LLLL.LLLL.LLLLLLL.LL.LLLLL.LLLLLLLLLLLLL.L..LLL
LLLLLL.LLLLLLLLLLLLLL.L.L.LLLLLLL.LLLLLLLL.LLLLLLLLLLL.LLLL.LLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLL
LLLLLL.LLLLL.LLLLLL.L.LLL.LLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLL.L.LLLLLL
LLLLLL.LLLLL.LLL.LLLLLLLL.LLLLLLL.LLLLLLLL.LLLLLL.LLLL.LLLL.LLLLLLLLLLLLLL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...L..LLL...L...L.......LL.......LL.L.L.....LLL....L.L......L.L...L...L.L.L.....L.LL........L
LLLLLL.LLLLLLLLLLLL.LLLLL.LLLLLLL..LLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLL.L.LLLL
LLLLLL.LLLLLLLLLLLL.LLLLL.LLLLLLL.LLLLLLLLLLLLLLL.LLLL.LLLL.LLLLLLL.LLLLLLLL.LLLLLL.LLLLLL.LLLLLL
LLLLLL.LLLLL.LLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLLL
LLLLLL.LLLLL.LLLLLL.LLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLLL..L.L.LLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLL
..LLL..L.......LL.L..........L...L....LL..L.L......LLLLLLL..LLL...L.L.LL..LL...L......L...L.L...L
LLLLL..LLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLL.LLLLLL.LLLL.LLL.LLLL.LLL.LLLLLLLLLLLLLLLLLLLLLL.LLLLLL
LLLLLL.LLLLL.LLLLLLLLLLLLLLLLL..L.LLLLLLLL.LLLLLL..LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLL
.LLLLL.LLLLL.LL.L.L.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLL..LLLLLLLLLLLL.LLLLLLLL.LLL.L.L.LLLLLLLLLLL
LLLLLL.LLLLLLLLLLLL.LLLLL.LLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLL.LL.LLLLLLLLLLLLLLL.LLLLLL.LLLLLL
..L.L...L..L.....L...L..L...L.........LL......LL..L..L.L...L....LL.L....L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LLLL.LLLLLLLLLLLLL.LLLLLL
LL.LL.LLLLLL.LLLLLL.LLLLL.LLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLL.LLLL.LLLLLL.LLLLLL
L.LLLL.LLLLL.LLLLLLLLLLLL.LLLLLLL.L.LLLLLLLLLLLLLLLLLLLLLLLLLLLL.LL.LLLLLLLLLLLLLLLLLLLLLL.LLLLL.
LLLLLL.LLLLLLLLLLLLLLL.LL.LLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLL.LLLLLLL.LLLLLLLL.LLLLLL.LLLLLL
LLLLL..LLLLLLLLLLLL.LLLLL.LLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLL.LLL.LL
LLLLLL.LLLLLL.LLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLL.LLLLLLLLLLLLL.LLLLLL

55
2020/11/part1.js Normal file
View file

@ -0,0 +1,55 @@
// looks like today's puzzle is the game of life but with holes in the grid
// never programmed the game of life before but I am familiar
const fs = require('fs');
const data = fs.readFileSync('input.txt', 'utf-8');
// convert seat layout to matrix of bools with padding left and right
let layoutMat = data.split('\n').map(l=>('.'+l+'.').split('').map(s=>s=='L'));
// create padding on top and bottom
layoutMat.unshift(new Array(layoutMat[0].length).fill(false));
layoutMat.push(new Array(layoutMat[0].length).fill(false));
function iterate (stateMat, layoutMat){
let nextState = stateMat.map(r => r.slice());
for (let y = 1; y < layoutMat.length - 1; y++) {
for (let x = 1; x < layoutMat[1].length - 1; x++) {
if (!layoutMat[y][x]) continue // if this point is the floor, skip
// count how many adjacent seats are full
let adjSeatsFull = stateMat[y-1][x-1] + stateMat[y-1][x ] + stateMat[y-1][x+1] +
stateMat[y ][x-1] + stateMat[y ][x+1] +
stateMat[y+1][x-1] + stateMat[y+1][x ] + stateMat[y+1][x+1];
// update seat according to rules in the challenge
nextState[y][x] = stateMat[y][x] ? adjSeatsFull < 4 : adjSeatsFull == 0;
}
}
return nextState
}
// convert matrix of seats into a string for logging
function matToString (m) {
return m.reduce((a1, r, y) => {
return a1 +
r.reduce((a,full,x) => a+String(full?'#':layoutMat[y][x]?'L':'.'), '') +
'\n'},
'');
}
// compare two matricies because javascript wont do that for you
function compareMat (m1, m2) {
return m1.reduce((a1,v1,i)=>a1&&v1.reduce((a2,v2,j)=>a2&&v2==m2[i][j],true),true)
}
// create matrix of false values. this is the initial state (all empty seats)
let stateMat = new Array(layoutMat.length).fill(new Array(layoutMat[0].length).fill(false));
while (true) {
// console.log(matToString(stateMat))
// console.log(stateMat.reduce((a1,b1)=>a1+b1.reduce((a2, b2)=>a2+b2, 0), 0))
prevState = stateMat.map(r => r.slice()); // clone the current state
stateMat = iterate(stateMat, layoutMat); // iterate on the state
if (compareMat(prevState, stateMat)) break // if there is no change, we are done
}
// log the total amount of taken seats
console.log(stateMat.reduce((a1,b1)=>a1+b1.reduce((a2, b2)=>a2+b2, 0), 0))

71
2020/11/part2.js Normal file
View file

@ -0,0 +1,71 @@
// this is really slow but it works after a few seconds. I only learnt about memoising yesterday and couldn't get it to work. I will probably leave this as it is anyway.
const fs = require('fs');
const data = fs.readFileSync('input.txt', 'utf-8');
// convert seat layout to matrix of bools
let layoutMat = data.split('\n').map(l=>l.split('').map(s=>s=='L'));
function iterate (stateMat, layoutMat){
let nextState = stateMat.map(r => r.slice());
for (let y = 0; y < layoutMat.length; y++) {
for (let x = 0; x < layoutMat[1].length; x++) {
if (!layoutMat[y][x]) continue // if this point is the floor, skip
// count how many adjacent seats are full
let seenSeats = lookAround(stateMat, x, y)
// update seat according to rules in the challenge
nextState[y][x] = stateMat[y][x] ? seenSeats < 5 : seenSeats == 0;
// console.log('from', x,y,'i see', seenSeats, 'full chairs', nextState[y][x] ? 'so i sit here':'so i leave')
}
}
return nextState
}
// look every direction from given co-ords
function lookAround (stateMat, x, y) {
let amt = 0
for (let dx = -1; dx <=1; dx++) {
for (let dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) continue
isChair = look(stateMat, x, y, dx, dy);
// console.log(matToString(stateMat))
// console.log('looking from', x, y, 'in direction', dx,dy, 'chair is', isChair)
amt += isChair
}
}
return amt
}
function look(stateMat, x, y, dx, dy) {
// if this is the edge, it there is no full chair
if (x+dx < 0 || y+dy < 0 || x+dx >= stateMat[0].length|| y+dy >= stateMat.length) {return false}
// if there is a chair, return if it is full or not
if (layoutMat[y+dy][x+dx]) {return stateMat[y+dy][x+dx]}
// this must be floor, check next spot
return look(stateMat, x+dx, y+dy, dx, dy)
}
// convert matrix of seats into a string for logging
function matToString (m) {
return m.reduce((a1, r, y) => {
return a1 +
r.reduce((a,full,x) => a+String(full?'#':layoutMat[y][x]?'L':'.'), '') +
'\n'},
'');
}
// compare two matricies because javascript wont do that for you
function compareMat (m1, m2) {
return m1.reduce((a1,v1,i)=>a1&&v1.reduce((a2,v2,j)=>a2&&v2==m2[i][j],true),true)
}
// create matrix of false values. this is the initial state (all empty seats)
let stateMat = new Array(layoutMat.length).fill([]).map(r => {return new Array(layoutMat[0].length).fill(false)})
while (true) {
prevState = stateMat.map(r => r.slice()); // deep clone the current state
stateMat = iterate(stateMat, layoutMat); // iterate on the state
// console.log(matToString(stateMat))
if (compareMat(prevState, stateMat)) break // if there is no change, we are done
}
// log the total amount of taken seats
console.log(stateMat.reduce((a1,b1)=>a1+b1.reduce((a2, b2)=>a2+b2, 0), 0))

10
2020/11/test.txt Normal file
View file

@ -0,0 +1,10 @@
L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL

View file

@ -51,6 +51,7 @@ if (advent) {
8. [:star: :star:](https://adventofcode.com/2020/day/8 "see puzzle") 8. [:star: :star:](https://adventofcode.com/2020/day/8 "see puzzle")
9. [:star: :star:](https://adventofcode.com/2020/day/9 "see puzzle") 9. [:star: :star:](https://adventofcode.com/2020/day/9 "see puzzle")
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")
## Languages Used ## Languages Used
* Python * Python