From 11733b6d15da75125d18bae91e125aa74a068b7a Mon Sep 17 00:00:00 2001 From: Matthias Bilger Date: Thu, 10 Dec 2020 07:45:31 +0100 Subject: [PATCH] day10 --- day10/data/data.txt | 94 +++++++++++++++++++++ day10/data/sample.txt | 31 +++++++ day10/data/sample4.txt | 11 +++ day10/index.html | 188 +++++++++++++++++++++++++++++++++++++++++ day10/module.py | 86 +++++++++++++++++++ 5 files changed, 410 insertions(+) create mode 100644 day10/data/data.txt create mode 100644 day10/data/sample.txt create mode 100644 day10/data/sample4.txt create mode 100644 day10/index.html create mode 100644 day10/module.py diff --git a/day10/data/data.txt b/day10/data/data.txt new file mode 100644 index 0000000..cc4a600 --- /dev/null +++ b/day10/data/data.txt @@ -0,0 +1,94 @@ +71 +30 +134 +33 +51 +115 +122 +38 +61 +103 +21 +12 +44 +129 +29 +89 +54 +83 +96 +91 +133 +102 +99 +52 +144 +82 +22 +68 +7 +15 +93 +125 +14 +92 +1 +146 +67 +132 +114 +59 +72 +107 +34 +119 +136 +60 +20 +53 +8 +46 +55 +26 +126 +77 +65 +78 +13 +108 +142 +27 +75 +110 +90 +35 +143 +86 +116 +79 +48 +113 +101 +2 +123 +58 +19 +76 +16 +66 +135 +64 +28 +9 +6 +100 +124 +47 +109 +23 +139 +145 +5 +45 +106 +41 diff --git a/day10/data/sample.txt b/day10/data/sample.txt new file mode 100644 index 0000000..e6376dc --- /dev/null +++ b/day10/data/sample.txt @@ -0,0 +1,31 @@ +28 +33 +18 +42 +31 +14 +46 +20 +48 +47 +24 +23 +49 +45 +19 +38 +39 +11 +1 +32 +25 +35 +8 +17 +7 +9 +4 +2 +34 +10 +3 diff --git a/day10/data/sample4.txt b/day10/data/sample4.txt new file mode 100644 index 0000000..ec4a03f --- /dev/null +++ b/day10/data/sample4.txt @@ -0,0 +1,11 @@ +16 +10 +15 +5 +1 +11 +7 +19 +6 +12 +4 diff --git a/day10/index.html b/day10/index.html new file mode 100644 index 0000000..9dd6ef5 --- /dev/null +++ b/day10/index.html @@ -0,0 +1,188 @@ + + + + +Day 10 - Advent of Code 2020 + + + + + + + +

Advent of Code

m42e (AoC++) 18*

  0.0.0.0:2020

+ + + +
+ +

--- Day 10: Adapter Array ---

Patched into the aircraft's data port, you discover weather forecasts of a massive tropical storm. Before you can figure out whether it will impact your vacation plans, however, your device suddenly turns off!

+

Its battery is dead.

+

You'll need to plug it in. There's only one problem: the charging outlet near your seat produces the wrong number of jolts. Always prepared, you make a list of all of the joltage adapters in your bag.

+

Each of your joltage adapters is rated for a specific output joltage (your puzzle input). Any given adapter can take an input 1, 2, or 3 jolts lower than its rating and still produce its rated output joltage.

+

In addition, your device has a built-in joltage adapter rated for 3 jolts higher than the highest-rated adapter in your bag. (If your adapter list were 3, 9, and 6, your device's built-in adapter would be rated for 12 jolts.)

+

Treat the charging outlet near your seat as having an effective joltage rating of 0.

+

Since you have some time to kill, you might as well test all of your adapters. Wouldn't want to get to your resort and realize you can't even charge your device!

+

If you use every adapter in your bag at once, what is the distribution of joltage differences between the charging outlet, the adapters, and your device?

+

For example, suppose that in your bag, you have adapters with the following joltage ratings:

+
16
+10
+15
+5
+1
+11
+7
+19
+6
+12
+4
+
+

With these adapters, your device's built-in joltage adapter would be rated for 19 + 3 = 22 jolts, 3 higher than the highest-rated adapter.

+

Because adapters can only connect to a source 1-3 jolts lower than its rating, in order to use every adapter, you'd need to choose them like this:

+
    +
  • The charging outlet has an effective rating of 0 jolts, so the only adapters that could connect to it directly would need to have a joltage rating of 1, 2, or 3 jolts. Of these, only one you have is an adapter rated 1 jolt (difference of 1).
  • +
  • From your 1-jolt rated adapter, the only choice is your 4-jolt rated adapter (difference of 3).
  • +
  • From the 4-jolt rated adapter, the adapters rated 5, 6, or 7 are valid choices. However, in order to not skip any adapters, you have to pick the adapter rated 5 jolts (difference of 1).
  • +
  • Similarly, the next choices would need to be the adapter rated 6 and then the adapter rated 7 (with difference of 1 and 1).
  • +
  • The only adapter that works with the 7-jolt rated adapter is the one rated 10 jolts (difference of 3).
  • +
  • From 10, the choices are 11 or 12; choose 11 (difference of 1) and then 12 (difference of 1).
  • +
  • After 12, only valid adapter has a rating of 15 (difference of 3), then 16 (difference of 1), then 19 (difference of 3).
  • +
  • Finally, your device's built-in adapter is always 3 higher than the highest adapter, so its rating is 22 jolts (always a difference of 3).
  • +
+

In this example, when using every adapter, there are 7 differences of 1 jolt and 5 differences of 3 jolts.

+

Here is a larger example:

+
28
+33
+18
+42
+31
+14
+46
+20
+48
+47
+24
+23
+49
+45
+19
+38
+39
+11
+1
+32
+25
+35
+8
+17
+7
+9
+4
+2
+34
+10
+3
+
+

In this larger example, in a chain that uses all of the adapters, there are 22 differences of 1 jolt and 10 differences of 3 jolts.

+

Find a chain that uses all of your adapters to connect the charging outlet to your device's built-in adapter and count the joltage differences between the charging outlet, the adapters, and your device. What is the number of 1-jolt differences multiplied by the number of 3-jolt differences?

+
+

To begin, get your puzzle input.

+

Answer:

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file diff --git a/day10/module.py b/day10/module.py new file mode 100644 index 0000000..aa6af5c --- /dev/null +++ b/day10/module.py @@ -0,0 +1,86 @@ +from aoc.input import get_input +import copy +import itertools +import time +import collections +import re +from aoc.partselector import part_one, part_two +import functools + +def pw(line): + return int(line.strip()) + +def p1(): + inp = get_input(pw) + inp.append(max(inp)+3) + print(sorted(inp)) + last = 0 + result = { 0: 0, 1: 0, 2:0, 3:0 } + for sample in sorted(inp): + result[sample - last] +=1 + last = sample + print(result) + print(result[1] * result[3]) + segments = [] + for index, sample in enumerate(sorted(inp)): + if (sample - last) == 3: + segments.append(index) + last = sample + print(segments) + + return segments + + +def p2(segments): + last = 0 + count = 0 + inp = get_input(pw) + inp.append(max(inp)+3) + inp = sorted(inp, reverse=True) + last = 0 + result = { 0: 0, 1: 0, 2:0, 3:0 } + lst = [(0, inp)] + ok = True + parts = [] + ls = 0 + for segment in segments: + parts.append(inp[len(inp)-segment:len(inp)-ls]) + ls = segment + + pcount = [] + lasts = 0 + for part in parts: + lst = [(lasts, part)] + lasts = part[0] + ok = True + count = 0 + while ok: + if len(lst) == 0: + break + sample = lst.pop() + last = sample[0] + newlst = sample[1][:] + while sample[0] - newlst[-1] >= -3: + if len(newlst) == 1: + count +=1 + break + lst.insert(0, (newlst[-1], newlst[:-1])) + newlst.pop() + pcount.append(count) + print(pcount) + print(functools.reduce(lambda a,b: a*b, pcount)) + return 0 + +result1 = None +if part_one(): + start = time.time() + result1 = p1() + print(round(1000*(time.time() - start), 2), 'ms') + + +if part_two(): + start = time.time() + p2(result1) + print(round(1000*(time.time() - start), 2), 'ms') + +