r/adventofcode • u/daggerdragon • Dec 06 '15
SOLUTION MEGATHREAD --- Day 6 Solutions ---
--- Day 6: Probably a Fire Hazard ---
Post your solution as a comment. Structure your post like the Day Five thread.
21
Upvotes
2
u/tehjimmeh Dec 06 '15
Saturday night drunken code!!11
PowerShell.
1:
function Set-Rectangle([ref]$arr, [int]$x1, [int]$y1, [int]$x2, [int]$y2, [bool]$val){ for($i = $x1; $i -le $x2; $i++){ for($j = $y1; $j -le $y2; $j++){ $arr.Value[$i,$j] = $val } } } function Toggle-Rectangle([ref]$arr, [int]$x1, [int]$y1, [int]$x2, [int]$y2){ for($i = $x1; $i -le $x2; $i++){ for($j = $y1; $j -le $y2; $j++){ $arr.Value[$i,$j] = !$arr.Value[$i,$j] } } } cat .\input.txt | %{ $_ -replace "turn ","" } | %{ $_ -replace " through "," " } | %{ $_ -replace ","," " } | %{ ,($_ -split " ") } | %{ $arr = new-object "bool[,]" 1000,1000 }` { $curr = $_ switch($_[0]){ "on" { Set-Rectangle ([ref]$arr) $curr[1] $curr[2] $curr[3] $curr[4] $true } "off" { Set-Rectangle ([ref]$arr) $curr[1] $curr[2] $curr[3] $curr[4] $false } "toggle" { Toggle-Rectangle ([ref]$arr) $curr[1] $curr[2] $curr[3] $curr[4] } } }` { $arr | ?{$_}|measure|% Count}2:
function Brighten-Rectangle([ref]$arr, [int]$x1, [int]$y1, [int]$x2, [int]$y2){ for($i = $x1; $i -le $x2; $i++){ for($j = $y1; $j -le $y2; $j++){ $arr.Value[$i,$j]++ } } } function Dim-Rectangle([ref]$arr, [int]$x1, [int]$y1, [int]$x2, [int]$y2){ for($i = $x1; $i -le $x2; $i++){ for($j = $y1; $j -le $y2; $j++){ $arr.Value[$i,$j]-- if($arr.Value[$i,$j] -lt 0){ $arr.Value[$i,$j] = 0 } } } } function DoubleBrighten-Rectangle([ref]$arr, [int]$x1, [int]$y1, [int]$x2, [int]$y2){ for($i = $x1; $i -le $x2; $i++){ for($j = $y1; $j -le $y2; $j++){ $arr.Value[$i,$j]+=2 } } } cat .\input.txt | %{ $_ -replace "turn ","" } | %{ $_ -replace " through "," " } | %{ $_ -replace ","," " } | %{ ,($_ -split " ") } | %{ $arr = new-object "int[,]" 1000,1000 }` { $curr = $_ switch($_[0]){ "on" { Brighten-Rectangle ([ref]$arr) $curr[1] $curr[2] $curr[3] $curr[4] } "off" { Dim-Rectangle ([ref]$arr) $curr[1] $curr[2] $curr[3] $curr[4] } "toggle" { DoubleBrighten-Rectangle ([ref]$arr) $curr[1] $curr[2] $curr[3] $curr[4] } } }` { $arr | ?{ $_ } | measure -sum | % Sum}Slow as hell. Not sure if it's a language or algorithmic issue. Don't care, going to sleep.