2020 day 12 + visualisation

This commit is contained in:
tristan 2020-12-12 18:34:40 +00:00
parent 29b64b4715
commit 171802a184
8 changed files with 1038 additions and 3 deletions

3
.gitignore vendored
View file

@ -2,4 +2,5 @@ build
frames
.DS_Store
.vscode
.replit
.replit
main.sh

View file

@ -1,5 +1,5 @@
const fs = require('fs');
const data = fs.readFileSync('input.txt', 'utf-8');
const data = fs.readFileSync('./input.txt', 'utf-8');
const lines = data.split('\n').map(num => parseInt(num)).sort((a,b)=>(a-b));
lines.unshift(0); // add the outlet with joltage 0
lines.push(lines[lines.length-1]+3); // your device has a joltage 3 above the highest

788
2020/12/input.txt Normal file
View file

@ -0,0 +1,788 @@
N1
R90
S5
R180
N3
W1
L180
F92
R270
E4
F4
W4
W4
L180
S2
W2
F90
E1
S5
W3
F78
S5
R180
F100
N1
W3
L90
L90
N1
F94
W2
R90
F49
W2
F26
R180
W1
S5
R180
W4
S3
R90
W3
S4
E5
S1
F13
N5
R270
E2
R270
S5
F3
E3
F4
S3
R270
S1
W4
R90
S4
L180
N4
F81
W2
R90
F61
R90
F13
N3
R180
W1
F98
S5
F50
W5
S3
W5
R90
F17
S5
F70
F7
E2
F87
E1
L270
F59
E2
R180
N5
F59
L90
N5
W5
F10
N3
E1
R90
W1
S2
R90
N5
F25
R90
E2
F57
R180
E1
N3
W2
F85
L90
F50
W2
R90
S3
R90
F27
E1
S1
L90
F32
L90
W3
R90
E1
F39
S5
E4
F50
W4
L90
F63
N2
F67
W3
R90
F4
N2
R90
F90
N5
L180
F24
E5
N3
L180
F67
E3
L90
S3
F49
R90
E5
F89
W5
F62
F39
F33
W1
R90
F18
S3
R90
N4
F47
N5
N3
W2
S5
L90
E4
L90
W2
R90
W5
L90
W5
N4
F64
R90
S2
W4
R90
N3
F18
L90
S4
L90
F31
S4
L90
F79
R90
F69
N3
E4
F64
N2
E4
R90
F20
R180
E1
F85
W1
S5
S2
F21
R90
F43
N1
F18
S5
R180
F52
L180
W4
F5
L90
F70
S4
N3
R180
F64
R90
F17
R90
E5
F85
N1
F74
E5
F21
N1
F35
N1
F65
W2
F67
N1
E5
F79
S4
R90
F20
R180
W5
L180
S4
F56
S4
L90
E5
F13
S5
F38
W1
S2
L90
N4
E3
R180
W3
N1
R90
F52
N5
F23
E5
F82
E5
S2
E3
N3
S2
L90
N1
R90
S5
F60
W1
N2
W1
N3
E4
F2
E2
L90
S1
L90
E4
N1
R180
E2
R180
F93
F94
L90
S4
E5
R90
F5
S2
E2
S3
E4
R180
F56
E2
N2
F3
R90
W2
F94
W5
F47
L180
F68
E5
F63
S3
E4
F93
L90
S5
L180
W5
S5
W3
L180
F34
R90
F87
W4
S1
W3
R270
S1
E1
F78
E4
R90
F91
W4
S3
W1
F41
N4
E1
F66
S1
W5
F62
N2
W2
L90
W1
F23
L270
N2
W2
S3
F9
R90
F2
E4
F61
L90
W5
N4
F97
L90
F93
N5
L270
R90
W1
R90
R90
N4
E1
F72
N4
R270
F24
W1
F79
S1
E3
N4
E3
L90
W2
S1
R270
W5
F24
E5
S4
F22
L180
F57
S5
R90
N4
W3
F18
N2
R90
E3
F55
N2
R90
S5
F4
W3
L90
N2
W3
L270
E4
R90
F46
S5
N1
F16
N1
R90
F8
L180
N2
W3
N4
E1
S3
L90
F4
E5
N5
E3
R90
F35
N2
F68
F33
E5
F38
E4
F27
R180
S5
F47
R90
F43
R90
S1
F84
L180
F47
R90
N4
E4
F77
R180
N1
E2
S4
F45
S1
L90
E5
F40
L90
W5
F25
W4
R90
F80
N5
E2
F74
W3
N3
E4
F48
N3
R90
N2
W1
L90
S2
F35
L90
E5
R180
W5
N2
E1
L90
N2
F78
S5
R270
S5
R90
N5
E3
L90
S5
F13
S5
F52
L90
N2
R180
E1
F41
S1
F20
N4
F34
N2
F45
E5
L90
W3
L270
N5
F52
R90
N5
E5
N2
W2
W5
R270
W5
F10
N3
F63
N4
F53
L90
E5
L270
F17
N1
L90
F26
F93
R90
S5
R270
S5
R180
N4
F58
L180
F40
S2
F54
N5
F70
W1
N4
W1
L90
W5
R90
N2
R90
S5
F95
W4
L180
E3
F68
S1
F56
R90
W1
L180
F66
R90
S2
F57
L90
E1
F42
S4
F44
L90
F42
E4
R90
S4
W5
R90
E4
S4
E5
F27
R90
N1
R90
E5
R90
W4
S1
F81
N5
R180
S4
E4
F68
S3
L90
E4
E4
L180
E3
F8
W2
L90
S4
L180
N2
L180
E1
R90
W5
N4
W4
R90
F1
S5
E2
L90
F49
N4
W3
R90
E5
F33
R180
S4
E5
S2
F79
W4
F38
R90
F1
L90
F56
L270
N2
L90
E2
L90
F25
W1
S4
L270
W3
R90
N2
F68
E1
R180
W3
R90
W3
R90
S3
F4
W3
N3
R90
W3
N1
F54
W2
S5
E4
F76
F47
N1
F32
L180
L90
F19
N2
E5
L90
E1
L90
E3
R90
F48
R270
S3
R180
S4
F53
R90
F90
E4
F100
L90
F49
N1
W1
F56
E2
N5
L90
F39
R90
W2
F26
E4
N4
L90
F9
L90
F41
W5
N4
S1
W4
N3
R90
N5
L270
F82
L90
F75
S5
F25
S4
F67
N4
F57
E4
N4
F73
W5
L90
E2
R180
N5
L270
W3
F95
W2
S4
E1
R180
N3
W2
N1
F28
N2
R90
E3
S1
F41
E4
N1
R90
F12
L90
N2
S2
E3
F31
W1
L90
E5
S1
F12
R180
W5
R90
F26

38
2020/12/part1.js Normal file
View file

@ -0,0 +1,38 @@
// cool today's puzzle is basically a turtle
const fs = require('fs');
const instructions = fs.readFileSync('input.txt', 'utf-8').split('\n');
// position and direcion start at 0
let x = y = d = 0;
instructions.forEach((instruction, i) => {
console.log(x,y,d)
action = instruction[0];
arg = Number(instruction.slice(1));
switch (action) {
case 'N':
y+=arg;
break;
case 'S':
y-=arg;
break;
case 'E':
x+=arg;
break;
case 'W':
x-=arg;
break;
case 'L':
d-=arg;
break;
case 'R':
d+=arg;
break;
case 'F':
y -= Math.round(arg * Math.sin(d*Math.PI/180));
x += Math.round(arg * Math.cos(d*Math.PI/180));
break;
default:
console.log('instruction no', i+1, action,'invald')
}
})
console.log(x,y,d)
console.log(Math.abs(x)+ Math.abs(y))

27
2020/12/part2/index.html Normal file
View file

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>day 12 visualisation</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<canvas id="can" width="500" height="500"></canvas>
<br>
<label for="scalerange">scale</label>
<input name="scalerange" type="range" min="-1" max="1.5" step="0.01" value="0" id="scale">
<label for="speedrange">speed</label>
<input name="speedrange" type="range" min="15" max="1000" step="1" value="1000" id="speed" onchange="setSpeed()">
<textarea rows="5" id="input" onchange="reset();setSpeed()">F10
N3
F7
R90
F11</textarea>
<label for="loopbox">loop?</label>
<input type="checkbox" id="loop" name="loopbox" checked="true" onchange="setSpeed()">
<div id="output"></div>
<button id="clearbutton">clear</button>
<script src="script.js"></script>
</body>
</html>

149
2020/12/part2/script.js Normal file
View file

@ -0,0 +1,149 @@
const canvas = document.getElementById('can')
const c = canvas.getContext('2d');
const out = document.getElementById('output')
let instructions=document.getElementById('input').value.split('\n');
let wpx = 10;
let wpy = -1;
let shipX = 0;
let shipY = 0;
let i = 0
function reset () {
instructions = document.getElementById('input').value.split('\n');
wpx = 10;
wpy = -1;
shipX = 0;
shipY = 0;
i = 0
print('reset')
}
function stop() {
document.getElementById('loop').checked = false
clearInterval(loopInterval);
reset()
}
function iterate(i) {
instruction = instructions[i]
if (!instruction) {print('oops there is no instruction no '+ i); return}
action = instruction[0].toUpperCase();
arg = Number(instruction.slice(1));
if (isNaN(arg)){
print('instruction no '+(i+1)+': "'+instruction.slice(1)+'" is not a number')
stop();
return
}
switch (action) {
case 'N':
wpy-=arg;
break;
case 'S':
wpy+=arg;
break;
case 'E':
wpx+=arg;
break;
case 'W':
wpx-=arg;
break;
case 'L':
switch (arg) {
case 90:
bx = wpx
by = wpy
wpx = by
wpy = -bx
break;
case 180:
wpx *= -1;
wpy *= -1;
break;
case 270:
bx = wpx
by = wpy
wpx = -by
wpy = bx
break;
case 0:
break;
default:
print('tried to rotate '+ arg+ ' degrees. can only rotate in right angles');
stop()
return
}
break;
case 'R':
switch (arg) {
case 90:
bx = wpx
by = wpy
wpx = -by
wpy = bx
break;
case 180:
wpx *= -1;
wpy *= -1;
break;
case 270:
bx = wpx
by = wpy
wpx = by
wpy = -bx
break;
case 0:
break;
default:
print('tried to rotate '+ arg+ ' degrees. can only rotate in right angles');
stop()
return
}
break;
case 'F':
shipX += wpx*arg;
shipY -= wpy*arg;
break;
default:
print('instruction no '+ (i+1) + ': "' + action+'" invald')
stop()
return
}
print('instruction no '+ (i+1) + ': "'+instruction+'" ship at ('+shipX+', '+shipY+') pointing ('+wpx+', '+-wpy+')')
}
function clear() {
out.innerHTML = ""
}
document.getElementById('clearbutton').onclick = clear;
setInterval(()=>{
scale = Math.pow(100, document.getElementById('scale').value)
c.clearRect(0,0,canvas.width,canvas.height);
c.fillStyle = 'green'
c.fillRect(canvas.width/2+(shipX)/scale-5, canvas.height/2-(shipY)/scale-5, 10,10);
c.fillStyle = 'yellow'
c.fillRect(canvas.width/2+(shipX+wpx)/scale-2, canvas.height/2-(shipY-wpy)/scale-2,4,4);
}, 30)
let loopInterval = setInterval(loop, 1000)
function loop () {
if (i<instructions.length) {
iterate(i)
i++
} else {
print('ended at ('+shipX+', '+shipY+') with manhattan distance of '+(Math.abs(shipX)+Math.abs(shipY)))
reset();
if (!document.getElementById('loop').checked)
clearInterval(loopInterval);
}
}
function setSpeed () {
clearInterval(loopInterval);
loopInterval = setInterval(loop, document.getElementById('speed').value)
}
function print(str) {
out.innerHTML=str+'<br>'+out.innerHTML
}

27
2020/12/part2/style.css Normal file
View file

@ -0,0 +1,27 @@
#output {
display: block;
overflow-y: scroll;
height: 100px;
}
#input {
position: absolute;
top: 8px;
left: 516px;
height: 500px;
}
input {
direction: rtl;
}
canvas, #output {
background-color: #10101a;
border: 1px solid #333340;
}
body {
background-color: #0f0f23;
color: #cccccc;
font-family: "Source Code Pro", monospace;
}

View file

@ -52,6 +52,7 @@ if (advent) {
9. [:star: :star:](https://adventofcode.com/2020/day/9 "see puzzle")
10. [:star: :star:](https://adventofcode.com/2020/day/10 "see puzzle")
11. [:star: :star:](https://adventofcode.com/2020/day/11 "see puzzle")
12. [:star: :star:](https://adventofcode.com/2020/day/12 "see puzzle")
## Languages Used
* Python
@ -59,4 +60,8 @@ if (advent) {
* Node JS
- js
* Java (Processing)
- pde
- pde
* HTML, CSS, JavaScript
- html
- css
- js