Skip to content

Instantly share code, notes, and snippets.

@godeater
Created December 4, 2023 09:45
Show Gist options
  • Save godeater/5b6a2f2a0550399735a30b928437eda4 to your computer and use it in GitHub Desktop.
Save godeater/5b6a2f2a0550399735a30b928437eda4 to your computer and use it in GitHub Desktop.
A note quite working Day 4 part 1 for 2023 advent of code in Powershell. Passes the sample data, but not the real input - comes out too high.
param(
[Parameter(Mandatory=$false)]
[bool]$sample = $false
)
Import-Module $PSScriptRoot/../_lib/aoc.psm1
$input_file = @(
"Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53",
"Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19",
"Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1",
"Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83",
"Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36",
"Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11"
)
Function Solve {
param(
[string[]]$input_file
)
$count = 0
[int[]]$score = New-Object int[] 199
$input_file | ForEach-Object {
$line = $_
$winners = 0
$line -match "^Card\W+(?<card>\d+): (?<win>[\d ]+) \| (?<nums>[\d ]+)$" | Out-Null
[int[]]$number_winners = $Matches['win'] -split '\s+' | ForEach-Object { [int]$_ } | Sort-Object
[int[]]$numbers = $Matches['nums'] -split '\s+' | ForEach-Object { [int]$_ } | Sort-Object
$numset=[Collections.Generic.HashSet[int]]::new($numbers)
$winners = $($number_winners.Where({$numset.Contains($_)})).Count
$count++
if($winners -gt 0){
$line_score = [Math]::Pow(2, $winners - 1)
$score[$count] = $line_score
}
}
$sum = $($score | Measure-Object -Sum).Sum
Write-Host "Score : $sum"
}
Function Main {
param(
[bool]$sample
)
#$puzzle_input = $input_file #Get-AoCInput -Sample $sample -input_path $PSScriptRoot
$puzzle_input = Get-AoCInput -Sample $sample -input_path $PSScriptRoot
Solve -input_file $puzzle_input
}
Main -sample:$sample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment