-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.yml
73 lines (62 loc) · 2.71 KB
/
main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
---
- name: Split input into separate rucksacks
ansible.builtin.set_fact:
rucksacks: "{{ day3_input | trim | split('\n') }}"
- name: Split the rucksacks into two halves
tags: part1
ansible.builtin.set_fact:
rucksack_compartments: "{{ rucksack_compartments | default([]) + [[first_half, second_half]] }}"
vars:
half_len: "{{ (item | length | int / 2) | int }}"
regex_first_half: "{{ '^' + ('.' * half_len | int) }}"
regex_second_half: "{{ ('.' * half_len | int) + '$' }}"
first_half: "{{ item | regex_replace(regex_second_half, '') }}"
second_half: "{{ item | regex_replace(regex_first_half, '') }}"
loop: "{{ rucksacks }}"
- name: Get character that occurs in both halves
tags: part1
ansible.builtin.set_fact:
rucksack_characters: "{{ rucksack_characters | default([]) + [item.0 | list | intersect(item.1 | list) | first] }}"
loop: "{{ rucksack_compartments }}"
- name: Return the characters
tags: part1
ansible.builtin.debug:
msg: "{{ rucksack_characters | join('') }}"
- name: Loop through all letters in the alphabet and set their priorities
ansible.builtin.set_fact:
rucksack_priorities: "{{ rucksack_priorities | default({}) | combine({item: index + 1}) }}"
loop: "{{ 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | list }}"
loop_control:
index_var: index
- name: Get the total priorities of all double letters
tags: part1
ansible.builtin.set_fact:
rucksack_total_priorities: "{{ rucksack_total_priorities | default(0) | int + rucksack_priorities[item] | int }}"
loop: "{{ rucksack_characters }}"
- name: ANSWER TO PART 1 | Return the total priorities of all double letters
tags: part1
ansible.builtin.debug:
msg: "{{ rucksack_total_priorities }}"
- name: Split the elves into groups of three
tags: part2
ansible.builtin.set_fact:
rucksack_groups: "{{ rucksack_groups | default([]) + [item] }}"
loop: "{{ rucksacks | list | batch(3) }}"
- name: Get letter that occurs in all three elves
tags: part2
ansible.builtin.set_fact:
rucksack_characters: "{{ rucksack_characters | default([]) + [item.0 | list | intersect(item.1 | list) | intersect(item.2 | list) | first] }}"
loop: "{{ rucksack_groups }}"
- name: Return the characters from each group
tags: part2
ansible.builtin.debug:
msg: "{{ rucksack_characters | join('') }}"
- name: Get the total priorities of all triple letters
tags: part2
ansible.builtin.set_fact:
rucksack_total_priorities: "{{ rucksack_total_priorities | default(0) | int + rucksack_priorities[item] | int }}"
loop: "{{ rucksack_characters }}"
- name: ANSWER TO PART 2 | Return the total priorities of all triple letters
tags: part2
ansible.builtin.debug:
msg: "{{ rucksack_total_priorities }}"