Initial Commit - days 01-12

This commit is contained in:
2020-12-21 14:58:15 +01:00
commit 11844d1904
131 changed files with 16060 additions and 0 deletions

769
day12/INPUT.txt Normal file
View File

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

29
day12/Makefile Normal file
View File

@@ -0,0 +1,29 @@
INPUT := INPUT.txt
SHELL := /bin/bash
#CFLAGS := -w -pg
CFLAGS := -w -O3
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
export PATH := .:$(PATH)
.PHONY: clean all compile deploy ex1 ex2
all: ex1 ex2
output:
@$(MAKE) --no-print-directory all 2>&1 > OUTPUT
compile: ex1-c ex2-c
ex1: ex1-c
@$(TIME) ex1.bash < $(INPUT) 2>&1
@$(TIME) ex1-c < $(INPUT) 2>&1
ex2: ex2-c
@$(TIME) ex2.bash < $(INPUT) 2>&1
@$(TIME) ex2-c < $(INPUT) 2>&1
clean:
@rm -f ex1-c ex2-c core
deploy:
@$(MAKE) -C .. deploy

16
day12/OUTPUT Normal file
View File

@@ -0,0 +1,16 @@
ex1.bash : res=1457
time: 0:00.02 real, 0.02 user, 0.00 sys
context-switch: 5+1, page-faults: 0+152
ex1-c : res=1457
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+72
ex2.bash : res=106860
time: 0:00.09 real, 0.08 user, 0.00 sys
context-switch: 10+1, page-faults: 0+153
ex2-c : res=106860
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 1+1, page-faults: 0+75

76
day12/README Normal file
View File

@@ -0,0 +1,76 @@
--- Day 12: Rain Risk ---
Your ferry made decent progress toward the island, but the storm came in faster than anyone expected. The ferry needs to take evasive actions!
Unfortunately, the ship's navigation computer seems to be malfunctioning; rather than giving a route directly to safety, it produced extremely circuitous instructions. When the captain uses the PA system to ask if anyone can help, you quickly volunteer.
The navigation instructions (your puzzle input) consists of a sequence of single-character actions paired with integer input values. After staring at them for a few minutes, you work out what they probably mean:
Action N means to move north by the given value.
Action S means to move south by the given value.
Action E means to move east by the given value.
Action W means to move west by the given value.
Action L means to turn left the given number of degrees.
Action R means to turn right the given number of degrees.
Action F means to move forward by the given value in the direction the ship is currently facing.
The ship starts by facing east. Only the L and R actions change the direction the ship is facing. (That is, if the ship is facing east and the next instruction is N10, the ship would move north 10 units, but would still move east if the following action were F.)
For example:
F10
N3
F7
R90
F11
These instructions would be handled as follows:
F10 would move the ship 10 units east (because the ship starts by facing east) to east 10, north 0.
N3 would move the ship 3 units north to east 10, north 3.
F7 would move the ship another 7 units east (because the ship is still facing east) to east 17, north 3.
R90 would cause the ship to turn right by 90 degrees and face south; it remains at east 17, north 3.
F11 would move the ship 11 units south to east 17, south 8.
At the end of these instructions, the ship's Manhattan distance (sum of the absolute values of its east/west position and its north/south position) from its starting position is 17 + 8 = 25.
Figure out where the navigation instructions lead. What is the Manhattan distance between that location and the ship's starting position?
Your puzzle answer was 1457.
--- Part Two ---
Before you can give the destination to the captain, you realize that the actual action meanings were printed on the back of the instructions the whole time.
Almost all of the actions indicate how to move a waypoint which is relative to the ship's position:
Action N means to move the waypoint north by the given value.
Action S means to move the waypoint south by the given value.
Action E means to move the waypoint east by the given value.
Action W means to move the waypoint west by the given value.
Action L means to rotate the waypoint around the ship left (counter-clockwise) the given number of degrees.
Action R means to rotate the waypoint around the ship right (clockwise) the given number of degrees.
Action F means to move forward to the waypoint a number of times equal to the given value.
The waypoint starts 10 units east and 1 unit north relative to the ship. The waypoint is relative to the ship; that is, if the ship moves, the waypoint moves with it.
For example, using the same instructions as above:
F10 moves the ship to the waypoint 10 times (a total of 100 units east and 10 units north), leaving the ship at east 100, north 10. The waypoint stays 10 units east and 1 unit north of the ship.
N3 moves the waypoint 3 units north to 10 units east and 4 units north of the ship. The ship remains at east 100, north 10.
F7 moves the ship to the waypoint 7 times (a total of 70 units east and 28 units north), leaving the ship at east 170, north 38. The waypoint stays 10 units east and 4 units north of the ship.
R90 rotates the waypoint around the ship clockwise 90 degrees, moving it to 4 units east and 10 units south of the ship. The ship remains at east 170, north 38.
F11 moves the ship to the waypoint 11 times (a total of 44 units east and 110 units south), leaving the ship at east 214, south 72. The waypoint stays 4 units east and 10 units south of the ship.
After these operations, the ship's Manhattan distance from its starting position is 214 + 72 = 286.
Figure out where the navigation instructions actually lead. What is the Manhattan distance between that location and the ship's starting position?
Your puzzle answer was 106860.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
You can also [Share] this puzzle.

5
day12/TEST.txt Normal file
View File

@@ -0,0 +1,5 @@
F10
N3
F7
R90
F11

61
day12/ex1-c.c Normal file
View File

@@ -0,0 +1,61 @@
/* ex1-c: Advent2020 game, day 12/game 1
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ABS(i) ((i)>=0?(i):(-i))
int main(ac, av)
int ac;
char **av;
{
char line[512], command;
unsigned val, direction=90, real;
int x=0, y=0;
while (fgets(line, sizeof line, stdin)) {
command=*line;
val=atoi(line+1);
switch (command) {
case 'N':
real=0;
break;
case 'E':
real=90;
break;
case 'S':
real=180;
break;
case 'W':
real=270;
break;
case 'F':
real=direction;
break;
case 'L':
val=360-val;
case 'R':
direction=(direction+val)%360;
}
if (command != 'R' && command != 'L') {
switch (real) {
case 0:
y+=val;
break;
case 90:
x+=val;
break;
case 180:
y-=val;
break;
case 270:
x-=val;
break;
}
}
}
printf("%s : res=%d\n", *av, ABS(x)+ABS(y));
exit (0);
}

53
day12/ex1.bash Executable file
View File

@@ -0,0 +1,53 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 12/game 1.
CMD=${0##*/}
shopt -s extglob
declare -i X=0 Y=0
declare -i DIRECTION=90 REAL
while read -r line; do
C=${line:0:1}
A=${line:1}
#printf "CMD=%s ARG=%d\n" "$C" "$A"
case "$C" in
R) ((DIRECTION=(DIRECTION+A)%360))
#printf "D=%d\n" "$DIRECTION"
;;
L) ((DIRECTION=(DIRECTION+360-A)%360))
#printf "D=%d\n" "$DIRECTION"
;;
F) REAL=$DIRECTION
;;
N) REAL=0
;;
S) REAL=180
;;
E) REAL=90
;;
W) REAL=270
;;
esac
if [[ "$C" != "R" && "$C" != "L" ]]; then
case "$REAL" in
0) ((Y+=A))
;;
90) ((X+=A))
;;
180) ((Y-=A))
;;
270) ((X-=A))
;;
esac
fi
#printf "D=%3d POS=(%d,%d)\n" "$DIRECTION" "$X" "$Y"
done
((X<0)) && ((X=-X))
((Y<0)) && ((Y=-Y))
printf "%s : res=%d\n" "$CMD" $((X+Y))
exit 0

58
day12/ex2-c.c Normal file
View File

@@ -0,0 +1,58 @@
/* ex2-c: Advent2020 game, day 12/game 1
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ABS(i) ((i)>=0?(i):(-i))
int main(ac, av)
int ac;
char **av;
{
char line[512], command;
unsigned val, direction, i;
int x=0, y=0, wx=10, wy=1, tmp;
while (fgets(line, sizeof line, stdin)) {
command=*line;
val=atoi(line+1);
direction=0;
switch (command) {
case 'L':
direction=360-val;
break;
case 'R':
direction=val;
break;
case 'F':
for (i=0; i<val; ++i) {
x+=wx;
y+=wy;
}
break;
case 'N':
wy+=val;
break;
case 'E':
wx+=val;
break;
case 'S':
wy-=val;
break;
case 'W':
wx-=val;
break;
}
if (direction) {
for (i=90; i<=direction; i+=90) {
tmp=wy;
wy=-wx;
wx=tmp;
}
}
}
printf("%s : res=%d\n", *av, ABS(x)+ABS(y));
exit (0);
}

48
day12/ex2.bash Executable file
View File

@@ -0,0 +1,48 @@
#!/bin/bash
#
# ex2.bash: Advent2020 game, day 12/game 2.
CMD=${0##*/}
shopt -s extglob
declare -i X=0 Y=0 WX=10 WY=1
declare -i DIRECTION=90
while read -r line; do
C=${line:0:1}
A=${line:1}
DIRECTION=0
case "$C" in
R) ((DIRECTION=A))
;;
L) ((DIRECTION=360-A))
;;
F) for ((i=0; i<A; ++i)); do
((X+=WX))
((Y+=WY))
done
;;
N) ((WY+=A))
;;
S) ((WY-=A))
;;
E) ((WX+=A))
;;
W) ((WX-=A))
;;
esac
if ((DIRECTION > 0)); then
for ((i=90; i<=DIRECTION; i+=90)); do
((tx=WY))
((WY=-WX))
((WX=tx))
done
fi
done
((X<0)) && ((X=-X))
((Y<0)) && ((Y=-Y))
printf "%s : res=%d\n" "$CMD" $((X+Y))
exit 0