Crestwave's recent activity

  1. Comment on Day 10: Cathode-Ray Tube in ~comp.advent_of_code

    Crestwave
    Link
    I initially found part 2's instructions a bit confusing, but this was a cute one. Part 1 #!/usr/bin/awk -f function cycle(n,i) { for (i = 1; i <= n; ++i) { t += 1 if ((t - 20) % 40 == 0) sum += X...

    I initially found part 2's instructions a bit confusing, but this was a cute one.

    Part 1
    #!/usr/bin/awk -f
    function cycle(n,	i) {
    	for (i = 1; i <= n; ++i) {
    		t += 1
    		if ((t - 20) % 40 == 0)
    			sum += X * t
    	}
    }
    
    BEGIN { X = 1 }
    
    /noop/ { cycle(1) }
    /addx/ {
    	cycle(2)
    	X += $2
    }
    
    END { print(sum) }
    
    Part 2
    #!/usr/bin/awk -f
    function cycle(n,	i, j) {
    	for (i = 1; i <= n; ++i) {
    		j = t % 40
    
    		if (X == j - 1 || X == j || X == j + 1)
    			str = str "#"
    		else
    			str = str "."
    
    		if (j == 39)
    			str = str "\n"
    
    		t += 1
    	}
    }
    
    BEGIN { X = 1 }
    
    /noop/ { cycle(1) }
    /addx/ {
    	cycle(2)
    	X += $2
    }
    
    END { printf("%s", str) }
    
    2 votes
  2. Comment on Day 8: Treetop Tree House in ~comp.advent_of_code

    Crestwave
    Link
    Not particularly elegant but it works. :P Part 1 #!/usr/bin/awk -f BEGIN { FS = "" } { for (x = 1; x <= NF; ++x) grid[x, NR] = $x } END { X = length($0) Y = NR for (x = 1; x <= X; ++x) { i = -1...

    Not particularly elegant but it works. :P

    Part 1
    #!/usr/bin/awk -f
    BEGIN { FS = "" }
    
    {
    	for (x = 1; x <= NF; ++x)
    		grid[x, NR] = $x
    }
    
    END {
    	X = length($0)
    	Y = NR
    
    	for (x = 1; x <= X; ++x) {
    		i = -1
    		for (y = 1; y <= Y; ++y) {
    			if (grid[x, y] < i)
    				continue
    			if (grid[x, y] > i)
    				visible[x, y] = 1
    			i = grid[x, y]
    		}
    	}
    
    	for (x = 1; x <= X; ++x) {
    		i = -1
    		for (y = Y; y >= 1; --y) {
    			if (grid[x, y] < i)
    				continue
    			if (grid[x, y] > i)
    				visible[x, y] = 1
    			i = grid[x, y]
    		}
    	}
    
    	for (y = 1; y <= Y; ++y) {
    		i = -1
    		for (x = 1; x <= X; ++x) {
    			if (grid[x, y] < i)
    				continue
    			if (grid[x, y] > i)
    				visible[x, y] = 1
    			i = grid[x, y]
    		}
    	}
    
    	for (y = 1; y <= Y; ++y) {
    		i = -1
    		for (x = X; x >= 1; --x) {
    			if (grid[x, y] < i)
    				continue
    			if (grid[x, y] > i)
    				visible[x, y] = 1
    			i = grid[x, y]
    		}
    	}
    
    	for (i in visible)
    		sum += 1
    
    	print(sum)
    }
    
    Part 2
    #!/usr/bin/awk -f
    function sight(x, y, _x, _y, X, Y,	i, sum) {
    	i = grid[x, y]
    	sum = 0
    
    	do {
    		x += _x
    		do {
    			y += _y
    			if (grid[x, y] != "") {
    				sum += 1
    				if (grid[x, y] >= i)
    					return sum
    			}
    		} while (y != Y)
    	} while (x != X)
    
    	return sum
    }
    
    BEGIN { FS = "" }
    
    {
    	for (x = 1; x <= NF; ++x)
    		grid[x, NR] = $x
    }
    
    END {
    	X = length($0)
    	Y = NR
    
    	for (x = 1; x <= X; ++x)
    		for (y = 1; y <= Y; ++y) {
    			score = sight(x, y, 0, -1, x, 1-1) * \
    				 sight(x, y, 0, +1, x, Y+1) * \
    				 sight(x, y, -1, 0, 1-1, y) * \
    				 sight(x, y, +1, 0, X+1, y)
    
    			if (score > max)
    				max = score
    		}
    
    	print(max)
    }
    
    2 votes
  3. Comment on Day 7: No Space Left On Device in ~comp.advent_of_code

    Crestwave
    Link
    The aimless trawling in the input file is quite relatable—almost looks like my .bash_history at times! This was a fun one. There doesn't seem to be any trickery like repeated ls invocations in the...

    The aimless trawling in the input file is quite relatable—almost looks like my .bash_history at times!

    This was a fun one. There doesn't seem to be any trickery like repeated ls invocations in the same directory, so I took quite a few shortcuts in parsing. :P

    Part 1
    #!/usr/bin/awk -f
    /cd \// {
    	p = 0
    	s[p] = $3
    }
    
    /cd [a-z]/ { s[++p] = $3 }
    /cd \.\./ { delete s[p--] }
    
    /^[0-9]/ {
    	str = ""
    	for (i = 0; i <= p; ++i) {
    		str = str "/" s[i]
    		a[str] += $1
    	}
    }
    
    END {
    	for (i in a)
    		if (a[i] <= 100000)
    			sum += a[i]
    
    	print(sum)
    }
    
    Part 2
    #!/usr/bin/awk -f
    /cd \// {
    	p = 0
    	s[p] = $3
    }
    
    /cd [a-z]/ { s[++p] = $3 }
    /cd \.\./ { delete s[p--] }
    
    /^[0-9]/ {
    	str = ""
    	for (i = 0; i <= p; ++i) {
    		str = str "/" s[i]
    		a[str] += $1
    	}
    }
    
    END {
    	target = 30000000 - (70000000 - a["//"])
    	min = a["//"]
    
    	for (i in a)
    		if (a[i] >= target && a[i] < min)
    			min = a[i]
    
    	print(min)
    }
    
    4 votes
  4. Comment on Day 6: Tuning Trouble in ~comp.advent_of_code

  5. Comment on Day 6: Tuning Trouble in ~comp.advent_of_code

    Crestwave
    Link
    Quick and dirty because I am quite late. Part 1 #!/usr/bin/awk -f BEGIN { FS = "" } { for (i = 1; i <= NF; ++i) { a[$i] += 1 if (i >= 4) { if (a[$i]*a[$(i-1)]*a[$(i-2)]*a[$(i-3)] == 1) { print(i)...

    Quick and dirty because I am quite late.

    Part 1
    #!/usr/bin/awk -f
    BEGIN { FS = "" }
    {
    	for (i = 1; i <= NF; ++i) {
    		a[$i] += 1
    
    		if (i >= 4) {
    			if (a[$i]*a[$(i-1)]*a[$(i-2)]*a[$(i-3)] == 1) {
    				print(i)
    				exit
    			}
    
    			a[$(i-3)] -= 1
    		}
    	}
    }
    
    Part 2
    #!/usr/bin/awk -f
    BEGIN { FS = "" }
    {
    	for (i = 1; i <= NF; ++i) {
    		a[$i] += 1
    
    		if (i >= 14) {
    			for (j in a)
    				if (a[j] > 1)
    					break
    
    			if (a[j] < 2) {
    				print(i)
    				exit
    			}
    
    			a[$(i-13)] -= 1
    		}
    	}
    }
    
    2 votes
  6. Comment on Day 5: Supply Stacks in ~comp.advent_of_code

    Crestwave
    Link
    Nice warmup to some familiar AoC patterns. Part 1 #!/usr/bin/awk -f /[][]/ { input[NR] = $0 } /^ 1/ { for (i = 2; i < length($0); i += 4) { j = substr($0, i, 1) for (k = NR; k > 0; --k) { c =...

    Nice warmup to some familiar AoC patterns.

    Part 1
    #!/usr/bin/awk -f
    /[][]/ { input[NR] = $0 }
    
    /^ 1/ {
    	for (i = 2; i < length($0); i += 4) {
    		j = substr($0, i, 1)
    		for (k = NR; k > 0; --k) {
    			c = substr(input[k], i, 1)
    			if (c != " ") {
    				stack[j, ++stack[j]] = c
    			}
    		}
    	}
    }
    
    /move/ {
    	for (i = 1; i <= $2; ++i) {
    		stack[$6, ++stack[$6]] = stack[$4, stack[$4]--]
    	}
    }
    
    END {
    	for (i = 1; i <= j; ++i) {
    		printf("%s", stack[i, stack[i]])
    	}
    
    	print ""
    }
    
    Part 2
    #!/usr/bin/awk -f
    /[][]/ { input[NR] = $0 }
    
    /^ 1/ {
    	for (i = 2; i < length($0); i += 4) {
    		j = substr($0, i, 1)
    		for (k = NR; k > 0; --k) {
    			c = substr(input[k], i, 1)
    			if (c != " ") {
    				stack[j, ++stack[j]] = c
    			}
    		}
    	}
    }
    
    /move/ {
    	for (i = $2-1; i >= 0; --i) {
    		stack[$6, ++stack[$6]] = stack[$4, stack[$4]-i]
    	}
    
    	stack[$4] -= $2
    }
    
    END {
    	for (i = 1; i <= j; ++i) {
    		printf("%s", stack[i, stack[i]])
    	}
    
    	print ""
    }
    
    2 votes
  7. Comment on Day 4: Camp Cleanup in ~comp.advent_of_code

    Crestwave
    Link Parent
    Nah I had something even longer at first. I just took the time afterwards to distill it to a simpler form. :P

    Nah I had something even longer at first. I just took the time afterwards to distill it to a simpler form. :P

    1 vote
  8. Comment on Day 3: Rucksack Reorganization in ~comp.advent_of_code

    Crestwave
    Link Parent
    No problem, Bash is always fun to hack on. The brackets denote a regex character class (e.g., [0-9]) that matches any character in $b.

    No problem, Bash is always fun to hack on. The brackets denote a regex character class (e.g., [0-9]) that matches any character in $b.

    2 votes
  9. Comment on Day 3: Rucksack Reorganization in ~comp.advent_of_code

    Crestwave
    (edited )
    Link Parent
    Have you read the Pure Bash Bible? Although external commands may be fast at processing data, it is significantly more efficient to use builtins for small changes because you avoid the fork and...

    Have you read the Pure Bash Bible? Although external commands may be fast at processing data, it is significantly more efficient to use builtins for small changes because you avoid the fork and startup time. Especially when you would otherwise use command substitution and piping, as those are already costly by themselves.

    Some examples of simple substitutions you could make are printf -v str %s {a..z} instead of str=$(echo {a..z} | tr -d ' ' and upCase=${commonLetter^} instead of upCase=$(tr [:lower:] [:upper:]<<<$commonLetter).

    Beyond that, you can get creative with Bash's builtins and solve them in a completely different way. This may be a bit too arcane, but here's how I would solve it:

    Part 1
    #!/usr/bin/env bash
    while read -r line; do
    	a=${line::${#line}/2}
    	b=${line:${#line}/2}
    	c=${a//["${a//["$b"]}"]}
    	c=${c::1}
    
    	printf -v val %d "'$c"
    
    	# alternatively, use a [[ $c =~ [[:lower:]] ]] check
    	(( val -= (val >= 97 ? 96 : 38)))
    	(( sum += val ))
    done <"$1"
    
    printf '%s\n' "$sum"
    
    Part 2
    #!/usr/bin/env bash
    mapfile -t lines <"$1"
    
    for (( i = 0; i < ${#lines[@]}; i += 3 )); do
    	a=${lines[i]}
    	b=${lines[i+1]}
    	c=${lines[i+2]}
    	d=${a//["${a//["$b"]}"]}
    	d=${c//["${c//["$d"]}"]}
    	d=${d::1}
    
    	printf -v val %d "'$d"
    
    	(( val -= (val >= 97 ? 96 : 38)))
    	(( sum += val ))
    done
    
    printf '%s\n' "$sum"
    

    Both of these execute in ~0.010s on my machine. But if you want a more straightforward way to find the common letters, you can use Bash's associative arrays to do something like my AWK solution.

    EDIT - One easier method for part 1:

    Part 1
    #!/usr/bin/env bash
    while read -r line; do
    	a=${line::${#line}/2}
    	b=${line:${#line}/2}
    	[[ $a =~ [$b] ]]
    	c=${BASH_REMATCH[0]}
    
    	printf -v val %d "'$c"
    
    	(( val -= (val >= 97 ? 96 : 38)))
    	(( sum += val ))
    done <"$1"
    
    printf '%s\n' "$sum"
    
    3 votes
  10. Comment on Day 4: Camp Cleanup in ~comp.advent_of_code

    Crestwave
    Link
    Easier one than yesterday for those of us without sets. Tiny AWK solutions: Part 1 #!/usr/bin/awk -f BEGIN { FS = "[,-]" } { sum += (($1 >= $3 && $2 <= $4) || ($3 >= $1 && $4 <= $2)) } END {...

    Easier one than yesterday for those of us without sets. Tiny AWK solutions:

    Part 1
    #!/usr/bin/awk -f
    BEGIN { FS = "[,-]" }
    { sum += (($1 >= $3 && $2 <= $4) || ($3 >= $1 && $4 <= $2)) }
    END { print(sum) }
    
    Part 2
    #!/usr/bin/awk -f
    BEGIN { FS = "[,-]" }
    { sum += ($1 <= $4 && $3 <= $2) }
    END { print(sum) }
    
    4 votes
  11. Comment on Day 3: Rucksack Reorganization in ~comp.advent_of_code

    Crestwave
    Link
    Neat one. Part 1 #!/usr/bin/awk -f { split("", items) types = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" len = length($0) for (i = 1; i <= len; ++i) { c = substr($0, i, 1) if (i < len...

    Neat one.

    Part 1
    #!/usr/bin/awk -f
    {
    	split("", items)
    	types = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    	len = length($0)
    
    	for (i = 1; i <= len; ++i) {
    		c = substr($0, i, 1)
    		if (i < len / 2 + 1) {
    			items[c] = 1
    		} else if (items[c]) {
    			sum += match(types, c)
    			next
    		}
    	}
    }
    
    END { print(sum) }
    
    Part 2
    #!/usr/bin/awk -f
    {
    	group = ((NR - 1) % 3) + 1
    	types = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    	len = length($0)
    
    	if (group == 1)
    		split("", items)
    
    	for (i = 1; i <= len; ++i) {
    		c = substr($0, i, 1)
    		if (items[c] == group - 1) {
    			items[c] = group
    			if (group == 3)
    				sum += match(types, c)
    		}
    	}
    }
    
    END { print(sum) }
    
    2 votes
  12. Comment on Day 2: Rock Paper Scissors in ~comp.advent_of_code

    Crestwave
    Link
    Got a bit hacky with my calculations, but this was pretty fun overall. Part 1 #!/usr/bin/awk -f BEGIN { shape["A"] = 1 shape["B"] = 2 shape["C"] = 3 shape["X"] = 1 shape["Y"] = 2 shape["Z"] = 3...

    Got a bit hacky with my calculations, but this was pretty fun overall.

    Part 1
    #!/usr/bin/awk -f
    BEGIN {
            shape["A"] = 1
            shape["B"] = 2
            shape["C"] = 3
            shape["X"] = 1
            shape["Y"] = 2
            shape["Z"] = 3
    
            outcome[0] = 3
            outcome[1] = 6
            outcome[2] = 0
    }
    
    { score += outcome[((shape[$2]-shape[$1])+3)%3] + shape[$2] }
    
    END { print(score) }
    
    Part 2
    #!/usr/bin/awk -f
    BEGIN {
            shape["A"] = 1
            shape["B"] = 2
            shape["C"] = 3
    }
    
    {
            if ($2 == "X")
                    score += ((shape[$1]-1)+2) % 3 + 1
            else if ($2 == "Y")
                    score += shape[$1] + 3
            else if ($2 == "Z")
                    score += ((shape[$1]) % 3) + 6 + 1
    }
    
    END { print(score) }
    
    4 votes
  13. Comment on Day 1: Calorie Counting in ~comp.advent_of_code

    Crestwave
    Link
    POSIX AWK as usual. Maybe I'll try another language mid-way. Part 1 #!/usr/bin/awk -f /^$/ { i += 1 } { arr[i] += $1 } END { for (i in arr) if (arr[i] > max) max = arr[i] print(max) } Part 2...

    POSIX AWK as usual. Maybe I'll try another language mid-way.

    Part 1
    #!/usr/bin/awk -f
    /^$/ { i += 1 }
    { arr[i] += $1 }
    
    END {
    	for (i in arr)
    		if (arr[i] > max)
    			max = arr[i]
    
    	print(max)
    }
    
    Part 2
    #!/usr/bin/awk -f
    /^$/ { i += 1 }
    { arr[i] += $1 }
    
    END {
    	for (i in arr) {
    		if (arr[i] > max[0]) {
    			max[2] = max[1]
    			max[1] = max[0]
    			max[0] = arr[i]
    		} else if (arr[i] > max[1]) {
    			max[2] = max[1]
    			max[1] = arr[i]
    		} else if (arr[i] > max[2]) {
    			max[2] = arr[i]
    		}
    	}
    
    	print(max[0] + max[1] + max[2])
    }
    
    1 vote
  14. Comment on Tildes Game Giveaway Thread: June/July 2022 in ~games

    Crestwave
    Link Parent
    I'd love to try Trine Enchanted Edition, thanks!

    I'd love to try Trine Enchanted Edition, thanks!

    1 vote
  15. Comment on Tildes Game Giveaway Thread: June/July 2022 in ~games

    Crestwave
    Link Parent
    Note that @opheron is also offering Slay the Spire

    Note that @opheron is also offering Slay the Spire

    3 votes
  16. Comment on GitHub will require two-factor authentication (2FA) for all users who contribute code by the end of 2023 in ~comp

    Crestwave
    Link Parent
    GitHub supports 2FA via TOTP; presumably it should suffice for this requirement.

    GitHub supports 2FA via TOTP; presumably it should suffice for this requirement.

    6 votes
  17. Comment on What are some good mobile games which doesn't require dragging? in ~games

    Crestwave
    Link
    Shattered Pixel Dungeon (F-Droid) One of my favorite roguelikes ever; it's actively developed, very well-suited to mobile, and it's open source!

    Shattered Pixel Dungeon (F-Droid)
    One of my favorite roguelikes ever; it's actively developed, very well-suited to mobile, and it's open source!

    7 votes
  18. Comment on <deleted topic> in ~tech

    Crestwave
    (edited )
    Link Parent
    Curious, why not? They knowingly and intentionally distributed malware and went to lengths to cover it up. The main problem lies at the heart of the node ecosystem*, but I don't see how this is...

    What a trainwreck. Hopefully the repository author faces no repercussions though.

    Curious, why not? They knowingly and intentionally distributed malware and went to lengths to cover it up. The main problem lies at the heart of the node ecosystem*, but I don't see how this is "chaotic good".

    * As a side note, it's annoying how people keep pinning this on open source; if you see a headline about a library being maliciously updated, you already know it's npm.

    4 votes
  19. Comment on Steam Deck launch day megathread in ~games

  20. Comment on What small purchase has vastly improved your life? in ~life

    Crestwave
    Link Parent
    To clarify, it is possible to block them as they are usually served through a different subdomain. However, you can't use blanket wildcards for this without blocking the actual video playback as...

    To clarify, it is possible to block them as they are usually served through a different subdomain. However, you can't use blanket wildcards for this without blocking the actual video playback as well, so the subdomains have to be blocked individually. These are not static and change regularly, and also varies between regions, which is why blocking is very inconsistent.

    Pi-hole should be able to achieve anything any blocking NextDNS can with the same blocklists.

    5 votes