Skip to content

Instantly share code, notes, and snippets.

@bielawb
Created December 29, 2021 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bielawb/cffac2c7556f35db26eb1dbae309446c to your computer and use it in GitHub Desktop.
Save bielawb/cffac2c7556f35db26eb1dbae309446c to your computer and use it in GitHub Desktop.
Advent of Code 2021 - Day 24 - part 2
[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()
$getX =
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 -lt 0) {
$serial[$pair.w2 - 1] = 1
$serial[$pair.w1 - 1] = 1 - $pair.diff
} else {
$serial[$pair.w2 - 1] = 1 + $pair.diff
$serial[$pair.w1 - 1] = 1
}
}
-join $serial
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment