Using Applicatives in day 2
[advent-of-code-23.git] / problems / day04.html
1 <!DOCTYPE html>
2 <html lang="en-us">
3 <head>
4 <meta charset="utf-8"/>
5 <title>Day 4 - Advent of Code 2023</title>
6 <link rel="stylesheet" type="text/css" href="/static/style.css?31"/>
7 <link rel="stylesheet alternate" type="text/css" href="/static/highcontrast.css?1" title="High Contrast"/>
8 <link rel="shortcut icon" href="/favicon.png"/>
9 <script>window.addEventListener('click', function(e,s,r){if(e.target.nodeName==='CODE'&&e.detail===3){s=window.getSelection();s.removeAllRanges();r=document.createRange();r.selectNodeContents(e.target);s.addRange(r);}});</script>
10 </head><!--
11
12
13
14
15 Oh, hello! Funny seeing you here.
16
17 I appreciate your enthusiasm, but you aren't going to find much down here.
18 There certainly aren't clues to any of the puzzles. The best surprises don't
19 even appear in the source until you unlock them for real.
20
21 Please be careful with automated requests; I'm not a massive company, and I can
22 only take so much traffic. Please be considerate so that everyone gets to play.
23
24 If you're curious about how Advent of Code works, it's running on some custom
25 Perl code. Other than a few integrations (auth, analytics, social media), I
26 built the whole thing myself, including the design, animations, prose, and all
27 of the puzzles.
28
29 The puzzles are most of the work; preparing a new calendar and a new set of
30 puzzles each year takes all of my free time for 4-5 months. A lot of effort
31 went into building this thing - I hope you're enjoying playing it as much as I
32 enjoyed making it for you!
33
34 If you'd like to hang out, I'm @ericwastl@hachyderm.io on Mastodon and
35 @ericwastl on Twitter.
36
37 - Eric Wastl
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 -->
89 <body>
90 <header><div><h1 class="title-global"><a href="/">Advent of Code</a></h1><nav><ul><li><a href="/2023/about">[About]</a></li><li><a href="/2023/events">[Events]</a></li><li><a href="https://teespring.com/stores/advent-of-code" target="_blank">[Shop]</a></li><li><a href="/2023/settings">[Settings]</a></li><li><a href="/2023/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <a href="/2023/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a> <span class="star-count">8*</span></div></div><div><h1 class="title-event">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="title-event-wrap"></span><a href="/2023">2023</a><span class="title-event-wrap"></span></h1><nav><ul><li><a href="/2023">[Calendar]</a></li><li><a href="/2023/support">[AoC++]</a></li><li><a href="/2023/sponsors">[Sponsors]</a></li><li><a href="/2023/leaderboard">[Leaderboard]</a></li><li><a href="/2023/stats">[Stats]</a></li></ul></nav></div></header>
91
92 <div id="sidebar">
93 <div id="sponsor"><div class="quiet">Our <a href="/2023/sponsors">sponsors</a> help make Advent of Code possible:</div><div class="sponsor"><a href="https://canvasapp.com/?ref=aoc2023" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">Canvas</a> - Grok your startup.</div></div>
94 </div><!--/sidebar-->
95
96 <main>
97 <article class="day-desc"><h2>--- Day 4: Scratchcards ---</h2><p>The gondola takes you up. Strangely, though, the ground doesn't seem to be coming with you; you're not climbing a mountain. As the circle of Snow Island recedes below you, an entire new landmass suddenly appears above you! The gondola carries you to the surface of the new island and lurches into the station.</p>
98 <p>As you exit the gondola, the first thing you notice is that the air here is much <em>warmer</em> than it was on Snow Island. It's also quite <em>humid</em>. Is this where the water source is?</p>
99 <p>The next thing you notice is an Elf sitting on the floor across the station in what seems to be a pile of colorful square cards.</p>
100 <p>"Oh! Hello!" The Elf excitedly runs over to you. "How may I be of service?" You ask about water sources.</p>
101 <p>"I'm not sure; I just operate the gondola lift. That does sound like something we'd have, though - this is <em><span title="I find this way funnier than it has any right to be.">Island Island</span></em>, after all! I bet the <em>gardener</em> would know. He's on a different island, though - er, the small kind surrounded by water, not the floating kind. We really need to come up with a better naming scheme. Tell you what: if you can help me with something quick, I'll let you <em>borrow my boat</em> and you can go visit the gardener. I got all these <a href="https://en.wikipedia.org/wiki/Scratchcard" target="_blank">scratchcards</a> as a gift, but I can't figure out what I've won."</p>
102 <p>The Elf leads you over to the pile of colorful cards. There, you discover dozens of scratchcards, all with their opaque covering already scratched off. Picking one up, it looks like each card has two lists of numbers separated by a vertical bar (<code>|</code>): a list of <em>winning numbers</em> and then a list of <em>numbers you have</em>. You organize the information into a table (your puzzle input).</p>
103 <p>As far as the Elf has been able to figure out, you have to figure out which of the <em>numbers you have</em> appear in the list of <em>winning numbers</em>. The first match makes the card worth <em>one point</em> and each match after the first <em>doubles</em> the point value of that card.</p>
104 <p>For example:</p>
105 <pre><code>Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
106 Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
107 Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
108 Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
109 Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
110 Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
111 </code></pre>
112 <p>In the above example, card 1 has five winning numbers (<code>41</code>, <code>48</code>, <code>83</code>, <code>86</code>, and <code>17</code>) and eight numbers you have (<code>83</code>, <code>86</code>, <code>6</code>, <code>31</code>, <code>17</code>, <code>9</code>, <code>48</code>, and <code>53</code>). Of the numbers you have, four of them (<code>48</code>, <code>83</code>, <code>17</code>, and <code>86</code>) are winning numbers! That means card 1 is worth <code><em>8</em></code> points (1 for the first match, then doubled three times for each of the three matches after the first).</p>
113 <ul>
114 <li>Card 2 has two winning numbers (<code>32</code> and <code>61</code>), so it is worth <code><em>2</em></code> points.</li>
115 <li>Card 3 has two winning numbers (<code>1</code> and <code>21</code>), so it is worth <code><em>2</em></code> points.</li>
116 <li>Card 4 has one winning number (<code>84</code>), so it is worth <code><em>1</em></code> point.</li>
117 <li>Card 5 has no winning numbers, so it is worth no points.</li>
118 <li>Card 6 has no winning numbers, so it is worth no points.</li>
119 </ul>
120 <p>So, in this example, the Elf's pile of scratchcards is worth <code><em>13</em></code> points.</p>
121 <p>Take a seat in the large pile of colorful cards. <em>How many points are they worth in total?</em></p>
122 </article>
123 <p>Your puzzle answer was <code>17803</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>Just as you're about to report your findings to the Elf, one of you realizes that the rules have actually been printed on the back of every card this whole time.</p>
124 <p>There's no such thing as "points". Instead, scratchcards only cause you to <em>win more scratchcards</em> equal to the number of winning numbers you have.</p>
125 <p>Specifically, you win <em>copies</em> of the scratchcards below the winning card equal to the number of matches. So, if card 10 were to have 5 matching numbers, you would win one copy each of cards 11, 12, 13, 14, and 15.</p>
126 <p>Copies of scratchcards are scored like normal scratchcards and have the <em>same card number</em> as the card they copied. So, if you win a copy of card 10 and it has 5 matching numbers, it would then win a copy of the same cards that the original card 10 won: cards 11, 12, 13, 14, and 15. This process repeats until none of the copies cause you to win any more cards. (Cards will never make you copy a card past the end of the table.)</p>
127 <p>This time, the above example goes differently:</p>
128 <pre><code>Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
129 Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
130 Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
131 Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
132 Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
133 Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
134 </code></pre>
135 <ul>
136 <li>Card 1 has four matching numbers, so you win one copy each of the next four cards: cards 2, 3, 4, and 5.</li>
137 <li>Your original card 2 has two matching numbers, so you win one copy each of cards 3 and 4.</li>
138 <li>Your copy of card 2 also wins one copy each of cards 3 and 4.</li>
139 <li>Your four instances of card 3 (one original and three copies) have two matching numbers, so you win <em>four</em> copies each of cards 4 and 5.</li>
140 <li>Your eight instances of card 4 (one original and seven copies) have one matching number, so you win <em>eight</em> copies of card 5.</li>
141 <li>Your fourteen instances of card 5 (one original and thirteen copies) have no matching numbers and win no more cards.</li>
142 <li>Your one instance of card 6 (one original) has no matching numbers and wins no more cards.</li>
143 </ul>
144 <p>Once all of the originals and copies have been processed, you end up with <code><em>1</em></code> instance of card 1, <code><em>2</em></code> instances of card 2, <code><em>4</em></code> instances of card 3, <code><em>8</em></code> instances of card 4, <code><em>14</em></code> instances of card 5, and <code><em>1</em></code> instance of card 6. In total, this example pile of scratchcards causes you to ultimately have <code><em>30</em></code> scratchcards!</p>
145 <p>Process all of the original and copied scratchcards until no more scratchcards are won. Including the original set of scratchcards, <em>how many total scratchcards do you end up with?</em></p>
146 </article>
147 <p>Your puzzle answer was <code>5554894</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
148 <p>At this point, you should <a href="/2023">return to your Advent calendar</a> and try another puzzle.</p>
149 <p>If you still want to see it, you can <a href="4/input" target="_blank">get your puzzle input</a>.</p>
150 <p>You can also <span class="share">[Share<span class="share-content">on
151 <a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22Scratchcards%22+%2D+Day+4+%2D+Advent+of+Code+2023&amp;url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F4&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
152 <a href="javascript:void(0);" onclick="var ms; try{ms=localStorage.getItem('mastodon.server')}finally{} if(typeof ms!=='string')ms=''; ms=prompt('Mastodon Server?',ms); if(typeof ms==='string' && ms.length){this.href='https://'+ms+'/share?text=I%27ve+completed+%22Scratchcards%22+%2D+Day+4+%2D+Advent+of+Code+2023+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F4';try{localStorage.setItem('mastodon.server',ms);}finally{}}else{return false;}" target="_blank">Mastodon</a
153 ></span>]</span> this puzzle.</p>
154 </main>
155
156 <!-- ga -->
157 <script>
158 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
159 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
160 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
161 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
162 ga('create', 'UA-69522494-1', 'auto');
163 ga('set', 'anonymizeIp', true);
164 ga('send', 'pageview');
165 </script>
166 <!-- /ga -->
167 </body>
168 </html>