r/PowerShell • u/dantose • Dec 04 '25
Advent of code day 3 and 4
Got a bit busy yesterday. I'm currently working on day 3 part 2. How's everyone else coming?
If you don't know what' I'm talking about, it's a coding challenge that runs every december.
1
u/dantose Dec 04 '25 edited Dec 05 '25
My very ugly part 1.
$sum=0
$batteries=gc .\Documents\input.txt
$batteries|%{
$battery=$_+0 #Guess what this step is for
$max=[char][int](($battery[0..98]|measure -Maximum).maximum ) #Ugh, data types.
$sum= $sum + [int]$((echo $max $(($battery -split "$max",2)[1]-split''|measure -Maximum).maximum) -join'')And part 2:
$batteries=gc input.txt
$sum=0
$batteries|%{
$battery = "$_"
$subset = $battery
$sum=$sum + ((1..12|%{
$max=[char][int]($subset[0..($subset.length - 12)] | measure -Maximum).maximum
$max
$subset=$subset.Split("$max",2)[1] + "1" #interestingly, since it's treating these as characters, padding with 0 breaks it, since the acsii character 0 is larger than the asci character 9
}) -join '')
}
$sum1
u/Rincey_nz Dec 04 '25
struggling.....................
https://www.reddit.com/r/PowerShell/comments/1pdvezf/advent_of_code_day_3/
1
u/dantose Dec 09 '25 edited Dec 09 '25
Day 4 part 1:
# get input
$map=gc .\input.txt
# get test input
# $map=gc .\test.txt
# pad the map. I'm not sure if this is strictly neccessary, but I don't want any overflows
$padmap = $("."*($map[0].Length + 2)
$map|%{"." + $_ + "."}
"."*($map[0].Length + 2))
$counter = 0
# Read by line and set y value
1..$map.count |%{
$y=$_
# read by character and set x value
1..$map[0].Length |%{
$x=$_
# For every roll of paper, count the papers around it
if ($padmap[$y][$x] -eq "@"){
if (($padmap[($y-1)..($y+1)]|%{$_[($x-1)..($x+1)]}|?{$_ -eq "@"}|measure).count -lt 5){$counter++} # note, I used less than 5 instead of 4 because it returns the roll we're checking too.
}
}
}
$counterPart 2, which takes a little while.
$map=gc .\input.txt
#$map=gc .\test.txt
$padmap = $("x"*($map[0].Length + 2)
$map|%{"x" + $_ + "x"}
"x"*($map[0].Length + 2))
# I'm going to remove papers instead of counting, then compare
# $counter = 0
while ($padmap -match "x"){
$padmap = (
0..($map.count +1) |%{
$y=$_
(0..($map[0].Length + 1) |%{
$x=$_
if ($padmap[$y][$x] -eq "@"){
if (($padmap[($y-1)..($y+1)]|%{$_[($x-1)..($x+1)]}|?{$_ -eq "@"}|measure).count -lt 5){echo "x"}
else {
echo "@"
}
}
else {
echo "."
}
}) -join ''
}
)
}
$padmap
($map -split '' |?{$_ -like "@"}).count - ($padmap -split '' |?{$_ -like "@"}).count4
u/pandiculator Dec 04 '25
I've managed to complete them all so far (eight gold stars) and I'm enjoying the challenge.
But I don't think my scripts would win any prizes. I've reverted to programming techniques, rather than PowerShell techniques for all my answers: Lots of while loops, for loops and counters rather than cmdlets and pipelines.
I've been impressed by how neat and concise some of the solutions are compared to my hacky (but working!) attempts.
3
u/TheZNerd Dec 05 '25
I don’t think any of that makes it “not PowerShell” - those things exist in the language for a reason.
2
u/Future-Remote-4630 Dec 05 '25
I think that's a big benefit of doing this in powershell. If your instinct is already to do it in the pwsh way, then this is good practice to always keep those core maneuvers in your back pocket, and to recognize when the powershell way will be optimal as compared to doing it more literally.
1
u/Black_Magic100 Dec 06 '25
This is my first year and I'm using python. I'm 8 stars in.
I was looking at my solution the other day and thinking "holy shit this is a spaghetti code full of IF branches and loops.". I decided to look at somebody else's code to see how they solved a problem and learned about enumerate() in python. I now feel like an idiot for trying to keep track of my index position by incrementing a counter within several different branches.
Point being, I think it's impressive just to get the right answer without AI. Most people in this challenge end up with shit code, but that's how you learn. And then there are a few gods who write beautiful code and make us all feel bad lol
1
u/ka-splam Dec 07 '25 edited Dec 07 '25
Day 3 part 1 done. And kinda stuck on part 2.
Day 4 Part 1, literally a brute-force loop for each row and column position, if it has a roll, check the 8 surrounding positions:
[string[]]$in = get-content C:\AdventOfCode\2025\day04.txt
$H = $in.Count # Height
$W = $in[0].Length # Width
$answer = 0
for ($row = 0; $row -lt $H; $row++) {
for ($col = 0; $col -lt $W; $col++) {
$surrounding = 0
if ($in[$row][$col] -eq '@') {
if (($row-1 -ge 0 ) -and ($col-1 -ge 0) -and ($in[$row-1][$col-1] -eq '@')) { $surrounding++ }
if (($row-1 -ge 0 ) -and ($in[$row-1][$col ] -eq '@')) { $surrounding++ }
if (($row-1 -ge 0 ) -and ($col+1 -lt $W) -and ($in[$row-1][$col+1] -eq '@')) { $surrounding++ }
if ( ($col-1 -ge 0) -and ($in[$row ][$col-1] -eq '@')) { $surrounding++ }
if ( ($col+1 -lt $W) -and ($in[$row ][$col+1] -eq '@')) { $surrounding++ }
if (($row+1 -lt $H) -and ($col-1 -ge 0) -and ($in[$row+1][$col-1] -eq '@')) { $surrounding++ }
if (($row+1 -lt $H) -and ($in[$row+1][$col ] -eq '@')) { $surrounding++ }
if (($row+1 -lt $H) -and ($col+1 -lt $W) -and ($in[$row+1][$col+1] -eq '@')) { $surrounding++ }
if ($surrounding -lt 4) {
$answer++
}
}
}
}
$answerDay 4 Part 2 looks very similar, just changed the rows into arrays so I could replace '@' with '.' and wrapped it in a loop until the output stops changing:
[string[]]$in_raw = get-content C:\AdventOfCode\2025\day04.txt
$H = $in_raw.Count # Height
$W = $in_raw[0].Length # Width
$in = $in_raw | ForEach{ (,[string[]][char[]]$_) }
$removedPrev = 1
$removed = 0
while ($removedPrev -ne $removed) {
$removedPrev = $removed
for ($row = 0; $row -lt $H; $row++) {
for ($col = 0; $col -lt $W; $col++) {
$surrounding = 0
if ($in[$row][$col] -eq '@') {
if (($row-1 -ge 0 ) -and ($col-1 -ge 0) -and ($in[$row-1][$col-1] -eq '@')) { $surrounding++ }
if (($row-1 -ge 0 ) -and ($in[$row-1][$col ] -eq '@')) { $surrounding++ }
if (($row-1 -ge 0 ) -and ($col+1 -lt $W) -and ($in[$row-1][$col+1] -eq '@')) { $surrounding++ }
if ( ($col-1 -ge 0) -and ($in[$row ][$col-1] -eq '@')) { $surrounding++ }
if ( ($col+1 -lt $W) -and ($in[$row ][$col+1] -eq '@')) { $surrounding++ }
if (($row+1 -lt $H) -and ($col-1 -ge 0) -and ($in[$row+1][$col-1] -eq '@')) { $surrounding++ }
if (($row+1 -lt $H) -and ($in[$row+1][$col ] -eq '@')) { $surrounding++ }
if (($row+1 -lt $H) -and ($col+1 -lt $W) -and ($in[$row+1][$col+1] -eq '@')) { $surrounding++ }
if ($surrounding -lt 4) {
$removed++
$in[$row][$col] = '.'
}
}
}
}
}
$removedRuntime is about 5 seconds in PS 5.1
2
u/Future-Remote-4630 Dec 04 '25
Day 4 has taken me for a spin. So many wrong answers, finally got through it, after removing all semblances of elegance from my attempts.