Compare commits

...

5 Commits

Author SHA1 Message Date
53014651e9 day6, a set kills all 2024-04-14 01:16:42 +02:00
3ec99a8a10 day6, from N^3 to N^2 2024-04-14 00:19:55 +02:00
b360d60a05 day 18, compile with codon for speed 2023-06-12 12:49:11 +02:00
712049e929 add input for benchmark 2023-05-03 10:34:13 +02:00
42a525352a day 10, part 2, no need to buffer, spare 10 loc 2023-01-02 12:35:09 +01:00
7 changed files with 138 additions and 26 deletions

View File

@@ -1,17 +1,17 @@
BEGIN { W = 14 } { # use -vW=4 or 14
{ for (i=1; ; i++) {
for (i=1; i<=length($1); i++) {
window = substr($1,i,W) window = substr($1,i,W)
# compare each char of the window to rest of the window # compare each char of the window to rest of the window
S = 0 # to count match S = 0 # to count match
for (j=1; j<=W; j++) { for (j=1; j<W; j++) {
for (k=j+1; k<=W; k++) { for (k=j+1; k<=W; k++) {
N++
S+= (substr(window,j,1) == substr(window,k,1)) ? 1 : 0 S+= (substr(window,j,1) == substr(window,k,1)) ? 1 : 0
} }
} }
if (S == 0) { if (S == 0) {
print i + W - 1 print i + W - 1, "(" N " loops)"
next next
} }
} }
} } # O(N*W^2)

17
d06/exec1.awk Normal file
View File

@@ -0,0 +1,17 @@
{ N=0; # use -vW=4 or 14
for (i=1; ; i++) {
window = substr($0,i,W)
# compare each char of the window to rest of the window
S = 0 # to count match
for (j=1; j<W; j++) {
for (k=j+1; k<=W; k++) {
N++
S+= (substr(window,j,1) == substr(window,k,1)) ? 1 : 0
}
}
if (S == 0) {
print i + W - 1, "(" N " loops)"
next
}
}
} # O(N*W^2)

17
d06/exec2.awk Normal file
View File

@@ -0,0 +1,17 @@
{ N=0; j=0; # use -vW=4 or 14
for (i=1; ; i++) {
c = substr($0,i,1);
# look backward for a "new j" > j
for (k=i-1; k>j; k--) {
N++
if (substr($0,k,1) == c) {
j = k
break
}
}
if ((i-j) == W) {
print i, "(" N " loops)"
next
}
}
} # O(N*W)

13
d06/exec3.awk Normal file
View File

@@ -0,0 +1,13 @@
{ # use -vW=4 or 14
for (i=1; ; i++) {
window = substr($0,i,W)
for (j=1; j<=W; j++) { # populate a Set
A[substr(window,j,1)]=1
}
if (length(A) == W) {
print i + W - 1
next
}
delete A
}
} # O(N*W*log2(W))

View File

@@ -1,20 +1,10 @@
BEGIN { x=1 }
function inc() { function inc() {
i = (c%40) i = (c++%40)
A[c+1] = (i==x-1||i==x||i==x+1) ? "█" : "" printf "%s", (i==x||i==x+1||i==x+2) ? "█" : " "
c+=1 printf "%s", (i==39) ? "\n" : ""
} }
{ inc() } { inc() }
$1 == "addx" { $1 == "addx" {
inc() inc()
x+=$2 x += $2
}
END {
for (i=0;i<=5;i++) {
r = ""
for (j=1;j<=40;j++) {
r = r A[i*40+j]
}
print r
}
} }

View File

@@ -2,16 +2,18 @@ import sys
S = set() # cubes S = set() # cubes
for l in sys.stdin.read().splitlines(): for l in sys.stdin.read().splitlines():
S.add(tuple(map(int,l.split(',')))) (x,y,z) = list(map(int,l.split(','))) # makes type of tuple explicite for codon
S.add((x,y,z))
A = 23 A = 23
L = set() # water L = set() # water
for x in range(-2,A+1): for x in range(-2,A+1):
for y in range(-2,A+1): for y in range(-2,A+1):
L.add((x,y,-2)) L.add((x,y,-2))
more = True more = True
while more: while more:
c = 0 more = False
for x in range(-2,A+1): for x in range(-2,A+1):
for y in range(-2,A+1): for y in range(-2,A+1):
for z in range(-2,A+1): for z in range(-2,A+1):
@@ -19,9 +21,8 @@ while more:
for (i,j,k) in [(0,0,1),(0,0,-1),(0,1,0),(0,-1,0),(1,0,0),(-1,0,0)]: for (i,j,k) in [(0,0,1),(0,0,-1),(0,1,0),(0,-1,0),(1,0,0),(-1,0,0)]:
if (x+i,y+j,z+k) in L: # if neighbour is water if (x+i,y+j,z+k) in L: # if neighbour is water
L.add((x,y,z)) # water expand L.add((x,y,z)) # water expand
c += 1 more = True
break break
more = (c>0)
N = 0 N = 0
for (x,y,z) in S: for (x,y,z) in S:
@@ -30,5 +31,4 @@ for (x,y,z) in S:
N += 1 N += 1
print(N) print(N)
# > 2489 # ~/.local/programs/codon/bin/codon build --relocation-model=static --release -o part2 part2.py
# > 2520

75
d23/input Normal file
View File

@@ -0,0 +1,75 @@
...#######...####..#........#.#.##...##.##..#.##.####..####.##.##...###..##
##.###.#...##..#....###..#.##.###.##..##..###..#....##.#..##.####..#.#.#.##
..##.#.###.#......#...#....#...#.#.#...##......####..######.##....#####.##.
##.#####.#.###.####.#....#.....####.#####..##.....#.##...###...#.####..#...
...#.....#.#..#.######.###....#.....#.###..##..#.#.###..##..#.#......#..#..
.###.#.##.##.##.....##.........#....#.#.#..#....###..####..#.#.###..###..#.
.#####.......##.#..#...###.##...#.###...#..##.#.###.......##.#..#..##...##.
.###.#.#.###.#.#..#..###..###..###.########.###..#.##.#.##...##.##.###.#...
.....######.###.###.......####...##.#.##..#.#.##.#....##.###.#.###...###..#
..#..##...#..#...##.#.#..###...#.##.##.##..##.###.#.#..#...#.#..#......####
##..##.##..#.#.##...#.#.#...###.....#....###....#######.#####..#...#.##..##
####....######.#..##...#....#.##....##.#..#.##..##..#.#.....##..####..#....
....####..#.......##...#........#.##..#.#.#.##.##..##.##.....##..#...#.##.#
.####..######.##.#.#.#.##...###..##...######..#.#.#.####.######.#...###....
##..#.#...#.#.#.#.###.#.###.#.#.##.....#..##..##.##...###....#...#.###...#.
#####.##...###..#.#.######..#.###...###..#..###.....#....####..#.####...###
#.#.#....###.##...#.###.#.###.###..###.#.#.....#.##....#.##.##.#####..##..#
...##.#...##....####.#.##.#.####.###...#.##....#..##.###..#......#######.##
....#..#.#.#...##....#.####.##...#..#####...#.##..###..##...#..#.##.#.#.#.#
..#..#####.#..#.#.#..#.###.##...#........#.#.....#.##.....#.##.#..#.#.#.#..
..#.##.###....##.#..#.#####.....#.##.....#.#..###.###..#.#......#..#...#.##
##.#.#...#...#.##..#......###.#.#.####.#...#########.#..#..##...##...##.###
...#.###.#..#######.#.#.##.##..###.##.#.######.#.##.##.##.#########.#...#.#
#..#...##.##.######.#..#.####...####.###.#...##.####..##..#.#...##.#.#.##..
.##.###..#####.###.##..#.#.#.#....#.#....#...#....######.##.#...#...#.#...#
#..#.....##....#.#..##.###..#..####..######...##.#..#######....#.#..##.##..
##...#.##.#...#....#.##.##..#####...##.#.#.#.#...#...#.#....#.##..##..##..#
..##....#..###...#.#.#.##....#...##...###.##.#.....#..#####.#####.....##..#
.#.##..#.#.###..#...###.....##...###..#.#######.#..#.#.##.##..##.#.#..##.##
#.#...#..###..#...####.#.#..##.#..#....#.#.#.###.#.##.##.#.......#.##....##
.#.##.#.#..#.#.#.#.##..#.###..#....#..#...###.#.#.##..#.##..#..#.#..#...###
..#..#.##......#....##.#######..#..##.....#.#.##..#..#..#.#.#..#.#..#.##...
#.#.###.###..######.##....#####.####..#....##.##.##.....##..#..###...##..##
.##..#.#.###..##.....###.#.##.#..#.....#.##..#...#...###...#...##...#####.#
.....#....######..##.#..##..#...##.#.##.#..##...####.##..#.#.####.###......
#..##....#.#.#.#....##..#....##...##.#...###.##..#....#.#...#.#..###...#..#
...###.#.####..##...###.#.##.#..#.####.#.#.#####....####.#.#.#.##...#.##...
.#..##.#...#..##.######.#######.#.#...##...##..#.#.########..#..##.#..###..
..#####.####.#.#.##........###...#..###...##...#...#....####...##.#.#######
#.#####......###.#..###.....##..##...######.#..##.#.#.#######..##.####...#.
#.#.#.#..##..#.##..###....##.#.##.####..#.#####..###......#...#..#..###....
..#...#.##..##.#..#####.######...###...#...##..#.#####..#.###....#..#.#.#..
###..#.#####.##.#.######..##.#####...#...#####.#.#.#.#...#.#.#.#....##....#
#.#...###...#.#...####..........#####..###..#.#...####...#.#.###.#..####.##
..#..##.###.##...####.#....#..##.....####.##.#.#....#..#..##.#..##.#.#..###
..#......##.#.####..###.####..##.####.##...#.#.####.#.#..#..######.##.##.##
....###...#.##.####....#...#.#..#..##.#.####...##...#...#.#.##...##.##..#.#
######....#.#..####.#.#...#####.#.#..##.######.#.###.#.#.#....#...#....##..
....####...#..#.#.#.####.#..#..###..#..#.##.########...#..#.#.........#.#..
##....#.#.##.####.#.#..##.#..##.#.#.#.#.#.#..#######.##..#.##....####...###
##.#.#.##.#...###.#...#...##..#.#.#...#....#...#....#.#..###.##.##...##....
.####....#.##...#.#.#.....#.#.#.#..#.....###.##.#..#...#...###.#..#.#..##.#
##..#.#....##....#.##.##.#.###........#.#..#.#....#.#.###...####..##.####.#
.##.#..###..#.#...###.###.###.###.#.#...##.#.#.###.#.####.###.##...#.#.#...
.#.##.##..#.#.####..##.#####.#..#..####.###..###..####.....###.###.......#.
##.#..#.#..###..#.....#.##.....#.#.#.##..######.#..#.#...#...##..###..##.#.
##..##...#........##..##.######.#.####...#..#..####..#.##.#...#..#.##.#.##.
#..#.##..###.#......####..##.##.#....#.###.##..#.#.#.##.##..#..#.##..##....
.....#....#.#.##..#.##....####.####.....#.###.#.#..#.#.....#..##..##.###...
.#......##....#.##.#...#.#####...##..##.....#.##.##.....#..#..##.#...#####.
##.#...##...#....#.####.#.#....#.#..######..........####.....#.#.#......#..
#.#.#....###..#..####..#.##...##.#####.#...#.#...###...#..##..##.####.#...#
#...###.##.#..#.###..##...##.##..#...##.#....#.#.##.##....#.#....#...#####.
#....##..####.....#.##.#####.#...##..##....#...###...#...#..#.#...#..#...##
#..##.##.....##..#.###..........###...#.#...#..#..##..#...##.####.#....###.
.#....#..#..##.###..########.#......#...#..#..##.##....###.###.....#...##.#
##..###.#....#.#...##.#.###.#.#..#..#..###.#...#.##.#####.#.#..##..#.#..#.#
#.#.#...#....#####.####..#.##.####..#.#.#...#....#..#.###...###.###...#...#
.###..##.#..##..#..#...##.#.#...###..##.#.####.#....#.#...##....#....##..##
..#.##..####.##..##...##..#.###.......#..####.##..#.#......#...##..###.....
##....##.####.####..#.#.#..#####.....###.##..###..####.###.#####..#.....#.#
.####.#..#####.....#..######...##.#....####......######...###..###..#.#.###
.#..#.#.####.#.##........###.#.#.#.##.#..#...#......###.#.##.#..#.#...#...#
.#..#.##....#...#...#.##..#...##.....#..######..##.###...#..#..####..#.#.#.
##.#.#.#..#.##..#.#......#.######..#####...#.##.###.######.#...#.###.##...#