Skip to content

Instantly share code, notes, and snippets.

@smortex
Created December 2, 2022 17:36
Show Gist options
  • Save smortex/42f79bc7db8d0344338affd5027e1276 to your computer and use it in GitHub Desktop.
Save smortex/42f79bc7db8d0344338affd5027e1276 to your computer and use it in GitHub Desktop.
Advent of Code 2022 - Day 2
$data = file('aoc/2022/day2.txt')
$rounds = $data.split('\n').map |$line| { [$line[0], $line[2]] }
$scores1 = $rounds.map |$round| {
$shape_score = $round[1] ? {
'X' => 1,
'Y' => 2,
'Z' => 3,
}
$outcome_score = $round ? {
['A', 'X'] => 3,
['A', 'Y'] => 6,
['A', 'Z'] => 0,
['B', 'X'] => 0,
['B', 'Y'] => 3,
['B', 'Z'] => 6,
['C', 'X'] => 6,
['C', 'Y'] => 0,
['C', 'Z'] => 3,
}
$shape_score + $outcome_score
}
$scores2 = $rounds.map |$round| {
$outcome_score = $round[1] ? {
'X' => 0,
'Y' => 3,
'Z' => 6,
}
$shape_score = $round ? {
['A', 'X'] => 3,
['A', 'Y'] => 1,
['A', 'Z'] => 2,
['B', 'X'] => 1,
['B', 'Y'] => 2,
['B', 'Z'] => 3,
['C', 'X'] => 2,
['C', 'Y'] => 3,
['C', 'Z'] => 1,
}
$shape_score + $outcome_score
}
warning($scores1.reduce |$memo, $score| { $memo + $score })
warning($scores2.reduce |$memo, $score| { $memo + $score })
@pier4r
Copy link

pier4r commented Dec 2, 2022

Is this with puppet bolt or puppet agent? it would be nice to know the difference.

@smortex
Copy link
Author

smortex commented Dec 2, 2022

@pier4r I run it through bolt for convenience but language is the same so this can also run with puppet:

romain@zappy ~/Projects/aoc % bolt apply day2.pp -t localhost
Starting: install puppet and gather facts on localhost
Finished: install puppet and gather facts with 0 failures in 2.8 sec
Starting: apply catalog on localhost
localhost: Scope(Class[main]): 13809
localhost: Scope(Class[main]): 12316
Started on localhost...
Finished on localhost:
  changed: 0, failed: 0, unchanged: 0 skipped: 0, noop: 0
Finished: apply catalog with 0 failures in 4.44 sec
Successful on 1 target: localhost
Ran on 1 target in 7.29 sec
romain@zappy ~/Projects/aoc % puppet apply day2.pp --modulepath $PWD/..
Warning: Scope(Class[main]): 13809
Warning: Scope(Class[main]): 12316
Notice: Compiled catalog for zappy.blogreen.org in environment production in 0.81 seconds
Notice: Applied catalog in 0.01 seconds

The ideal would in fact be a plan, so that we do not need to pass an unneeded target 😄 :

romain@zappy ~/Projects/aoc % mkdir plans
romain@zappy ~/Projects/aoc % echo 'plan aoc::day2 {' > plans/day2.pp
romain@zappy ~/Projects/aoc % cat day2.pp >> plans/day2.pp
romain@zappy ~/Projects/aoc % echo '}' >> plans/day2.pp
romain@zappy ~/Projects/aoc % bolt plan run aoc::day2
Starting: plan aoc::day2
13809
12316
Finished: plan aoc::day2 in 0.8 sec
Plan completed successfully with no result

@pier4r
Copy link

pier4r commented Dec 2, 2022

I really have to use bolt, I thought it was a tad different but it seems like scriptable puppet. I am losing opportunities there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment