Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Advent of Coding
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Bohn
Advent of Coding
Commits
8c738249
Verified
Commit
8c738249
authored
1 year ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Completed 2023 Day 04
parent
0f41761f
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
2023/python/Day04.py
+61
-0
61 additions, 0 deletions
2023/python/Day04.py
with
61 additions
and
0 deletions
2023/python/Day04.py
0 → 100644
+
61
−
0
View file @
8c738249
from
math
import
log2
from
typing
import
List
,
Tuple
from
ImportData
import
import_data
day
:
int
=
4
sample_data
:
List
[
str
]
=
'''
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
'''
.
split
(
'
\n
'
)[
1
:
-
1
]
data_structure
:
type
=
List
[
Tuple
[
List
[
int
],
List
[
int
]]]
def
parse_data
(
data
:
List
[
str
])
->
data_structure
:
cards
:
List
[
Tuple
[
List
[
int
],
List
[
int
]]]
=
[]
for
card
in
data
:
numbers
:
List
[
str
]
=
card
.
split
(
'
:
'
)[
1
].
split
(
'
|
'
)
winning_numbers
:
List
[
int
]
=
[
int
(
number_string
)
for
number_string
in
numbers
[
0
].
strip
().
split
()]
your_numbers
:
List
[
int
]
=
[
int
(
number_string
)
for
number_string
in
numbers
[
1
].
strip
().
split
()]
cards
.
append
((
winning_numbers
,
your_numbers
))
return
cards
def
get_score
(
card
:
Tuple
[
List
[
int
],
List
[
int
]])
->
int
:
score
:
int
=
0
winning_numbers
:
List
[
int
]
=
card
[
0
]
your_numbers
:
List
[
int
]
=
card
[
1
]
for
number
in
your_numbers
:
if
number
in
winning_numbers
:
score
=
1
if
score
==
0
else
2
*
score
return
score
def
part1
(
data
:
data_structure
)
->
int
:
return
sum
([
get_score
(
card
)
for
card
in
data
])
def
part2
(
data
:
data_structure
)
->
int
:
copy_count
:
List
[
int
]
=
[
1
for
_
in
data
]
number_of_cards
=
len
(
data
)
for
i
in
range
(
number_of_cards
):
card
:
Tuple
[
List
[
int
],
List
[
int
]]
=
data
[
i
]
score
:
int
=
get_score
(
card
)
if
score
>
0
:
count
:
int
=
int
(
log2
(
score
))
+
1
# kludgey
for
j
in
range
(
i
+
1
,
min
(
number_of_cards
,
i
+
count
+
1
)):
copy_count
[
j
]
+=
copy_count
[
i
]
return
sum
(
copy_count
)
if
__name__
==
'
__main__
'
:
production_ready
=
True
raw_data
=
import_data
(
day
)
if
production_ready
else
sample_data
print
(
part1
(
parse_data
(
raw_data
)))
print
(
part2
(
parse_data
(
raw_data
)))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment