-
-
Save bielawb/054f52adc078bbe896c6691fec11c979 to your computer and use it in GitHub Desktop.
Advent of Code 2021 - Day 24 - part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
param( | |
[Parameter()] | |
[String]$Path = ".\2021-12-24.txt" | |
) | |
$Data = @((Get-Content -Path $Path -Raw) -split 'inp w') | |
class round { | |
[int] $w | |
[int] $y | |
[int] $x | |
[int] $div | |
} | |
class pair { | |
[int]$w1 | |
[int]$w2 | |
[int]$diff | |
} | |
$rounds = [System.Collections.ArrayList]::new() | |
for ($w = 1; $w -lt $Data.Count; $w++) { | |
$string = $Data[$w] | |
if ($string -match '(?m)^add x (-?\d+)') { | |
$x = $Matches[1] | |
} | |
if ($string -match '(?ms)^add y w\s*add y (-?\d+)') { | |
$y = $Matches[1] | |
} | |
if ($string -match '(?m)^div z (\d+)') { | |
$div = $Matches[1] | |
} | |
$null = $rounds.Add( | |
[round]@{ | |
w = $w | |
x = $x | |
y = $y | |
div = $div | |
} | |
) | |
} | |
$add = [System.Collections.ArrayList]::new() | |
$pairs = [System.Collections.ArrayList]::new() | |
foreach ($item in $rounds) { | |
if ($item.div -eq 1) { | |
$null = $add.Add($item) | |
} else { | |
$pair = $add[-1] | |
$add.Remove($pair) | |
$null = $pairs.Add( | |
[pair]@{ | |
w1 = $item.w | |
w2 = $pair.w | |
diff = - $pair.y - $item.x | |
} | |
) | |
} | |
} | |
$serial = [int[]]::new(14) | |
foreach ($pair in $pairs) { | |
if ($pair.diff -gt 0) { | |
$serial[$pair.w2 - 1] = 9 | |
$serial[$pair.w1 - 1] = 9 - $pair.diff | |
} else { | |
$serial[$pair.w2 - 1] = 9 + $pair.diff | |
$serial[$pair.w1 - 1] = 9 | |
} | |
} | |
-join $serial |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment